Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I need to run ssh-add <key> everytime I need to ssh into a webserver. Is there a way to add the ID permanently, so I dont have to keep adding the identities on each login?

EDIT: The key is a pem file, that I have downloaded from a cloud service.

Thanks.

share|improve this question

6 Answers

up vote 17 down vote accepted

Generate your key like normal: ssh-keygen then place that key to the remote server with ssh-copy-id which will sync it to the remove servers accepted keys.

ssh-keygen
ssh-copy-id user@host

It will prompt for your password then perform all the steps necessary to link your .pub key with the remote SSH server.

By default it will copy all your .pub keys to the remove server. If you just created your key with ssh-keygen then this isn't a problem (because you only have one!). However, if you have multiple keys you can copy just a specific key with the -i flag.

ssh-copy-id -i ~.ssh/key_name.pub user@host

Replacing key_name.pub with the name of the key.

share|improve this answer
1  
why doesn't this have more upvotes? :-) – Stefano Palazzo Jan 11 '11 at 11:50
That is a sweet command, can't believe i just found out about it! thanks – JavierIEH Dec 3 '12 at 11:48

You can generate a ssh key with the command:

ssh-keygen

Then you can copy your key to the server with:

ssh serveruser@servername "echo `cat ~/.ssh/id_dsa.pub` >> ~/.ssh/authorized_keys" 

Now you can automatically log in your webserver

share|improve this answer
To copy the key to ther server you can just use ssh-copy-id -i ~/.ssh/id_dsa.pub serveruser@servername or ~/.ssh/id_rsa.pub respectively. Actually, if you use the default name for the key file you do not even have to specify it. – Carsten Thiel Jan 10 '11 at 14:26

If your key is password-less and named as one of the files ssh will try to look for when identifying (~/.ssh/id_dsa or ~/.ssh/id_rsa), you shouldn't have to add it to your agent.

BUT. If there's the slightest possibility of those files being stolen, you would have just allowed anyone to access the servers on which you are using this identity. In short, pwned.

IMHO, password-less private keys are a bad practice, and should be used only on environments where ~/.ssh/authorized_keys is very restrictive.

share|improve this answer
ssh-agent can keep your decrypted private key, while you are logged in. If enabled, the gnome-keyring can even use your login password. Oh, and ssh-copy-id can copy your public key to servers, too. – Frank Jan 10 '11 at 14:02
 ssh serveruser@servername "cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys" 
share|improve this answer

Write a short shell script which will run ssh-add and then connect such as the following:

ssh-add ~/.ssh/your-key
ssh user@remotehost

You can then ssh into your host with one command.

share|improve this answer

Are you talking about Amazon Cloud? In your ~/.bashrc, create environment variables:

# Amazon
export EC2_PRIVATE_KEY=$HOME/Keys/pk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.pem
export EC2_CERT=$HOME/Keys/cert-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.pem
export JAVA_HOME=/usr/lib/jvm/java-6-openjdk/
share|improve this answer
yes, I talk about the cloud, but this is the key-pair to ssh into the instance, and the above environment variables are necessary to use the api-tools :) – theTuxRacer Jan 10 '11 at 16:27
Ok. So generate an RSA key with no password: 'ssh-keygen -t rsa' and then concatenate the generated public key (~/.ssh/id_dsa.pub) to the remote server's ~/.ssh/authorized_keys file. A one-liner to accomplish this was given by many. – user8290 Jan 10 '11 at 17:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.