Manage SSH connections with a SSH config file
Using an SSH config file is another way that UNIX can simplify your workflow.
SSH Connections ¶
If you are making anything on the web before long you will find yourself using
SSH
. SSH allows you to connect to and work on remote servers.
ssh -p 8675 hercules@foo.com
Using a SSH Config File ¶
If you find yourself frequently running SSH commands you may get the urge to
create an alias for the command. In fact there is a better way to manage
SSH options using an SSH config file. This file can be located in your home
directory at ~/.ssh/config
or be made available system wide at
/etc/ssh/ssh_config
. If the file does not exist you can create it and set the
correct permissions.
touch ~/.ssh/config
chmod 644 ~/.ssh/config.
For the simple example above we can create an entry in this file.
Host foo.com
HostName foo.com
Port 8675
User hercules
This allows the following to be run and for the options to be automatically set.
ssh foo.com
Setting an identity key ¶
If you are using shared keys to login to SSH without having to use a password you can declare the key in your SSH config. This can be useful if you are connecting to different servers that have different shared keys.
Host foobar.com
User horatio
HostName foobar.com
IdentityFile ~/.ssh/foobar.key
Setting defaults ¶
You can set defaults for all SSH connections by declaring an entry at the bottom of your config file.
Host *
ForwardAgent no
ForwardX11 no
ForwardX11Trusted yes
User shapeshed
Port 22
Protocol 2
Debugging ¶
To debug your setup run SSH in verbose mode to see the settings that are applied
ssh -v foobar.com
This will show which configuration settings are applied.
ssh -v foobar.com
OpenSSH_6.9p1, LibreSSL 2.1.8
debug1: Reading configuration data /Users/shapeshed/.ssh/config
debug1: /Users/shapeshed/.ssh/config line 5: Applying options for foobar.com
debug1: /Users/shapeshed/.ssh/config line 9: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 20: Applying options for *
debug1: /etc/ssh/ssh_config line 102: Applying options for *
debug1: Connecting to foobar.com [123.456.789.000] port 8675.
Security ¶
Personally I do not store my SSH config file in source control or in my dotfiles. Effectively an SSH config file declares route into servers and how to get into them so I recommend not making this information public.
Conclusion ¶
Using an SSH config file is another way that using UNIX can simplify your workflow. For more I recommend reading the man page.
Further Reading ¶
Tags
Can you help make this article better? You can edit it here and send me a pull request.
See Also
-
Linux and Unix find command tutorial with examples
Tutorial on using find, a UNIX and Linux command for walking a file hierarchy. Examples of finding a file by name, finding and deleting a file, finding a directory and searching by modification time and permissions. -
Making the case for make
Make gives you access to many tiny, powerful UNIX tools and shouldn't be overlooked as a build tool. -
Linux and Unix sort command tutorial with examples
Tutorial on using sort, a UNIX and Linux command for sorting lines of text files. Examples of alphabetical sorting, reverse order sorting, sorting by number and mixed case sorting.