Using .ssh/config
simplifies SSH connections, eliminating the need to remember and type lengthy commands for each server. By configuring host aliases, specifying user names, defining ports, and managing SSH keys, access to multiple servers becomes more convenient and efficient.
Creating the Config File #
If SSH key authentication is already set up, the ~/.ssh
directory should exist. Otherwise, create it as follows.
Verify if the ~/.ssh directory exists #
ls -la
If ~/.ssh
does not exist, create it and set the appropriate permissions:
mkdir ~/.ssh
chmod 700 ~/.ssh
Create the ~/.ssh/config file #
touch ~/.ssh/config
Editing ~/.ssh/config #
Next, add server connection details to ~/.ssh/config
.
A typical configuration might look like this:
# Server A
Host serverA
Hostname example.com
Port 22
User tanaka
IdentityFile ~/.ssh/serverA.key
# Server B
Host serverB
Hostname 192.168.1.10
Port 50134
User yamada
IdentityFile ~/.ssh/serverB.key
# Server C
Host serverC
Hostname test-server.com
Port 48912
User sato
Understanding Key Parameters #
Parameter | Description |
---|---|
Host |
Alias for the server (used in SSH commands) |
HostName |
Server domain name or IP address |
Port |
SSH port (default: 22) |
User |
SSH login username |
IdentityFile |
Path to the private key (for key authentication) |
Additional parameters can be configured as needed, but these are the essential ones.
Connecting to Servers #
Once the ~/.ssh/config
file is set up, connecting to a server is as simple as using the alias defined in the Host
field.
To connect to Server A:
ssh serverA
To connect to Server B:
ssh serverB
To connect to Server C:
ssh serverC