3 min read

Tip: Use SSH Config Files to Save Time and Improve Your Workflow

If you find yourself connecting to the same servers over and over again, an SSH config file will save you time and energy.

If you find yourself connecting to the same servers over and over again, an SSH config file will save you time and energy.

Basic Usage

Let's consider a scenario where you have purchased a new backup server in your home network to keep all your photos, videos and documents safe. From time to time, you want to connect to this server with SSH:

ssh root@

If you're anything like me, you will likely forget the exact IP address of your backup server and the username, too. The best way to remember this information is to store it in an SSH config file at ~/.ssh/config:

Host backup
	User root

SSH config file

With this config file in place, we can now run:

ssh backup

This simplified command will connect to the backup machine, and you will not have to remember the IP address and username each time.

Advanced Usage

In a more complicated setup, let's imagine a scenario where you connect to your office's work machine. Your office has set up SSH to be configured on port 3000 rather than the default port 22, and you use a different identity key to differentiate your work and personal projects.

In this scenario, your SSH command would look like this:

ssh -i ~/.ssh/work.key -p 3000 gotoole@office.oneyearcodecamp.com

Remembering and correctly typing this command each time would be a nightmare, so instead, we should save the information in a config file:

Host office
	HostName office.oneyearcodecamp.com
	IdentityFile ~/.ssh/work.key
	Port 3000
	User gotoole

With this in place, we can run a more simple command:

ssh office

SSH Tunnels

We can set up configurations to speed up developer workflows more than just connecting to servers. For example, if we were running a system with a database on a cloud server and wanted to run a local desktop GUI client against the remote database, we can configure that.

With SSH, the command would be:

ssh -f -N -L 33060: dba@project.oneyearcodecamp.com

Let's explain this command:

  • -f means "put SSH in the background"
  • -N makes it not execute a command
  • -L forwards to local port 33060 to the remote port 3306

With this in place, we could run a database GUI against local port 33060, and the command would tunnel all traffic to port 3306 on the remote service. The local GUI application would magically work as if being run directly against the remote database.

Again, remembering these flags each time would be tricky, so we can use an SSH config file:

Host database
	HostName project.oneyearcodecamp.com
	User dba
	LocalForward 33060

With this in place, we need only run:

ssh -f -N database

And the tunnel will be in place for the GUI application to use.

Multiple Configs and Wildcards

You may be wondering how to combine configurations for multiple remote servers in a single config file. The answer is simple:

Host server1
	User root
Host server2
	User gotoole
Host server3
	User admin
Host *
	Port 3000

The tabs and the new line lines separate the unique hosts with different configuration values. Each of the servers server1, server2, and server3 have a different hostname and user configurations, but all use port 3000 for the SSH connection, so we use a wildcard host to set this option across all servers.

Usage with ssh-copy-id

We can use ssh-copy-id to copy the local key to the remote authorized_keys file. This allows for public key authentication rather than password-based authentication, enabling faster access to remote servers.

With the SSH config file above in place in ~/.ssh/config, we can use:

ssh-copy-id server1

Future invocations to SSH to server1 will now use the public key for authentication. Note that both ssh and ssh-copy-id use the SSH config file for hostname, port, user, and other information.

Usage with SCP

Similar to ssh-copy-id, we can take advantage of the SSH config file when using scp to copy files between servers. With the config file in place, we can run commands like:

scp server1:~/file.txt .

This command uses the config values for server1 to copy the remote file file.txt to the local working directory.


It should be clear that you can use the SSH config file to make your life easier when connecting and interacting with remote computers, and you can start doing this today.