Pages

Friday, 15 July 2016

How Many Way to do Auto Ssh Login


There are multiple way to do automatic ssh login. Few i will demonstrate today.

        1) Public and private Key authentication.
        2) Re-using ssh connection with ControlMaster
        3) Using sshpass.

Prerequisites

        1) Two VM's (IP's)
        2) Server VM should have openssh-server and client vm should have openssh-client
        3) For the ubuntu you can install openssh-server and openssh-client thought apt-get install {package_name}


Public and private Key authentication


        Steps For Server VM


               Create Key-pair

      # ssh-keygen -t rsa -b 4096 -C "thegeeklinux@gmail.com"

        NOTE : It will ask for passphrase, you can leave blank or you can type passphrase. If you are typing passphrase then be remember you will need this passphrase while login to client.


               Copy Public key to client VM

      # scp /home/ubuntu/.ssh/thegeeklinux.pub ubuntu@ssh-client:~/


        Steps For client VM

        Copy public key data to authorized_keys file in /home/ubuntu/.ssh/ folder. If authorized_keys file is not there then you can create that file also. In my case i don't have authorized_keys file so i am creating.

 # cat /home/ubuntu/thegeeklinux.pub > /home/ubuntu/.ssh/authorized_keys


        Try to login From the server with private key.

      # ssh -i /home/ubuntu/.ssh/thegeeklinux ubuntu@ssh-client

Re-using tcp connection with ControlMaster

        The concept is very simple — rather than make every time new connection , you can use one SSH TCP connections. The authentication only happens once, when the TCP connection is opened, and then after all other connection will use same socket. To use this option we need to enable ControlMaster in ssh-server config.
        Open /home/ubuntu/.ssh/config file and add following lines.

        # vi /home/ubuntu/.ssh/config
            Host *
            ControlMaster auto
            ControlPath ~/.ssh/sockets/%r@%h-%p
            ControlPersist 60000


        When you do first login it will create one tcp connection socket and while doing next login on same host it will use same tcp socket. It will useful when some process is doing thousand of ssh connection.

Using SSHPASS

        For this option you need to install sshpass package. To install this package in ubuntu run "apt-get install sshpass".
        With sshpass command we can send password along with ssh command.

        # sshpass -p 'ubuntu' ssh ubuntu@openssh-client

    Here is the demo of way to do ssh login automatically


                                
<>

No comments:

Post a Comment