The SSH Config File: What It Is and How to Use It
When you SSH into your servers or connect to them via terminal, you may have noticed that the process can be time-consuming if you have to enter in all of the server addresses each time. Luckily, there’s an easy solution: SSH config files! This handy file keeps track of all of your frequently accessed servers and makes connecting to them through the command line much easier by using the ssh [email protected] format instead of IP addresses or hostnames. Here’s how to set one up on macOS!
Introducing the SSH configuration file
If you manage a lot of servers, you know that connecting to them can be a hassle. Fortunately, there’s a way to make it easier: by using an SSH config file. The basic idea is that this file contains the public key for each server and allows for automatic connection with one command. You just need to save the config file on your local machine in a specific location (/Users/UserName/.ssh/config) and modify it with your information. For example, if your username is user1 on our data center server and the hostname is dcserver1, then in the config file would look like this: Host dcserver1
- User user1
- IdentityFile ~/.ssh/id_rsa
- KeyAuthentication yes
- StrictHostKeyChecking no – PubkeyAcceptedKeyTypes ssh-rsa,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
- A few things to note about this config file
What you can do with it
An SSH config file can be used to configure SSH connections on a per-host basis. This means that you can specify different options for each host, making it easier to connect to your data center servers. The config file is located in the ~/.ssh directory, and you can edit it with any text editor. To make an edit, first open the file and then scroll down to find the Host directive.
A blank Host directive has a single line of code defining it as an empty block – which means all commands are executed by default. You can define your own blocks by adding multiple lines underneath this line; they will apply only to hosts specified after them (using Host) directives. For example, if you want to change the user name for connecting to just one server from root to server1, add this line below the Host directive:
Host server1 User server1
Creating the file
An SSH config file allows you to create shortcuts for commonly used SSH connections. This can be useful if you have multiple servers that you connect to frequently. To create an SSH config file, open a text editor and add the following lines at the top of the document.
- Host server1
- HostName server1
- User Username
- Password Password
- IdentityFile ~/.ssh/id_rsa_server1
- Then list your other hosts below it like so:
- Host server2 HostName server2 User username Password password IdentityFile ~/.ssh/id_rsa_server2 Host server3 HostName server3 User username Password password IdentityFile ~/.ssh/id_rsa_server3
Editing the file
If you’re using a Mac, there’s a file you can edit to make it easier to connect to your data center servers. This file is called the SSH config file, and it’s located in the ~/.ssh directory. You can edit this file with any text editor, but I prefer nano. To open the file in nano, type this into the terminal: $ nano ~/.ssh/config Press enter to confirm editing mode. Now, we’ll need to add a few lines of text to the file. First, we’ll enter our username (or [email protected]) so that our connection won’t ask for us to enter that information each time we try connecting: Host [email protected] Next, we’ll enter the hostname or IP address of the server we want to connect to: