- Understanding SSH config file with Examples
- What is an ssh config file?
- Why should I use an SSH config file?
- How do I create an SSH config file?
- Where should I store my SSH config file?
- Example of SSH config file
- can I use host pattern in an SSH config file?
- What options are available in an SSH config file?
- Can I use an SSH config file with all types of ssh connections?
- Can I store my private key in an SSH config file?
- What if I don’t have an SSH config file?
- How do I change the settings in my SSH config file?
- How do I connect to a server using my specified SSH config file?
- What if I have multiple SSH keys?
- What if I need to connect to a server that doesn’t have an ssh config file?
- What is the difference between a System-wide and User-specific SSH config file?
- Can I use both a System-wide and User-specific SSH config file?
- Use SSH Config File to Manage SSH Connections to Various Remote Servers
- Using SSH config file for easily connecting to remote servers
- Step 1: Create the SSH config file
- Step 2: Add an SSH profile in the config file
- Step 3: Adding multiple profiles in SSH config file
- Order of the SSH configuration
- There’s a lot more to SSH config
Understanding SSH config file with Examples
If you’ve ever worked with ssh, you know that it can be a bit of a pain to configure. There are so many options and parameters! One way to make working with ssh a bit easier is by using an ssh config file. In this blog post, we will discuss what ssh config files are, how to use them, and some of the benefits they offer.
What is an ssh config file?
An ssh config file is a text file that contains all of your ssh connection information. This includes the hostname of the server you’re connecting to, the username you’re using to connect, the port number, and the protocol you want to use. You can also specify a key file to use for authentication, as well as other options.
Why should I use an SSH config file?
There are many benefits to using an SSH config file. Some of the most common benefits include:
- Consistent settings across all servers – This makes it easy to keep your settings consistent across all of your servers. You can create separate configurations for each server you connect to, or you can create global configurations that apply to all servers.
- Easy configuration for multiple connections – You can create separate configurations for each situation, allowing you to easily specify different settings for different situations. For example, you could create a configuration that uses a specific key file when you’re connecting from your office, and another configuration that uses a different key file when you’re connecting from home.
How do I create an SSH config file?
There are a few different ways to create an ssh config file. Here is one example.
1. Launch the terminal application on your local computer and create your config file in your home directory:
touch ~/.ssh/config
NOTE:
Your .ssh directory is automatically created when you use the ssh command for the first time. If you have never used ssh before under this user account please create the directory first using:
mkdir ~/.ssh/ && chmod 700 touch ~/.ssh/config
2. Edit the file using vi or any Unix text editor you are comfortable with. We will use vi in this example:
vi ~/.ssh/config
Where should I store my SSH config file?
The location of your SSH config file will vary depending on your operating system and the software you’re using to edit it. The most common locations are:
- macOS: /etc/ssh/ssh_config or ~/.ssh/config
- Linux: /etc/ssh/ssh_config or ~/.ssh/config
- Windows: C:\Program Files (x86)\PuTTY\Configuration\ or %APPDATA%\Roaming\.putty\Configuration
Example of SSH config file
Host server
HostName www.howtouselinux.com
User howtouselinux
Port 4242
IdentityFile ~/.ssh/id_rsa
When we run ssh server command, it will try to login server www.howtouselinux.com with user howtouselinux, port 4242 and private key file ~/.ssh/id_rsa.
can I use host pattern in an SSH config file?
A pattern for Host directive is nothing but IP address, DNS hostname, or combination of special wildcard characters. For example, ? wildcard that matches exactly one character. * wildcard matches zero or more characters. The following example matches the host a.www.howtouselinux.com and b.www.howtouselinux.com.
Host *.www.howtouselinux.com
User howtouselinux
IdentityFile ~/.ssh/id_ed25519.pub
What options are available in an SSH config file?
The ssh config file contains all of your ssh connection information, including the hostname of the server you’re connecting to, the username you’re using to connect, the port number, and the protocol you want to use. You can also specify a key file to use for authentication, as well as other options.
Can I use an SSH config file with all types of ssh connections?
Yes, you can use an SSH config file with all types of ssh connections. The configuration options will vary depending on the type of connection being used. The following is an example.
### default for all ##
Host *
ForwardAgent no
ForwardX11 no
ForwardX11Trusted yes
User howtouselinux
Port 22
Protocol 2
ServerAliveInterval 60
ServerAliveCountMax 30
Can I store my private key in an SSH config file?
Yes, you can store your private key in an ssh config file. This will allow you to use your private key for authentication without having to enter it each time.
Host host
HostName 192.168.1.100
User root
IdentityFile ~/.ssh/host.key
What if I don’t have an SSH config file?
If you don’t have an ssh config file, then you’ll need to enter your connection information each time you connect to a server. This can be tedious and error-prone, so it’s a good idea to create a configuration file for your most common connections.
How do I change the settings in my SSH config file?
The settings in your SSH config file can be changed by editing the text file itself or by using a graphical interface like PuTTY. On Linux and macOS, we can use vi command. On Windows, PuTTY provides its own graphical interface for editing ssh config files.
How do I connect to a server using my specified SSH config file?
To use your specified SSH config file, you’ll need to specify the location of the file when you connect to a server. The command will vary depending on your operating system and software. For example, on macOS you would use “ssh -F /etc/ssh/ssh_config username@server”. On Linux, the command is usually “ssh -F ~/.ssh/config username@server”.
Specifies an alternative per-user configuration file. If a configuration file is given on the command line, the system-wide configuration file (/etc/ssh/ssh_config) will be ignored. The default for the per-user configuration file is ~/.ssh/config.
What if I have multiple SSH keys?
If you have multiple SSH keys, you can specify which key to use for authentication by adding the “IdentityFile” option to your ssh config file. For example, “IdentityFile ~/.ssh/id_rsa”. This will tell ssh to use the id_rsa key file for authentication of this host.
What if I need to connect to a server that doesn’t have an ssh config file?
If you need to connect to a server that doesn’t have an ssh config file, then you’ll need to enter your connection information manually. You can also create a global SSH config file that will apply to all servers.
What is the difference between a System-wide and User-specific SSH config file?
- System-wide OpenSSH config file client configuration – /etc/ssh/ssh_config : This files set the default configuration for all users of OpenSSH clients on that desktop/laptop and it must be readable by all users on the system.
- User-specific OpenSSH file client configuration – ~/.ssh/config or $HOME/.ssh/config : This is user’s own configuration file which, overrides the settings in the global client configuration file, /etc/ssh/ssh_config.
Can I use both a System-wide and User-specific SSH config file?
Yes, you can use both a System-wide and User-specific SSH config file. The ssh command reads its configuration in the following order:
David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.
howtouselinux.com is dedicated to providing comprehensive information on using Linux.
We hope you find our site helpful and informative.
Use SSH Config File to Manage SSH Connections to Various Remote Servers
Using SSH profiles can help you in cases where you regularly connect to various servers. No need to remember the IP address and other such details for SSH connection.
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
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.
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:
Host website Hostname 275.128.172.46 User alice Port 1500
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:
Host website Hostname 275.128.172.46 User alice Port 1500 Host forum-server Hostname 275.128.172.47 User alice Host main-server Hostname 275.128.172.49 Host common-test-server Hostname test-server Host * User root
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 [email protected]
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
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.