facebook youtube pinterest twitter reddit whatsapp instagram

Simplifying SSH Connections With a Config File

One issue I have when I am connecting to my remote servers is that I manually type the server I am connection to by hand, for example
What if you could simplify the above to:

ssh username

Easy peasy!

In this guide, you will learn how to simplify ssh connections with a config file.

The way this works is you create a configuration file for servers that you connect to often, this way, ssh knows your server and can automatically pull the info when you reference it by a name.

Go ahead and open up your config file in a text editor:

sudo nano ~username/.ssh/config

The tilde (~) is referencing the username home directory.

If you don't have the config file, the command would automatically create one as long as you have the .ssh directory.

The config file is a per-user configuration file, and since you would be adding your server ssh configuration in this file, you are highly recommended to create strict permission: read/write for the user, and not writable by others (I would show you how to do this in a moment).

Also, it may be group-writeable provided that the group in question contains only the user. Back to the config file, let's create a read & write permission for the user, in my case the file is own by root:

-rw-r--r-- 1 root   root    137 Feb 29 20:11 config

Change the group and user to the username that should access the config file:

sudo chown username:username config

This would change both the user and group of the config file to the username you specify.

I want to make the config file only readable and writeable by the user, this way no other user can access, use the following command:

sudo chmod 600 config

You can learn more on Permissions on Files & Directories

Now, go ahead and structure the config file this way:

host serverA
     Hostname 192.168.66.5
     Port 45
     User userserver_a

host serverB
     Hostname panel.website.dev
     Port 22
     User userserver_v

In the above example, I have 2 hosts outlined, serverA & serverB. I identified a way to reach serverA by its IP address and serverB by its fully qualified domain name, you can use whatever soothes you, it just makes much sense to use the FQDN (Fully Qualified Domain Name), this way, even if you change your IP address, you'll still be able to connect as you aren't relying on IP addresses.

Back on track..

Now, if I want to connect to serverA, I'll do

ssh serverA

It is as simple as that.

I should also mention that the host (the first line) can be anything, I might decide to name it baby1 and it would still connect as long as you fill in the user.

That is it about simplifying ssh connections with config files, see you later ;)

 

Related Post(s)

  • Using Pageant To Automatically Authenticate SSH key in Putty

    I can't count how many times I have typed my ssh key passphrase whenever my ssh connection times out, it is so annoying and repetitive. Well, thanks to the putty pageant, you can do that seamlessly.

  • Synchronize File With Cloud Storage Using [Rclone] In (GNU/Linux)

    Rclone is an open-source command-line program to sync files and directories to and from different cloud storage providers. It preserves timestamps, and if you are transferring from local to cloud, it

  • Finding Out File Using Up Disk Space With [du] in GNU/Linux

    It is a bit annoying when your disk space gets full, and you have no idea what is eating up the disk space, well, let me introduce you to ducommand, with this command you can get the disk usage of t

  • Simple Way to Fix SSH Permission Denied (Public key) error

    Most of the time when you encounter permission denied (public key) error, it is not because the public key doesn't exist but because the private key can't find the public key. If you are connecting f

  • Beginners Guide To SSH Key Management

    When you connect to a server via SSH from the client (the machine you want to use to establish the connection to the server), you will be asked to authenticate using the password of the user trying t