SSH Tricks: Working With a Config

Discussion in 'Linux Networking' started by Eric Hansen, Nov 27, 2013.

  1. Eric Hansen

    Eric Hansen Moderator Staff Writer

    Messages:
    124
    Likes Received:
    87
    Trophy Points:
    28
    SSH is pretty much the go-to method of remotely logging into a machine in terms of Linux. Its so vital that it often comes installed by default for server-intended OSes. Its even used for code versioning systems (CVS) such as Git to do a bulk of what its use is for.

    What happens, however, when you have too many hosts that you need to connect to, or even have too many connections to make on one host (using different credentials)? Well, you can either do it the old fashioned long-hand way, or use a custom SSH config file!

    Here's a real life scenario I experience daily that will give you some idea as to how effective this can be. There's 5 servers I need to manage at any given point. On top of that, I have 3 different CVS accounts that I need to use as well (2 for GitHub and 1 for BitBucket). That means I have 8 different SSH profiles I need to manage. Even with using keyfiles for authentication it gets annoying having to type "ssh ehansen@server1.example.tld" or "scp some.tar.gz ehansen@server1.example.tld:." Being able to connect via "ssh server1" or "scp some.tar.gz server1:." is a lot easier, no?

    What File To Edit?
    You need to edit the ~/.ssh/config file. If it doesn't already exist that's fine (typically isn't unless you've done this already).

    Where To Start?
    Lets take my server1.example.tld as a sample here. We'll want to be able to reference this host by the name of "server1" and use the keyfile ~/.ssh/server1 for authentication and log in as "ehansen". Here's what we need to have:
    Code:
    Host server1
        HostName server1.example.tld
        IdentityFile /home/ehansen/.ssh/server1
        User ehansen
    If "Host" would be "server1.example.tld" or we added an entry to our resolv.conf file to point server1 to a specific location, then we wouldn't have to add the HostName field, but that's not the point of this article. :)

    HostName is the FQDN of the server we want to connect to. IdentityFile is the keyfile (via ssh-keygen) that we want to use for authentication. The "User" option isn't necessary if you're already logged in on your machine as that user, but its always nice to specify regardless.


    Now, instead of having to even type "ssh server1.example.tld" we can just type "ssh server1". SSH will then parse the config file and see we're wanting the Host server1 and use the settings made from there.

    You can do this for as many hosts as you want, and if you're curious what other options you can set just read the man page for ssh_config (5) and it will give you all the information you need.

    Attached Files:

    • slide.jpg
      slide.jpg
      File size:
      59.1 KB
      Views:
      217,287
  2. FrankK

    FrankK New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Dear Eric,
    How about .bash_aliases?
    Code:
    alias server1='ssh -X user@serverip'
    when only using locally Iǘe added sshpass:
    Code:
    alias server2='sshpass -f '/somewhere/.passwordfile' ssh -X user@serverip
    and connect with
    Code:
    user@system:-$ server1
  3. CesarCara

    CesarCara New Member

    Messages:
    22
    Likes Received:
    3
    Trophy Points:
    3
    If I can only access my server by IP?
    Would it be like this?

    Code:
    Host server1
        HostName 192.168.x.x
        IdentityFile /home/ehansen/.ssh/server1
        User ehansen
  4. Eric Hansen

    Eric Hansen Moderator Staff Writer

    Messages:
    124
    Likes Received:
    87
    Trophy Points:
    28
    Yeaup! Of course changing the IdentityFile and User of the server information, but overall that'd be correct. ;)
    CesarCara likes this.

Share This Page