Recently I has to do some work by sshing into Seneca’s matrix server, and I thought to myself: there has got to be a quicker way to do this. Every time I wanted to ssh, I had to type out
ssh scarter4@matrix.senecac.on.ca
…and then wait, and then enter your password… and then wait…. and finally I log in.
Now, it gets a little tiresome to keep on typing all that out all the time. So like a good Linux sysadmin (read: lazy), I wanted to automate this as much as possible. My end result was this:
C:\home\evets>matrix
Last login: Tue Jan 27 09:23:29 2009 from net1.senecac.on.ca
Have a lot of fun...
scarter4@matrix:~>
(Yes, my command prompt is like dos. No, I’m not actually running dos; It really is Linux.)
So how did I accomplish this magic? read on…
Part 1: Passwordless ssh login
The first part of this trick is to get rid of that annoying password prompt. I did some digging on this, and I found that ssh can in fact be set up to use a private/public key pair instead of a password to authenticate. That way, the machines can automatically authenticate without the need for user input
So first, we need to go to the client machine, the one we are trying to run ssh from. On this computer, we generate a key pair to use with ssh with the command
ssh-keygen -t rsa
This command will ask you a bunch of questions, and then set up a key pair for you. The important thing here is that when it asks for a passphrase to use the key, leave it blank. That way, you won’t have to type in a passphrase instead of your password (which kind of defeats the purpose). Note that this IS inherently less secure, but not completly. Remember that you need to have you private key in order to authenticate, so only someone who is using your computer and is logged in as you will be able to use the private key to authenticate. Putting a passphrase on the key means that whenever you use your private key, it will ask you to authenticate and make sure that you’re really you.
so, anywho, once you’ve done that, you should have a nice new shiny key pair stored in “~/.ssh/” yaaaay. Now what? Well, in order to use your key to authenticate, the server you’re trying to connect to needs to have your public key. Use the following command to give the key to your server:
ssh-copy-id -i ~/.ssh/id_rsa.pub username@mystery
The server you’re connecting to will ask you for your password one last time, and then the “ssh-copy-id” command will magically take care of the rest, copying the files to the right spot on the server, setting permissions, and whatnot.
Done! Now try sshing to the server. It should work without asking you for a password.
(If you’re still confused about this, here’s the article that I found that explained it for me:
http://www.debian-administration.org/articles/152)
Part 2: Bash Aliases
So that automates the password, but at this point, I still have to type out “ssh scarter4@matrix.senecac.on.ca” every single time I want to ssh to matrix. How do we shorten that up? Very easily.
In your home directory, there is a file called .bashrc (or if it doesn’t exist yet you can create it), and whatever you put in this file will be run every time you log in. This configuration file can be used for lots of things, but one common use is to use it to set up aliases. Here’s what’s in my .bashrc file:
PS1='C:${PWD//\//\\\}>'
alias ls='ls -hF --color'
alias ll='ls -l'
alias cds='cd ~/storage'
alias nano='nano -i -k -m -c'
alias matrix='ssh scarter4@matrix.senecac.on.ca'
(That first line is what makes my bash prompt look like dos)
Here I’ve defined a bunch of aliases, which are short little commands that you can run instead of a big long command. Essentially, it is a way to create custom commands. So for instance, every time I want to run ls, it actually runs “ls -hF –color” which adds color formatting to ls. I find it easier to read, but I don’t want to type out “ls -hF –color every time! That last line however, means that I can just type “matrix”, and bash will expand that to the full ssh command, which will then log in without the password because that was set up beforehand. You can actually use whatever you want for the alias name. Pick something that is easy to remember for you, and is easy to type! If you wanted, you could even set the alias to MICROSOFTISAWESOME, and then everytime you type in MICROSOFTISAWESOME it would run whatever you put in quotes after the = .
So, the end result: I type “matrix”, and I log in to matrix. Easy as that. I hope this helps some people.