O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  



Modified (cleaner) ssh-to
Variation of ssh-to that doesn't require new files in your ~/bin directory for every new target.

Contributed by:
bt2001
[11/27/06 | Discuss (0) | Link to this hack]

While the previous ssh-to script works quite well, the following implementation is a bit cleaner and uses just the user's ~/.ssh/config file for reference. I got here because I have to log into many machines, and each may be on a different port and have a different username set up for me. I found the ssh config file, which is designed to deal with these issues, and then reasoned that it would be simpler and cooler if anything I added to the ssh config file automatically became an ssh-to without any additional work.

First, set up your ~/.ssh/config file. This file allows you to supply short names for your machines, as well as a different user, port number and other options for each target if you'd like. The format is very simple.

Host jschmoe User jdoe HostName JoeSchmoe.example.com Port 4022 ForwardX11 yes

man ssh_config
for more information and a listing of available options. Having this file allows you to do
jane@localhost:~ $ ssh jschmoe
and login to joeschmoe.example.com as user jdoe on port 4022.

Then, to take it one step further and because less typing is better, simply put the following lines in your ~/.bashrc file.

for name in `sed -n "/^Host/s/^Host //p" ~/.ssh/config`; do alias $name="ssh $name" done

This allows you to then issue

jane@localhost:~ $ jschmoe
to perform the above login.

See also:


  • man ssh_config

  • "Quick Logins with ssh Client Keys" (Linux Server Hacks #66)"

  • "Turbo-mode ssh Logins" (Linux Server Hacks #67)"

  • "Using ssh-Agent Effectively" (Linux Server Hacks #68)


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.