In computing, the hostname concept is defined as the hostname is a human-readable name of any system. And, the username is the name of a user. You can add multiple usernames for multiple users in the same system. In Linux, the hostname and the username of a computer are identical values that can be used to recognize your machine in a worldwide network or a local area network.
Most users use the same name for both the username and hostname in Linux. It usually doesn’t create any problem if you keep the same username and hostname. Still, it may create some mismatches or confusion if you connect your Linux machine into a network. Therefore you need to specify your hostname and username. To resolve this issue, you can change the hostname and username of your Linux machine.
Linux Hostname and Username
When you make a clean Linux installation on your machine, it asks for a hostname and a username to continue the installation process. You can type any name as your username and hostname. You can use both capital letters in your hostname, but you cant use any capital letters in your username.
Linux always allows its users to use a small letter username. Hence, it is possible to change, hide, or replace your Linux system’s hostname and username. In this post, we will see how to change the username and hostname on your Linux system.
Step 1: View Your Current Username and Hostname
You can see your current hostname and username after turning on your Linux system. There are several options to check the username and hostname; I am going to demonstrate a few of them. You can check the PC name from your system details option, but remember the PC name and username are not the same very often. You can change the PC name effortlessly very now and then.Â
You can view the user ID and Group ID of your Linux system by performing the following terminal command in your shell. You can also see all the user’s name by performing the user’s command in your terminal shell.
$ idÂ
$ users
$ whoami
By installing the finger tool on your Linux distribution, you can check the host directory, username, and shell location of your machine. To install the finger tool on your Ubuntu system, you can use the following apt
command provided below.
$ sudo apt install finger
Now run the following finger
command to see the user and host directory details of your Linux distribution.
$ finger ubuntupit
You can check the current hostname of your Ubuntu system from the terminal shell. You just need to type the hostname and hit enter.Â
You can use the following terminal commands in your Linux to see your hostname.
$ hostname
$ /etc/hostnameÂ
$ /etc/hosts
Step 2: Change Username on Linux
Here, we will see how you can change the username of your Linux machine by using the terminal command shell. We can use the usermod command to make the changes. The usermod is mainly used to modify, edit, remove, and replace the user pieces of information for a Linux service. First, we need to change the username by replacing it with a new one.
$ usermod -l newUsername oldUsername
Now remove the old username from your filesystem. Normally the username is associated with the Home
directory of your Linux system. So, now we will remove and replace the username to the Home
directory. You can use the following usermod commands to remove and replace the username.
$ mv /home/oldUsername* /home/newUsername* $ usermod -d /home/newUsername* -m newUsername
Besides these, you can also change your PC name from the general setting menu of your Linux system.
Step 3: Edit and Change Hostname on Linux
To change the hostname of your Linux system, you can use the hostname control (hostnamectl) command in your terminal shell. You need to write your current hostname after the hostnamectl
syntax, then you have to write the new hostname and hit the enter button.
$ sudo hostnamectl set-hostname Linux-Ubuntu
You can also change the hostname of your Linux system using the Nano script editor. In this method, you need to run the following terminal command-line to open and edit the hostname using the Nano editor.
$ sudo nano /etc/hostname
Step 4: Hide Hostname From Your Linux Shell
Normally the username and hostname of your Linux system appear in the terminal shell as username@hostname. In some cases showing the hostname might be a security compromise. In that case, you can hide the hostname of your machine. You can hide your Linux hostname from the terminal shell by editing the ~/.bashrc
file. Now, we will see how you can edit and hide the hostname from the Ubuntu terminal shell. Use the following gedit
command to open the Bash script.
$ gedit ~/.bashrc
After the .bashrc
file is opened, now you need to find the following line from the script. Here you will see the hostname and username are mentioned as \u@\h\
. All you need to do is just remove the @\h\
from the script to hide the hostname. I am writing the modified line below for a better understanding. You can copy and paste the line inside your script.
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
 Replace the above line by the following line, given below.
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
After you make this edit inside the .bashrc
file, you will be able to see the change in the terminal shell. You will see that the hostname is no longer visible on your Ubuntu terminal.
If you do any mismatch with your default .bashrc
script, here you can find the default .bashrc
script file. You just need to copy and paste the raw codes into your editor and save it.
Extra Tip: Set A Device Name While Sharing Files
In some cases of networking, the hostname and the device name is the same for Linux. Think about your Linux device’s simple sharing system; while you want to make your device discoverable on your local area network, you need to assign a name to your device. In that case, the discoverable device name is acting as a hostname. So, don’t forget to assign your device a name while you share files or documents from your Linux device through a network.
Ending Words
If you are a network and server admin, please be careful while installing an apache server on your Linux system. Sometimes, the device hostname and the apache server hostname could be mismatched. Make sure you install applications in different directories that require a hostname.
In the entire post, I have described the methods of how you can change the hostname and username in your Linux system. I have also mentioned the method of hiding the hostname from the Linux terminal shell.
I hope you find this post useful and informative. Please share this post with your friends and the Linux community. You can ask any question related to this post in the comment segment.