• ARTICLES
SEARCH

How-To Geek

Stupid Geek Tricks: Use Your SSH Config File to Create Aliases for Hosts

banner-01

This trick is for Linux and SSH users who often log in to remote systems. Having to type the same info over and over again is mind-numbingly repetitive, but using an SSH config file makes the process much more convenient.

It’s pretty easy to turn

scp –P 50001 username@remote.sshserver.com:somefile ./somefile

ssh –p 50001 username@remote.sshserver.com

into something quick:

scp remotehost:somefile ./somefile

ssh remotehost

Aside from specifying port numbers, addresses, and user names, you can specify key files, time out intervals, and tons of other options. All it takes is one little file.

Fire up a text editor and point it to this file:

~/.ssh/config

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. Here’s the basic format of what you need to put (or add to what you have).

config file

Replace “your_alias_name” with a short name for this connection. Something like “home,” “work,” or “asdf” should suffice. ;-)

Substitute your username, and the web address (or IP address for destinations inside of your network) instead of remote.sshserver.com. Lastly, if you use a custom port (anything other than 22, the default), specify that. Otherwise, you can skip that last line.

Next, I created a pair of key files on my remote server to use, so I didn’t have to supply a password each time. For more information, check out How To Remotely Copy Files Over SSH Without Entering Your Password and skip down to the “SSH and SCP Without Passwords” section for all of the details.

key pairs

Now, you can add an extra line and point it to your key file.

IdentityFile ~/path/to/id_file

id file

Let’s add a “keep alive” function to our connect, shall we? This will prevent your connection from ending by refreshing your connection every X seconds, a maximum number of Y times:

ServerAliveInterval X

ServerAliveCountMax Y

alive int

Our example will refresh every 2 minutes for a maximum of 30 consecutive times. That means, it will stop refreshing after an hour. This works from your client regardless of what your server has configured.

You can add multiple servers this way by adding another section with a difference Host section. And, if you want to create a set of default options, you can set the Host value to a single asterisk (*). Here’s a great example file:

final

sshot-1

Much better!

This is handy for situations when creating a bash alias isn’t an option. It also makes it easier to keep track of all of your SSH-based options (from the client-side) in one consolidated place. If you plan to use this for scripts, you can also use

BatchMode yes

to disable prompts to enter passwords. Of course, you’d still need to authenticate using key files.

If you want to look more into the StrictHostKeyChecking option, be sure to Learn the Ins and Outs of OpenSSH on Your Linux PC. Of course, there’s a cornucopia of options available to you in the OpenSSH man page, too.

Yatri Trivedi is a monk-like geek. When he's not overdosing on meditation and geek news of all kinds, he's hacking and tweaking something, often while mumbling in 4 or 5 other languages.

  • Published 09/22/11

Comments (4)

  1. Johann

    I used to use this but quickly moved on to just having my .login script parse my ~/.ssh/known_hosts file and create an alias such that merely typing the hostname would log me in (usernames are the same on all my hosts):

    foreach hostid ( `cat -q ~/.ssh/known_hosts | cut -d’,’ -f1 | cut -d’ ‘ -f1 | sort -u` )
    set srvname=`echo ${hostid} | sed -e ‘s/e$//g’`
    alias ${srvname} “ssh -A ${USER}@${hostid}”
    end

    As I connect to new hosts SSH automatically adds them to the known hosts file and so next time I log in the alias for the new host will be created. If you want to set a port forward you can of course just amend the alias line in the for loop. It’s not for everyone but if you work in a big environment and systems come and go, it’s better than manually configuring the ~/.ssh/config file.

  2. Johann

    OOPS!! The for loop mentioned above has a little sed command in it to strip the character ‘e’ from the end of my hostname aliases!! This is a company specific thing to do with public and private IPs, and obviously won’t be needed in a normal environment. The loop would more normally just be:

    foreach hostid ( `cat -q ~/.ssh/known_hosts | cut -d’,’ -f1 | cut -d’ ‘ -f1 | sort -u` )
    alias ${hostid} “ssh -A ${USER}@${hostid}”
    end

    Sorry about that!!!

  3. thorstenmz

    For me,
    alias connect=’ssh me@remote.host.de
    in my .bashrc does the job.

    For copying files, I have a bash script named “put” containing
    scp $3 $1 me@remote.host.de:$2
    and one named “get”
    scp $3 me@remote.host.de:$1 $2
    where $1 is the source and $2 ist the destination and $3 is an optional -r for subdirs.

  4. Florian Beer

    Apart from several entries in my .ssh/config I’ve learned to love the following handy little snippet in my .bashrc

    complete -W “$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ‘ ‘ | sed -e s/,.*//g | uniq | grep -v “\[“`;)” ssh

    This will allow you to “tab complete” every host, that’s in your .ssh/known_hosts file (aka servers you’ve successfully connected to in the past).

    See my full bash setup here: http://blog.no-panic.at/2011/09/15/my-bash-configuration/

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!