ssh and scp without password

January 31, 2009

If you have to login to your server using ssh, typing the password every time gets annoying pretty fast. You also need to get around the password check for automating scp.

It’s very easy to setup ssh so that it works without typing in your password. It works by creating a DSA key on your machine and sending the public portion of it to the server. If the server has your public key, it will allow you to login without a password.

The screenshot for this post is from OS X, but it will work on most unix platforms and under cygwin on Windows.

First you need to create a key on your local machine. Open terminal and type:


mkdir ~/.ssh
chmod 0700 ~/.ssh
cd ~/.ssh
ssh-keygen -t dsa -f $HOME/.ssh/id_dsa -P ''

This creates the .ssh folder in your home directory and generates a DSA key. You should have two files: id_dsa (this is the private key, don’t give this to anyone) and id_dsa.pub (the public portion):

id_dsa and id_dsa.pub keys

id_dsa and id_dsa.pub keys

Now send id_dsa.pub to the server. You can do this straight from the terminal:


scp ~/tmp/id_dsa.pub {YOUR_SERVER.COM}:

Login to the server and add the public key to the authorized hosts file:


cat id_dsa.pub >> ~/.ssh/authorized_keys2
chmod 0600 ~/.ssh/authorized_keys2

Now you should be able to ssh and scp to the server without typing in your password.

The keys are machine and user specific, but you can of course add more computers, just repeat the above procedure on each. I use multiple computers (at least apple and one microsoft flavored) and login to my web server using this method from all of them.

In case the server still asks for your password, you might need to add the key to another file:


cat id_dsa.pub >> ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys

2 Responses to “ssh and scp without password”

  1. There is a typo

    cat id_dsa.pub >> ~/.ssh/authorized_keys
    chmod 0600 ~/.ssh/authorized_keys2

    should be

    cat id_dsa.pub >> ~/.ssh/authorized_keys
    chmod 0600 ~/.ssh/authorized_keys

  2. @Arun: thanks for catching that! I fixed it.

Leave a Reply