Set Default SSH User for Specific Host in Ubuntu
This article explains how to configure a default username for specific SSH connections in Ubuntu Linux by modifying your local SSH client configuration file. By setting up this configuration, you can connect to your remote servers using a simple alias without having to manually type your remote username or the full IP address every time.
To set a default user for a specific SSH connection, you need to edit or create the SSH config file in your user directory. Follow these steps to set it up:
Step 1: Open the SSH Config File
Open your terminal and open the SSH configuration file using a text
editor like Nano. The file is located at ~/.ssh/config:
nano ~/.ssh/configIf the file or the ~/.ssh directory does not exist, this
command will create the file once you save it.
Step 2: Add the Connection Details
Add a configuration block for your remote server. You need to define a nickname for the host, the actual IP address or domain name, and the default username.
Use the following format:
Host my-server-alias
HostName example.com
User your_remote_username
Replace the values as follows: * Host: The alias you
want to type to initiate the connection (e.g., production,
backup-server). * HostName: The actual
domain name or IP address of the remote server. * User:
The default username you want to use for this connection.
Step 3: Save and Exit
If you are using Nano, save the changes by pressing
Ctrl + O, hit Enter to confirm, and then exit
the editor by pressing Ctrl + X.
Step 4: Set the Correct Permissions
For security reasons, your SSH configuration file must have strict permissions so that other local users cannot read it. Run the following command to set the correct permissions:
chmod 600 ~/.ssh/configStep 5: Connect to Your Server
Now you can connect to your remote server using only the alias you defined. You no longer need to type the username or the host’s IP address:
ssh my-server-aliasThe SSH client will automatically read your configuration file, map
my-server-alias to the correct IP address, and log you in
using the specified default username.