1. Introduction

Secure Shell (SSH) is a secure network protocol used for remote communication between a client and a server. It offers encrypted data exchange and serves as a secure alternative to traditional remote login methods. When establishing an SSH connection, it’s common to specify a username for authentication to the remote machine. However, there are situations where customizing the default username becomes necessary.

In this tutorial, we’ll explore methods to customize the default username for SSH connections. We’ll learn the basic usage of SSH and see how to explicitly specify a different username. Additionally, we’ll discuss using the SSH client config file to define host-specific configurations and default connection parameters. Furthermore, we’ll explore the use of aliases and enable the creation of shortcuts for SSH connections. Lastly, we’ll examine how environment variables can be used to set a default username for SSH connections.

By the end of this tutorial, we’ll have a comprehensive understanding of different techniques to personalize the default username for SSH connections.

2. Basic Usage

First, let’s establish a basic connection to a remote host:

$ ssh hostname

In this example, hostname represents the actual hostname or IP address of the remote server we want to connect to. When we execute this basic command, SSH attempts to connect to the remote host using our current username on the local machine. When we don’t specify an SSH port, the default is 22.

Let’s dive a bit deeper and get more details:

$ ssh -v 10.211.55.3
OpenSSH_8.9p1 Ubuntu-3ubuntu0.1, OpenSSL 3.0.2 15 Mar 2022
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to 10.211.55.3 [10.211.55.3] port 22.
debug1: Connection established.
...
debug1: Authenticating to 10.211.55.3:22 as 'amir'
...

We’re establishing an SSH connection to the IP address 10.211.55.3 while enabling verbose mode. By using the -v option, we can see detailed debugging information:

  • SSH client version
  • current client configuration file
  • at which point the connection is established
  • attempted authentication methods
  • username for the connection

Moreover, if the usernames on the local and remote machines differ, we need to explicitly provide the desired username during the SSH connection. To establish an SSH connection with a specific username, we can use @ between the username and the hostname:

$ ssh username@hostname

Alternatively, we can use the -l option:

$ ssh -l username hostname

By using these commands, we ensure that SSH uses the specified username for authentication when connecting to the remote host.

Both approaches achieve the same result of establishing an SSH connection to the specified hostname using the provided username. The choice between them depends mostly on personal choice.

3. Using the SSH Client Config File

The SSH client config file is a plain text file for configuring the SSH client behavior. It allows us to customize the behavior of SSH connections, define host-specific configurations, and specify default values for parameters. Moreover, we can leverage the SSH client config file to use a given default username in SSH for a specific host.

The SSH client config file is usually at ~/.ssh/config. However, we can use the system-wide configuration file /etc/ssh/ssh_config to apply settings to all users.

We can edit it using a text editor such as nano:

$ nano ~/.ssh/config

Here, we add blocks for the specific hosts we want to configure by specifying a name after the Host keyword:

$ cat ~/.ssh/config
Host host1
    HostName 10.211.55.3
    User user1

Host host2
    HostName 10.211.55.4
    User user2

In this example, host1 and host2 are the internal SSH client names we choose to identify the respective hosts. Everything after each Host line, before the end of the file and any next Host line belongs to the same block.

In this case, 10.211.55.3 and 10.211.55.4 are the IP addresses of the hosts. Similarly, user1 and user2 are the usernames we want to use by default when connecting to these hosts.

Now, let’s test ssh to host1:

$ ssh host1
[email protected]'s password:

Here, the prompt indicates that the SSH client needs the password for user1 on 10.211.55.3 as per our configuration.

Similarly, we can test ssh to host2:

$ ssh host2
[email protected]'s password:

Here, we can see the SSH command gets the username and IP corresponding to host2.

Of course, we can add more configurations to each block:

$ cat ~/.ssh/config
Host host1
    HostName 10.211.55.3
    User user1
    Port 2222
    IdentityFile ~/.ssh/id_rsa_host1

In this example, we added port 2222 instead of the default port 22. Moreover, we defined the path to the private key file by using IdentityFile. Each option is also available as a flag to the ssh command.

The SSH client prioritizes the configuration specified in the ~/.ssh/config file over any actual hostname, so Host host1 would take precedence over a machine with the name host1 on the network.

4. Using Aliases

An alias is a Bash built-in feature to create a custom shortcut for a command or series of commands. Aliases allow us to create names or modify existing ones to simplify complex or frequently performed operations.

Using an alias can help us set a different default username for SSH connections to specific hosts. Moreover, we can define a custom command that includes the desired username and hostname, eliminating the need to specify them every time we initiate an SSH connection manually.

To achieve this, we can directly use the alias command for the current session or add it to the shell’s configuration file:

  • ~/.bashrc
  • ~/.bash_profile

By setting an alias in shell configuration files, we preserve the settings across reboots.

The alias command specifies a name for the alias and associates it with the desired ssh command, including the different username and hostname.

First, let’s add our desired aliases to the shell configuration file:

$ nano ~/.bashrc
...
alias host1='ssh [email protected]'
alias host2='ssh [email protected]'

Then, we’ll save the file and either restart the terminal or run the command source ~/.bashrc to apply the changes.

Now, let’s verify our changes:

$ host1
[email protected]'s password:

As we can see, host1* is considered as *ssh [email protected].

Of course, we can also add more options:

$ cat ~/.bashrc
...
alias host1='ssh [email protected]:2222'

In this example, we create an alias named host1 that’s associated with the ssh command. When we run host1 in the terminal, it’ll automatically execute the ssh command with the specified arguments: [email protected]:2222. This command establishes an SSH connection to the host with the IP address 10.211.55.3 on port 2222, using the username user1. Hence, we can conveniently access the remote host by simply typing host1 instead of typing the entire ssh command each time.

5. Using Environment Variables

Environment variables are dynamic values that are part of the operating system context and can be accessed by various programs and processes. They provide a way to store and retrieve configuration settings, system paths, and other information.

Environment variables are a method to set a default username for SSH connections. By assigning a value to the SSH_DEFAULT_USER variable, we can override the default username used by SSH. Environment variables can be set in shell configuration files for persistent settings, but they can also be defined dynamically without modifying the configuration files.

Notably, this method applies the default username for all SSH connections and isn’t specific to a particular host.

So, we can set the variable in our shell configuration file like ~/.bashrc or ~/.bash_profile:

$ nano ~/.bashrc
...
export SSH_DEFAULT_USER=user1

Here, we used an assignment statement that sets the value of the SSH_DEFAULT_USER environment variable to user1.

Then, we can restart the terminal or use the source command to apply changes:

$ source ~/.bashrc

At this point, let’s test whether the variable has been assigned correctly:

$ echo $SSH_DEFAULT_USER
user1

As we can see, the output of the variable is user1.

Now, any SSH connection made without explicitly specifying a username will use user1:

$ echo $USER
user2
$ ssh 10.211.55.3
[email protected]'s password:

Here, the SSH connection uses the updated default username specified in the SSH_DEFAULT_USER environment variable instead of the current $USER.

6. Conclusion

In conclusion, customizing the default username for SSH connections offers flexibility and convenience when establishing secure remote connections.

In this article, we explored various techniques, including explicitly specifying the username during connection, utilizing the SSH client config file for host-specific configurations, creating aliases for streamlined connections, and setting default usernames through environment variables.