SaltyCrane Blog — Notes on JavaScript and web development

Creating remote server nicknames with .ssh/config

Using the ~/.ssh/config file is an easy way to give your remote machines nicknames and reduce the number of keystrokes needed to login with ssh, rsync, hg push/pull/clone, access files via Emacs Tramp (Transparent Remote (file) Access, Multiple Protocol), or use any other SSH-based tool. You can also set other ssh options such as IdentityFile, Port, or CompressionLevel. For more information and a full list of options, check out the man page for ssh_config or this article by Kimmo Suominen.

Here is part of my ~/.ssh/config file. It defines the nicknames turk, tyran, tuna, and tally for some EC2 servers I've been working with.

Host turk
  User root
  HostName ec2-67-202-21-122.compute-1.amazonaws.com

Host tuna
  User root
  HostName ec2-75-101-178-62.compute-1.amazonaws.com

Host tyran
  User root
  HostName ec2-67-202-43-207.compute-1.amazonaws.com

Host tally
  User root
  HostName ec2-67-202-59-207.compute-1.amazonaws.com

Now, wherever I would normally have typed root@ec2-67-202-21-122.compute-1.amazonaws.com, I can just type turk. Here are some examples.

SSH login

Old way:

ssh root@ec2-67-202-21-122.compute-1.amazonaws.com

New way:

ssh turk

rsync

Old way:

rsync -avz myproject root@ec2-67-202-21-122.compute-1.amazonaws.com:/srv

New way:

rsync -avz myproject turk:/srv

Mercurial

Old way:

hg push ssh://root@ec2-67-202-21-122.compute-1.amazonaws.com//srv/myproject

New way:

hg push ssh://turk//srv/myproject

Emacs Tramp

To use your ~/.ssh/config with Emacs Tramp, you will need something like the following in your .emacs:

(tramp-set-completion-function "ssh"
                               '((tramp-parse-sconfig "/etc/ssh_config")
                                 (tramp-parse-sconfig "~/.ssh/config")))

Old way:

C-x C-f /root@ec2-67-202-21-122.compute-1.amazonaws.com:/srv/myproject/myfile.py

New way:

C-x C-f /turk:/srv/myproject/myfile.py

scp

Old way:

scp etc/.screenrc root@ec2-67-202-21-122.compute-1.amazonaws.com:/root

New way:

scp etc/.screenrc turk:/root

Comments


#1 Marek Karwowski commented on :

Nice one. I found your post writing post explaining ssh config file in bit more details.


#2 Christina Lee commented on :

Alternatively, you could put the contents and save it to that file, but it’s best to make sure to open it if it already exists. Hosting w/ SSH Access

disqus:1975551192