How to SSH without username or password

Normally when you SSH into a computer, you need to provide a username and it will prompt you for your password, like this:

$ ssh joe@server_machine
joe@server_machine's password:

By generating a public-private key pair on the local machine and sharing the public key with a remote machine, you can SSH to the remote machine without providing a login or being prompted for a password.

After this setup is done, you will operate like this:

$ ssh server_machine
$ That is it! You are logged in without login or password!

To set this up:

  • Generate a public-private key pair on your local computer. If you already have one (probably you generated it for Github) and want to use that, then skip this step.
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

This generates a public key in ~/.ssh/id_rsa.pub and its corresponding private key in ~/.ssh/id_rsa file.

  • We need to add the public key from local machine to the ~/.ssh/authorized_keys file on the remote computer you are logging to. You can do this manually. However, the ssh-copy-id script does this for you. Just pass the remote computer name as input:
$ ssh-copy-id remote_machine

If there is no ~/.ssh/authorized_keys file on the remote machine, this script will create it for you. If the file exists, your public key from local machine will be appended to the existing file.

  • Make sure that the permissions of the .ssh directory is -rwx------ and of the .ssh/authorized_keys file is -rw-------. Otherwise SSH will determine that the keys are not safe and you will still get asked for your password.

  • That is it! You can now SSH directly to this server machine!

Reference: Arabesque: Linux Crypto – SSH Keys

Tried with: Ubuntu 14.04

Advertisements

One thought on “How to SSH without username or password

  1. Client Part:-

    I tried to connect to remote machine without username, so I typed the command β€œssh-copy-id remote_machine” and this the result;
    β€”β€”β€”β€”β€”β€”β€”-
    $ ssh-copy-id -i id_rsa.pub remote machine
    /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: β€œid_rsa.pub”
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed β€” if you are prompted now it is to install the new keys
    Permission denied (publickey).
    ———————–
    and I typed command β€œls” and the result;
    $ ls ~/.ssh/
    id_rsa id_rsa.pub known_hosts known_hosts.old
    
    and I typed β€œssh remote machine” and the result is;
    Permission denied (publickey).
    
    Could you tell me please what shall I do, and please be noted that I am in client part, and can’t do anything in server part?
    
    Thanks
    

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.