Setting up ssh public keys and adding an alias
This post is sort of a ‘note-to-myself’ post. For my Computer Science class, I have to do a lots of ssh-ing into remote host – my university server . I got tired of typing username and password every time I try to connect from home (using my MacBook Pro). I’ve seen people using an alias without even typing a username, password, and a hostname. I’ve once set up the ssh keys on my HP laptop but after I sold the laptop (I wiped out everything before selling :), I’ve been lazy setting up a keys; no more.
Here are the steps I did.
Let suppose our remote server (in my case, university computer) is called server.universityname.edu, your usual username is foouser, and you want to give an alias sshserv to connect to the remote server using ssh.
- Create a local ssh key on your local computer
$ ssh-keygen -t -rsa
There are other algorithms you can use to create your ssh key. I’m using rsa for this tutorial. ssh key algorithms and secure algorithms are too broad to be covered in this tutorial.
Anyway, you should have a id_rsa.pub file inside ~/.ssh folder. Do
$ ls ~/.ssh/
and make sure you have a id_rs.pub file
- Copy the public file to your remote server:
$ scp ~/.ssh/id_rsa.pub foouser@server.universityname.edu:.ssh/authorized_key_local
You will be prompted for password. Use your usual ssh password.
- Log in to your remote server to manage the key:
$ ssh foouser@server.universityname.edu
Again, you will be prompted for a password.
- Concatenate the contents of ~/.ssh/authorized_key_local to ~/.ssh/authorized_keys:
$ cat ~/.ssh/authorized_key_local >> ~/.ssh/authorized_keys
5. Remove the ~/.ssh/authorized_key_local file which is not required anymore:
$ rm ~/.ssh/authorized_key_local
6. Logout from the remote server:
$ exit
7. You are now set so that you don’t need the password every time you ssh into your remote server. Verify this by:
$ ssh foouser@server.universityname.edu
At this point, you shouldn’t be asked for any password. If you are prompted for a password, please make sure your properly followed this tutorial up to this point.
- If you are in your remote server, exit so that you are on local computer:
$ exit
Now, let’s setup an alias.
- Edit your ~/.ssh/config file to add the following. If the file doesn’t exist, create it first.
#The Host is the alias that you want to use
Host=myremoteserv
#The Hostname is the real hostname of the remote server
Hostname=server.universityname.edu
#The user is your username
User=foouser
- Save the file and try:
$ ssh myremoteserv
You should be able to login to your remote server.
- If you want to save 3 keystrokes every time you ssh using above command, you can add an alias for the whole command in your ~/.bash_profile file:
alias sshserv="ssh myremoteserv"
4. To use the alias ‘sshserv’, either restart the terminal or update your bash profile:
$ source ~/.bash_profile
5. Now, you can login to your remote server by just using:
$ sshserv
Happy ssh-ing!