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 [email protected]
, I can just type turk
. Here are some examples.
SSH login
Old way:
ssh [email protected]
New way:
ssh turk
rsync
Old way:
rsync -avz myproject [email protected]:/srv
New way:
rsync -avz myproject turk:/srv
Mercurial
Old way:
hg push ssh://[email protected]//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 /[email protected]:/srv/myproject/myfile.py
New way:
C-x C-f /turk:/srv/myproject/myfile.py
scp
Old way:
scp etc/.screenrc [email protected]:/root
New way:
scp etc/.screenrc turk:/root
Related posts
- Magit in Spacemacs (evil-magit) notes — posted 2018-11-02
- Switching from Emacs to Vim (actually Spacemacs) — posted 2015-12-31
- Colorized, interactive "git blame" in Emacs: vc-annotate — posted 2011-05-28
- My Emacs Python environment — posted 2010-05-10
- Emacs espresso-mode for jQuery — posted 2010-03-10
- Notes on C++ development with Emacs on Ubuntu Linux — posted 2009-07-08
Comments
Nice one. I found your post writing post explaining ssh config file in bit more details.
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