The contents on this site are being moved to Highlander Nexus

CreateSSHConfig

From Linux and MacOSX Wiki
Jump to: navigation, search

Creating an SSH Config file


Purpose: This documentation will help you create an SSH config file that allows convenient access to remote machines.


An SSH config file is helpful when multiple SSH keys are used for different hosts or if one would like to SSH to a machine without passing extra arguments in the command line such as username, port or options. It is a great way to organize hostnames and saves time.

The config file resides in the .~/ssh folder of the user's home directory.

~/.ssh/config

Open the file in your preferred editor.

  • For this example, it is assumed Github is configured for the user and the private key is located in the ~/.ssh folder. For help on configuring Github with SSH, please refer to the Generating an SSH key documentation.

  • At the top of the file, enter:

    1. Begin Github section

    Host github.com
     HostName github.com
     IdentityFile ~/.ssh/id_rsa
     User git

    1. End Github section


    Save and close the file.

    The name of the private key should be the name of your private Github key.

    Test that everything is working with ssh -T github, where 'T' disables pseudo-tty allocation. You should see the following message:

    You've successfully authenticated, but GitHub does not provide shell access.

    Now you can use Github without needing to include your private key in each commit.

  • Previously, when using SSH to access remote hosts, one would enter:

  • ssh user@host.domain

    However, the config file provides the possibility to simply:

    ssh host.domain

    Which can be aliased to a keyword. Please refer to the Aliases documentation for more information.

    To configure this, add the following below the Github addition and save the file:

    1. Begin afs section

    Host afs1
     HostName afs1.njit.edu
     User ucid

    1. End afs section

    Where ucid is your NJIT UCID.

    Then, ssh afs1 to logon to afs1.njit.edu <p>The SSH config file also takes SSH options such as VirtualHostKey, IdentityFile, CheckHostIP, Port, User, etc.

  • If a user's SSH config file contains a minimum number of hosts that use different usernames but a maximum number of hosts with the same username name it makes sense to configure the use of that username for all hosts in which the User field is not set.

  • For example:

    1. Begin domain example section

    Host domain
     HostName domain
     HostName nameserv.domain
     IdentityFile ~/.ssh/nameserv_rsa
     User rick

    1. End domain example section

    In the above example, the User "rick" is specified. However, if multiple hosts follow which use similar usernames, it is sensible to apply that username to all hosts in which there is no User set.

    1. Begin host monty section

    Host monty
     HostName montypython.domain
    Host verdun  HostName verdun.domain
    Host teleport  HostName teleport.domain

    1. End host monty section

    To allow these hosts to use the supplied username, the following is added to the end of the config file:

    Host*
     User jon


    This will append User jon as the username when using SSH to access the hosts monty, verdun and teleport. It is important that it be added to the end of the file or it will not work!

    The Port option is also necessary when, on the host, SSH is not listening on the default port of 22. If this is the case, the following can be added to any host:

    Port #

    Where, "#" is the port SSH is listening on.


    For example:

    1. Begin host midas section

    Host midas
     HostName midas.domain
     User kingmidas
     Port 23

    1. End host midas section

    The Port option can also be applied to all hosts that share a common port number.


    For example:

    1. Begin example config section

    Host monty
     HostName montypython.domain

    Host verdun
     HostName verdun.domain

    Host teleport
     HostName teleport.domain

    Host*
     User jon
     Port 23

    1. End example config section

    In the above example SSH config file, all three hosts listen on port 23 for SSH.

  • There are many options that can be added to the SSH config file to make accessing remote hosts easier. Please refer to the official SSH documentation for more information by running the following in the command line: man ssh