
For me as a Linuxw user I always had to type long lines to start my SSHw sessions. It’s no rocket science, you know, but for he who types thousands of characters each day, each character save is golden.
We usally start our SSH sessions by typing the following command string:
ssh user@domain.tld
I’ve tried to shorten this by adding the domain and its TLDw to my hosts file with a shorter alias like
hname hostname.tld
But obviously that crashed against Apachew Vhostw capability, the request never make it to the other end.
I personally have SSH keys on some of my servers to avoid typing passwords, that is good to prevent against MITMw attacks and keyloggers, but I still have to type the string mentioned lines above.
Then, with 3 lines in a SSH’s configuration file on my home directory (yes, you don’t need to alter the server’s SSH configuration file and yes, you can do that on your shared hosting’s shell account).
It’s very simple. SSH allows some user-based configuration in your ~/.ssh directory (note that ~ stands for your home directory (usually /home/yourusername) and it also provides Aliasesw capability.
The aliases can be configured in such a way that you won’t need to type neither the FQDNw nor even type a single username EVER MORE!
Your old
ssh user@domain.tld
will be shortened to
ssh aliasnameOk, talk is cheap, show me the code!
Creating the need files and directories
All the magic occurs on the config in your already-mentioned ~/.ssh/ directory. If you don’t have one, create it and secure it, as the following:
mkdir ~/.ssh/ chmod 700 ~/.ssh/
If the file does not exists, create it and secure it propperly, like:
touch ~/.ssh/config chmod 600 ~/.ssh/config
Syntax model
Host [alias name] HostName [host's fqdn] User [username to log-in as]
Without the braces, please!
For a user called jdoe on mydomain.com being shorted md, the code would be the following:
Host md HostName mydomain.com User jdoe
Save the file and you can start typing less with SSH:
ssh mdAnd that’s IT!
Wow that’s great! What else this beauty can do?
Omnipresence
Do you have the same username in a large number of machines, like in a local network? So you have to love the * wildcard.
Host * User jdoe
I have many names
Want to follow a naming pattern but also wants to have shorter ones? Give multiple aliases!
Host gdf gandalf HostName gandalf.mydomain.com User jdoe
Avoid the front door
Security through obscurity is always fun! Have your SSH aliases even in hosts with sshd running in a different port.
Host md HostName mydomain.com User jdoe Port 11
Be backwards compatible
Have outdated SSH servers? I hope not! But if you do, you may also define a different protocol in a host-based manner.
Host md HostName mydomain.com User jdoe Protocol 1
Handle multiple keys
You may specify the specific (what?) SSH key file for a host.
Host md HostName mydomain.com User jdoe IdentityFile ~/mykey.pub
And it also works on SCP!
Oh yeah, file transfers are easier now!
scp somefile.txt md:/path/to/upload
More parameters can be found on SSH manual, page 5.
man ssh 5
Phew! That’s all folks! Hope it helps someone as much as helped me! Anyone have any neat SSH trick?
Just added a few aliases here, really saves some typing.
Thanks for the tip!
Bruno Lustosa
March 12, 2010 at 12:44 pm