Using ssh in Terminal without entering password
When we log in to a server using ssh, we need to enter password every single time, which is getting tedious after several times. So how do we automate this proces; we’re using a computer after all.
ssh is capable of generating an encrypted file with an identification, and another file with a public key that is supposed to be on the server. After this is done, you can log in to the server where you put your public key, and the server does not prompt for a password.
The following instruction works on Mac OS X, and should work on Unixes too. There are only three lines; copy one at a time to the Terminal, and change some of it to your own username, host server name, etc. If you are going to do this, you also know what to change.
Here it begins:
ssh-keygen -t rsa — It asks for some stuff, just hit Enter (three times).
ssh username@ssh.server.com mkdir -p .ssh — Server password
cat .ssh/id_rsa.pub | ssh username@ssh.server.com 'cat >> .ssh/authorized_keys' — server password.
That was all.
When you execute the first command your computer prompts you for a file name, for a pass phrase, and for a confirmation of the pass phrase. This is where you just hit Enter.
This worked for me, however, you could use scp to transfer the public key file to the server, but that is another story. The result of this, is that you can enter ssh username@ssh.server.com and you are taken directly into the server, not prompted for password as usual.
So is this sufficiently easy? I prefer a short command like serverssh to ssh username@ssh.server.com, so let us make a new command that takes us into your user account on the host server. Open a text editor, like e.g. Smultron, and type the following:
#!/bin/sh
ssh username@ssh.server.com
Save it to a command name of your choice — I use domainssh. Then I have another command for domainftp — for each of my domains and for university. Just save the file to your home directory, and we will move it using Terminal.
chmod 700 domainssh Administrator password prompted.
sudo mv domainssh /usr/local/bin/
Done.
Hey, thanks for the tutorial, it’s really to the point. I had some problems still though…
Had no problem creating the key, had no problem creating the directory, but as soon as I execute this command:
PowerBook-G4:~ spencerhill$ cat .ssh/id_rsa.pub | ssh username@server.com ‘cat .ssh/authorized_keys’
Welcome to server!
username@server.com‘s password:
It tells me this:
cat: .ssh/authorized_keys: No such file or directory
It didn’t give me any errors creating the directory, so I’m totally lost…
Hey Spencer, I don’t know what the problem is, and I do not have time to look into it right now. I will see if I can get it fixed this weekend and update the post with a new solution, so other people can benefit from it too.
When it works it is very convenient, and especially if you have also made a shell script (a file with only one line) so you just type ssh_yourserver and it logs on, with no password prompt.
Fremragende guide. Tusind tak for hjælpen
Spencer, you forgot to type >> right after cat.
tenks admin you power blog !! cammozaikler.com
?????, ? ???? ?????? ????? ?? ?????? c ?????? ? ?? ???? ??????? ??? – ????? ? ?????. ?????? ??? ? ????????? ??? ?????? ??…
Gunnar Vestergaard has it right. It has to be exactly as written above.
I have looked back to my own blog post for doing this again a few times.
If you are asked if you want to overwrite the file after you enter the first line (ssh-keygen), just send “n” for no. It means, you have already done this, and you do not need to make a new keys for each server you log in to. If you have already made one, you just go directly to line two (the one with mkdir).
ssh-keygen -t rsa — just hit Enter (three times).
ssh username@remoteserver.com mkdir -p .ssh — it asks for remote server password
cat .ssh/id_rsa.pub | ssh username@remoteserver.com ‘cat >> .ssh/authorized_keys’ — asks for remote server password.
In the above code (in this comment), I have added “remote” to the server names, since it may be a little confusing if you are already standing on a server and logging in to a new server. You are never asked for the password to the server you are standing on, only the one you attempt to log in to and save a public key.