Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to be able to login to a remote via ssh without having to enter the password all the time.

  • How do I set it up?
  • Is a different command required to execute a password-less session?
share|improve this question
    
Are you using openssh? (if so that's easy ;) ) –  Rinzwind Jun 4 '11 at 17:29
    
@Rinzwind, Why would I bother getting the proprietary version when OpenSSH is already preinstalled? –  Oxwivi Jun 4 '11 at 18:18
4  
I try to not make assuptions ;) –  Rinzwind Jun 4 '11 at 18:21
add comment

8 Answers 8

up vote 53 down vote accepted

Answer

Execute these two commands:

ssh-keygen

Then you'll need to copy the new key to your server.

After the key is copied, ssh into the machine as normal

ssh user@host

You can now login without entering a password from the particular machine you executed the commands at.

Example

not-marco@rinzwind-desktop:~$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/home/not-marco/.ssh/id_rsa): 
Created directory '/home/not-marco/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/not-marco/.ssh/id_rsa.
Your public key has been saved in /home/not-marco/.ssh/id_rsa.pub.
The key fingerprint is:
b1:25:04:21:1a:38:73:38:3c:e9:e4:5b:81:e9:ac:0f not-marco@rinzwind-desktop
The key's randomart image is:
+--[ RSA 2048]----+
|.o= . oo.        |
|*B.+ . .         |
|*=o .   o .      |
| = .     =       |
|. o     S        |
|E.               |
| o               |
|  .              |
|                 |
+-----------------+
not-marco@rinzwind-desktop:~$ ssh-copy-id not-marco@127.0.0.1
not-marco@127.0.0.1's password: 
Now try logging into the machine, with "ssh 'not-marco@127.0.0.1'", and check in:

  ~/.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

Explanation

This assumes you already have successfully connected to your server via SSH.

You'll need to generate an SSH Keypair which will allow you to identify you as yourself without using a password. You can opt to protect keys with a passcode if you wish, but this can be left blank allowing totally password-less SSH access.

First create your SSH Keypair by running ssh-keygen this will create an id_rsa and id_rsa.pub file. The pub file is what goes on the servers, the private key (id_rsa) is what stays with you and is how you identify yourself.

Next copy the public key to your server with ssh-copy-id user@server replacing user with your remote user and server with the machine DNS name or IP address. It'll prompt for your SSH password, enter it and if all completes successfully you'll be able to access the machine via ssh user@server without needing a password.

References

share|improve this answer
    
kyentei's answer seems far simpler. –  Oxwivi Jun 4 '11 at 18:34
4  
@Oxwivi this answer is the more correct way to do it - but it seems longer. All you need to do is type ssh-keygen follow the on-screen instructions, then type ssh-copy-id user@server replacing user with your remote user and server with the remote machine –  Marco Ceppi Jun 4 '11 at 18:36
1  
I had this error "Agent admitted failure to sign using the key." every time when trying to login after following this procedure. The solution was to run "> ssh-add" on the local machine and now I can log to the remote machine as expected. –  jmbouffard Jul 5 '11 at 15:23
    
Best thing about your answer is that you give good explanations :) I lost an hour trying to do it the other way - generating the keys o the server machine, not the client. Though probably it is obvious how/where to do it, it is not when you do it for the first time. Thank you ! –  deckoff Mar 6 '12 at 19:28
    
marco did most of the answer ;) @deckoff –  Rinzwind Mar 6 '12 at 19:47
show 2 more comments

I normally use sshpass for that, install it with sudo apt-get install sshpass and use it like this

sshpass -p 'password' ssh your_username@your_server
share|improve this answer
4  
Why not to use ssh keys? –  enzotib Dec 6 '11 at 17:39
    
Its not a "why not" situation, it still works perfect without adding keys, its just another method I would say. –  Bruno Pereira Dec 6 '11 at 18:05
    
Thanks for the information on sshpass, never heard of it before. –  bodhi.zazen Dec 6 '11 at 18:39
1  
SSH keys are the "correct" answer to the question, but sshpass is a very useful hack in cases when you can't change the authentication method on the remote server! –  Jacob Krall Mar 14 at 15:27
add comment

Type the following commands:

  1. ssh-keygen

    Press Enter key till you get the prompt

  2. ssh-copy-id -i root@ip_address

    (It will once ask for the password of the host system)

  3. ssh root@ip_address

Now you should be able to login without any password.

share|improve this answer
add comment

The way I usually do this is as follows:

ssh-keygen -t rsa

(When prompted for a password, leave it blank)

Then: cat ~/.ssh/id_rsa.pub | ssh username@hostname 'cat >> .ssh/authorized_keys'

(This requires the folder .ssh to be in the home directory on the targeted hostname, with the authorized_keys file in it)

Of course, replace username with the desired username, and hostname with the desired hostname or IP address

After that, just SSH to that box just like you're used to.

share|improve this answer
    
What about the touch and chmod command in Rinzwind's answer? –  Oxwivi Jun 4 '11 at 18:29
    
You'll need to chmod the .ssh/authorized_keys file to 0600 or this will not work –  Marco Ceppi Jun 4 '11 at 18:36
add comment

If you create a public/pricate keypair und log in using the public key method you will not need to type your password. That is depending on the configuration of your key-ring and ssh agent.

Here is one of many short howtos for you. It is of crucial importance to the safety of this method, that the thusly generated private key remains private!

share|improve this answer
add comment

To make some additions:

  • Mac by default doesn't have ssh-copy-id, you'll have to install it youself:

    curl https://raw.github.com/beautifulcode/ssh-copy-id-for-OSX/master/ssh-copy-id.sh -o /usr/local/bin/ssh-copy-id
    chmod +x /usr/local/bin/ssh-copy-id
    

find more here: https://github.com/beautifulcode/ssh-copy-id-for-OSX

  • if you've made a port-forwarding, the command should be like this:

    ssh-copy-id "not-marco@127.0.0.1 -p 2222"
    

note that the quotes are necessary.

share|improve this answer
add comment

Following article explains in clear and concise way. I use it myself for a quick reference.

http://manjeetdahiya.com/2011/03/03/passwordless-ssh-login/

To answer your second question. Once the password-less ssh setup is done. You just need to use exactly the same commands as before.

share|improve this answer
    
Welcome to Ask Ubuntu! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. –  Eric Carvalho Nov 7 '13 at 10:51
add comment
ssh-keygen
ssh-add
ssh-copy-id userId@host
share|improve this answer
2  
Please elaborate on what should be done here - headlines alonne are not really helpful. –  guntbert May 31 '13 at 21:48
add comment

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.