Remote Ssh Config File



Managing remote servers requires either a very good memory to remember connection options like usernames, remote addresses, ports and further details or a good way to document all the details for each server.

The SSH config file should be your helping hand to control and simplify SSH connections. If you didn't have any SSH client installed yet, please go ahead and do it. OpenSSH is our tool of choice.

Put ssh myhostname in the command line, and then add the command cd /folder/anotherone/muchmuchdeeper/ in the send text at start field. When you connect to the sessions it will ssh to your hostname and cd to the directory in your. The sshdconfig file is an ASCII text based file where the different configuration options of the SSH server are indicated and configured with keyword/argument pairs. Arguments that contain spaces are to be enclosed in double quotes ('). In the sshdconfig file the keywords are case-insensitive while arguments are case-sensitive. Visual Studio Code Remote - SSH: Editing Configuration Files The Remote - SSH extension lets you use any remote machine with a SSH server as your development environment. This extension complements the Remote - SSH extension with syntax colorization, keyword intellisense, and simple snippets when editing SSH configuration files. Ssh (1) obtains configuration data from the following sources in the following order: 1. Command-line options 2. User's configuration file (/.ssh/config) 3. System-wide configuration file (/etc/ssh/sshconfig) For each parameter, the first obtained value will be used. Ssh(1) obtains configuration data from the following sources in the following order: 1. Command-line options 2. User's configuration file (/.ssh/config) 3. System-wide configuration file (/etc/ssh/sshconfig) For each parameter, the first obtained.

Complicated Connections

Connecting to a remote server via SSH requires a username, server url or IP address and the SSH server port listening for connections. Let's look at a specific example. Assuming your server url is yourserver.url with user marcus on port 2222.

Your connection command looks like this:

All parameters are required to pass within the connection string.

Simplify with SSH Config File

The SSH config file isn't created automatically while installing SSH on your machine. The config file needs to be placed into your .ssh folder. By default, the location is ~/.ssh. Let's create the config file using nano command line editor. Since nano is Linux specific, you can use any other editor of your choice.

This command opens the nano editor with a blank file. Saving to disk will create the desired SSH config file.

We define the file content using the key-value system. Each key-value-pair is stated in a separate line. A key gets its value assigned by separating both by either whitespace or equal sign or a combination of equal sign with spaces. The SSH clients interpret all statements identical.

Each configuration in your config file is initiated by the keyword Host followed by an identifier.

Common SSH configuration options

  • HostName: the hostname or IP address of your remote server. You can skip this definition if the Host identifier already specifies the actual hostname you want to connect with.
  • User: the connection username.
  • Port: the port where your remote SSH server is listening for connections. Default value 22.

The options above describe the basic configuration for an entry in the SSH config file. There are additional SSH connection items and tweaks which can be used for more complex setups.

General Tweaks and SSH connection items

  • Compression: a useful option for (very) slow connections.
  • ServerAliveInterval: use this option to let both peers stay in contact and avoid session closes due to SSH timeouts. Configure this option to let SSH send a packet to keep the connection between client and server alive. Also, you can use this option to know if your unreliable connection is still alive.
  • StrictHostKeyChecking: this option is used to configure whether SSH automatically adds hosts to the ~/.ssh/known_hosts file. By default, you're asked to confirm the addition to the known hosts. The default value can be annoying connecting to multiple different hosts, so you may want to set this to no and add every connected remote host to known hosts automatically.

Actually, there are more options to configure SSH. You can keep those items listed above in mind and in case you run into issues with your SSH connections, change the values and check whether they improve.

Complete configuration entry example:

Of course you can define multiple entries in your config file. Just separate them by an empty line

The second example entry omits the HostName definition because it's already set as Host identifier.

Just save the file and leave nano. Your created file will be recognized by your SSH client for future connections.

Connect Painlessly

From now on, you can use the defined Host identifier for any connection to your remote server.

Your SSH client parses the config file and matches the defined Host identifier values with your provided identifier. In case they match, the specific configuration gets automatically loaded from the config file.

That's all the magic! Enjoy the simplification of your SSH configuration.

Ssh

Additional Ressources

If you are even a tad bit familiar with SSH, you know that you can use it to connect to remote Linux systems.

Using SSH to connect to remote system is simple. All you need to do is to use a command like this:

This connects to the default SSH port 22. You may specify the port as well if you want.

Now this is all plain and simple if you just have one server. Even if you don’t remember the server’s IP address, you can perform a reverse search to the history using the famous terminal keyboard shortcut Ctrl+R and find the SSH command you used in the past.

But things get complicated when you have several servers to manage. I have around ten servers that I connect to from time to time. Some are production servers and some are test servers.

Now keeping a track of these servers is not easy. Even if I can find the SSH commands from the history, it is difficult to guess which IP belongs to which server.

Of course, I can open my dashboards on Linode, UpCloud, DigitalOcean and Google Cloud to get the IP or keep a list on my local system.

A better and easier way is to use SSH config file.

Using SSH config file for easily connecting to remote servers

Config

The SSH config file allows you to create different profiles for different host configurations. There is no limit to such profiles and you may add as many as possible.

So, if you connect to multiple remote systems via SSH, creating SSH profiles will be a good move to save your time.

Let me show you how to use it.

Step 1: Create the SSH config file

When you install SSH, you’ll have a ~/.ssh directory created automatically. This direct contains your public key, private key a known_hosts file. Your config is also stored here.

Ssh config file git

At least on Ubuntu, the SSH config file is not created by default. You can easily create this file using the touch command like this:

Step 2: Add an SSH profile in the config file

Now that you have the SSH config file, you can edit it using Vim or Nano. Let me show you an example of the syntax which you should follow.

Let’s say you connect to a server with IP 275.128.172.46. Your username is Alice and the server is used for hosting your website. To harden SSH security, you use port 1500 instead of the default SSH port 22.

You can add all this information in the following manner in your ~/.ssh/config file:

Ssh config file format

Just save the information in the file. No need to restart any service.

Now, instead of writing a long command like this:

You can just use this command (tab completion works as well):

When you run the above command, ssh looks for a Host named website in the ~/.ssh/config. If it finds a host with that name, it gets all the information related and used it for making an SSH connection.

You might wonder about a few things, so I’ll mention it here:

  • There is no space or tab indention restriction while entering the host information. Space or tab indention are used for making the config file easily understandable.
  • The Hostname can be the IP address of the server or a hostname that can be resolved on your network.
  • All the parameters like hostname, user and port are optional. However, I personally advise keeping at least hostname because that’s what you need (and you forget) most of the time.
  • If your SSH config file is wrongly configured, it will result in an error when you try to use it for SSH connection.
  • You cannot save passwords in SSH config. I advise adding your public SSH key to the server for easy access.

Step 3: Adding multiple profiles in SSH config file

The previous step gave you an idea about how to add an SSH profile. Let’s take it to the next step by adding multiple profiles in it.

Here’s what the SSH config file looks like now:

File

This time, I have added four different SSH profiles in it.

Did you notice the Host * entry at the end of the file? You can use this entry to for adding a parameter common to all profiles if that parameter hasn’t been mentioned for the profile explicitly.

So if I try to use the main-server SSH profile, it will automatically take root user.

ssh main-server = ssh root@275.128.172.49

Order of the SSH configuration

The ssh configuration follows the following order:

  • command-line options
  • user’s configuration file (~/.ssh/config)
  • system-wide configuration file (/etc/ssh/ssh_config)

This means that the priority is given to the command you enter and then it looks into ~/.ssh/config and then in /etc/ssh/ssh_config.

So, if you want to override a profile, you can do that using the -o option of the ssh command.

For example, if I use this command:

It will take user bob instead of the user alice as defined in the ~/.ssh/config (in the previous step).

There’s a lot more to SSH config

Ssh Config File Remote Forward

To be honest, there is so much more to SSH config file that cannot be covered in a single article. You can use name/IP matching, subnets and what not.

The scope of this article was to introduce you to SSH config and help you create SSH profiles for easily connecting to various remote Linux systems.

You can always refer to the man page of ssh_config to know more about the parameters you can use while creating your SSH config file.

I hope this SSH tip was helpful to you. If you already use SSH config file and have a some nifty tip with you, do share it with the rest of us in the comment section.

Become a Member for FREE
Become a member to get the regular Linux newsletter (2-4 times a month) and access member-only contents.

Ssh Config File Remote Command

Join the conversation.