How to SSH with a Different Username on Ubuntu
When connecting to an Ubuntu Linux machine via SSH, the client automatically attempts to log in using your current local system’s username. However, if your remote account name differs, you must explicitly specify the correct username to establish a successful connection. This article explains the standard terminal commands, flag options, and configuration file shortcuts to connect to your Ubuntu server using a different username.
Method 1: Specify Username in the SSH Connection Command
The most common and straightforward way to connect with a different username is to define it directly within your SSH command. You can do this using two different syntax options.
Option A: Using the
username@ Syntax
This is the standard and most widely used format. You simply prefix
the target IP address or domain name with your desired username,
separated by an @ symbol.
ssh username@remote_host_or_ipFor example, to connect to an Ubuntu server at
192.168.1.50 as the user ubuntu:
ssh ubuntu@192.168.1.50Option B: Using the
-l (Login Name) Flag
Alternatively, you can use the -l flag followed by the
username you wish to log in as. This is useful if you prefer to keep the
hostname separate.
ssh -l username remote_host_or_ipUsing the same example as above:
ssh -l ubuntu 192.168.1.50Method 2: Configure the SSH Config File for Automatic Login
If you frequently connect to a specific Ubuntu machine, typing the username every time can become tedious. You can automate this process by editing your local SSH configuration file.
Open (or create) the SSH config file on your local machine using a text editor:
nano ~/.ssh/configAdd the following block of code, replacing the placeholders with your actual server details:
Host myubuntu HostName remote_host_or_ip User usernameSave and close the file (in Nano, press
Ctrl+O,Enter, thenCtrl+X).Now, you can connect to your Ubuntu server using the nickname you defined, without needing to specify the IP address or username:
ssh myubuntu
Method 3: Specifying a Different Username with an SSH Key
If your Ubuntu server requires an SSH key for authentication, you can
combine the username specification with the identity file
(-i) flag:
ssh -i /path/to/private_key username@remote_host_or_ipFor example:
ssh -i ~/.ssh/id_rsa ubuntu@192.168.1.50