SSH connection parameters are a long list, and it is troublesome to enter them every time, not to mention multiple servers. Sometimes it is just a different port, different username, and different key path, and you can enter them incorrectly if you are not careful. For developers, SSH is the main way to remotely operate Linux. But once it involves multiple machines, different identities, and different ports, management is prone to errors. So how to correctly set up and use the local SSH configuration file to make connecting to the server simple and efficient. Just set up .ssh/config.
Common pain points of using SSH configuration files include: remembering the IP and port of each server; using multiple sets of keys together, often making mistakes; typing long commands every time, inefficient; temporary switching environments are easy to confuse (development/testing/production);
Using the .ssh/config configuration file can solve these problems, so that you no longer need to remember the IP, set the default username, port, and key, manage by environment or business organizational structure, and quickly log in with one click;
Where is the SSH configuration file? How to write it?
SSH configuration file path: ~/.ssh/config
If you don’t have it, you can create it manually:
touch ~/.ssh/config
chmod 600 ~/.ssh/config
The permissions must be set to 600. Otherwise SSH will refuse to read it.
A simple example:
Host web1
HostName 192.168.1.205
User root
Port 2222
IdentityFile ~/.ssh/id_rsa
Once configured, connecting is as simple as:
ssh web1
Isn't it much simpler?
Advanced usage: multiple machines, multiple users, automatic jump server
Multiple machine configuration:
Multiple servers can be configured:
Host dev
HostName 10.0.0.10
User ubuntu
IdentityFile ~/.ssh/dev.pem
Host test
HostName test.server.com
User ec2-user
Port 2200
IdentityFile ~/.ssh/test_key.pem
Then directly:
ssh dev ssh test
One command connects different environments, very clear.
Multiple users connect to the same IP
Sometimes you need to log in to the same server with different identities:
Host dev-admin
HostName 192.168.1.105
User admin
IdentityFile ~/.ssh/admin_key
Host dev-readonly
HostName 192.168.1.105
User readonly
IdentityFile ~/.ssh/readonly_key
They are all one host and one identity, and they do not interfere with each other.
Configure a jump server
If you need to connect to the target server through a jump server (bastion server):
Host jump
HostName jump.example.com
User jumpuser
IdentityFile ~/.ssh/jump_key
Host db
HostName 10.0.1.20
User root
IdentityFile ~/.ssh/db_key
ProxyJump jump
In this way, you can directly ssh db, and SSH will automatically connect to the springboard first and then jump over. It is safe and convenient.
In daily development, operation and maintenance, deployment, and remote collaboration, SSH is an unavoidable tool. But the efficiency varies greatly depending on how well it is used. You might as well spend ten minutes to write all your frequently used servers into .ssh/config. From now on, you no longer have to copy IPs all over the screen, find pem files, and look through documents to find port numbers - one command goes directly to the target server.