4 ssh config tips for faster remote working
by Matthias Willerich on April 17 2009, 12:00
With the vast majority of web developers deploying their own code, and ssh playing a central role in the toolbox, I felt that tweaking this part of my work saves me some time, and if not, definitely some frustration over mistyping passwords and host names.
So, for myself as much as everyone out there, here is my collected useful knowledge that has helped me ease the pain of remote work.
1. Execute commands remotely as one-liners
This is what some deployment tools such as vlad use, but for a quick look at something, it cuts out valuable seconds you could look at web comics instead.
Let's try a simple example:
matthias:~ matthias$ ssh firstname.lastname@example.org ls -l email@example.com's password: total 804 lrwxrwxrwx 1 remoteuser remoteuser 31 Nov 10 2007 access-logs -> /links/to/my/access-logs drwxr-xr-x 2 remoteuser remoteuser 2048 Apr 6 2008 cgi-bin -rw-r--r-- 1 remoteuser remoteuser 808417 Jul 17 2007 error_log_dump drwxr-x--- 3 remoteuser mail 2048 Oct 1 2007 etc drwxrwx--- 7 remoteuser remoteuser 2048 Apr 4 02:33 mail drwxr-x--- 3 remoteuser remoteuser 2048 May 23 2008 public_ftp drwxr-x--- 30 remoteuser nobody 2048 Dec 4 06:33 public_html drwxr-xr-x 7 remoteuser remoteuser 2048 Sep 24 2008 tmp lrwxrwxrwx 1 remoteuser remoteuser 11 Apr 1 2006 www -> public_html matthias:~ matthias$
You can even pipe through to a remote command. Here's an example taken from Look Here First, where the content of a local file is concatenated to a remote file.
matthias:~ matthias$ cat localfile.txt | ssh firstname.lastname@example.org "cat - >> remotefile.txt"
2. Add your public key to your remote server
Next let's see how we can get around typing our password all the time. One way of doing this is to add your public key to your remote server.
# first, upload public key from client to server client$ scp ~/.ssh/id_rsa.pub email@example.com:~ # next, setup the public key on server server$ mkdir ~/.ssh server$ chmod 700 ~/.ssh server$ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys server$ chmod 600 ~/.ssh/authorized_keys server$ rm ~/id_rsa.pub
Of course, we can combine with the previous example and run it like this:
matthias:~ matthias$ cat ~/.ssh/id_dsa.pub | ssh firstname.lastname@example.org "cat - >> ~/.ssh/authorized_keys2"
Windows users fear not, you can do this too, with PuTTY and PuTTYgen.
3. Let the ControlMaster handle your sessions
Sadly this might not always possible, maybe your key management on your server is different, or you don't have permissions to the file.
Luckily you can get around this by letting your first ssh connection act as ControlMaster. By adding the following to ~/.ssh/config, you only need to enter your password once, and all parallel sessions won't ask for a password
Host * ControlMaster auto ControlPath ~/.ssh/master-%r@%h:%p
4. Set your user and hostname for your host
Finally, more often than not your username won't be the same on your local machine and on whatever server you'd like to connect to, or your server might not use the default ssh port. Or maybe you're just tired of typing that superlong domain name, and look for a less challenging typing task. ~/.ssh/config to the rescue.
As final example:
Host myserver User remoteuser Port 22022 HostName myserver.pretendco.com
Most likely you won't need the Port instruction in there, because your server runs ssh on the default port 22, so just drop the line.
All this allows us something as compact as:
matthias:~ matthias$ ssh myserver mycommand
Have fun, and let's hope this got you into your weekend early!