0
0
Linux CLIscripting~5 mins

SSH config file in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Managing multiple SSH connections can be confusing and slow if you have to type long commands every time. The SSH config file lets you save connection details so you can connect quickly with simple commands.
When you connect regularly to several servers and want to avoid typing full addresses and options each time
When you want to use different usernames or keys for different servers without remembering all details
When you want to simplify SSH commands for easier and faster access
When you want to set default options like ports or forwarding for specific hosts
When you want to organize your SSH connections neatly in one place
Config File - config
config
Host myserver
  HostName 192.168.1.100
  User admin
  Port 2222
  IdentityFile ~/.ssh/id_rsa_myserver

Host github
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github
  IdentitiesOnly yes

Host: A nickname for the server you want to connect to.

HostName: The real address or IP of the server.

User: The username to log in as.

Port: The port number if not the default 22.

IdentityFile: The private key file to use for authentication.

IdentitiesOnly: Ensures only the specified key is used.

Commands
Connects to the server using the saved settings in the SSH config file under the nickname 'myserver'. This saves typing the full address, port, and user.
Terminal
ssh myserver
Expected OutputExpected
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.4.0-104-generic x86_64) admin@myserver:~$
Connects to GitHub using the nickname 'github' with the specified user and key from the config file.
Terminal
ssh github
Expected OutputExpected
PTY allocation request failed on channel 0 Hi username! You've successfully authenticated, but GitHub does not provide shell access. Connection to github.com closed.
Shows the contents of your SSH config file so you can review or edit your saved connection settings.
Terminal
cat ~/.ssh/config
Expected OutputExpected
Host myserver HostName 192.168.1.100 User admin Port 2222 IdentityFile ~/.ssh/id_rsa_myserver Host github HostName github.com User git IdentityFile ~/.ssh/id_rsa_github IdentitiesOnly yes
Key Concept

If you remember nothing else from this pattern, remember: the SSH config file lets you save and reuse connection details to connect faster and easier.

Common Mistakes
Not setting correct file permissions on the SSH config file
SSH ignores the config file if permissions are too open, causing connections to fail or ignore settings.
Set permissions with 'chmod 600 ~/.ssh/config' to keep the file private and usable.
Using incorrect indentation or syntax in the config file
SSH config requires indentation before options; wrong indentation causes errors or ignored settings.
Always indent options with spaces under each Host entry.
Forgetting to reload or re-run SSH after editing the config file
Changes won't apply until you start a new SSH session; old sessions keep using old settings.
Simply start a new SSH connection to apply changes; no special reload needed.
Summary
Create an SSH config file to save connection details like hostname, user, port, and keys.
Use simple nicknames to connect quickly without typing full commands.
Check and fix file permissions and syntax to ensure SSH uses your config correctly.