Saltycrane logo

Sofeng's Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

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.

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

Pretty cool, eh? You can also specify the Port, CompressionLevel, and many other ssh settings. See the ssh_config(5) manpage for more information.


Post a comment

: Required
Email: Required, but not displayed
Website: Optional
:

Format using Markdown. (HTML not allowed.)
  • Code blocks: prefix each line by at least 4 spaces or 1 tab
  • Code span: surround with backticks
  • Blockquotes: prefix lines to be quoted with >
  • Links: <URL>
  • Links w/ description: [description](URL)
:

Created with Django | Hosted by Webfaction