How To Change a ‘Hostname’ in Linux
In this article we are talking about the Hostname of user in your system. You have probably needed to change the hostname of your Linux system after installing to personalize your system. Changing the hostname is not the big task but is more simpler and important for every pro and basic user.
So we are practice some commands about changing the hostname in linux
1. Check Hostname
To check the hostname open your terminal and type the following command:
By default the hostname is ‘localhost.localdomain’. To change this you can follow the following steps.
2. Edit /etc/hostname File
The easiest way to change the hostname is to edit the /etc/hostname file. Open your terminal and with your favourite text editor delete ‘localhost.localdomain’ and change to whatever name you like
For doing this task the user should be a superuser or use sudo.
# vim /etc/hostname
After this command one window of Vi editor is open in this window we are edit the default name ‘localhost.localdomain’ to new name ‘userone’. After making that changes we checking this by command ‘hostname’
# hostname userone
3. Using Hostnamectl
Hostnamectl is a tool that is used control the Linux system hostname. You can also use this tool to change the hostname in a few easy steps.
First and foremost confirm the hostname by using hostnamectl as follows:
# hostnamectl status
To change the hostname type the command:
# hostnamectl set-hostname blue # hostname blue
It may be necessary to restart the systemd-hostnamed daemon so that as to reflect the change in Static hostname
# systemctl restart systemd-hostnamed
4. Changing username by Network manager by Command ‘nmtui’ :
To change the hostname you can also use the NetworkManager text interface tool (nmtui). This is also another very easy method and fairly straight forward.
From the command line invoke nmtui:
This will present to you a text user interface like this. Using the arrow keys select Set system hostname and use tab to select OK.
Edit the hostname which by default is ‘localhost.localdomain’ and change it to whatever you prefer.
After changing the hostname, choose okay, then the following confirmation message will appear. If the hostname is as you desire you can choose OK. You can confirm changes using the hostname command.
5. Using Nmcli :
Nmcli is a command line tool for controlling the NetworkManager and can also be used to change the hostname.
To check the hostname with nmcli, type in your terminal:
$ nmcli general hostname
This will print out the hostname to your terminal. By default in CentOS 7 it should be localhost.localdomain.
To change this use the command:
$ nmcli general hostname grace
This will request for your password via a GUI interface. If you are running this command remotely remember to use root or sudo:
$ sudo nmcli general hostname grace
You can then check the hostname with the command hostname or nmcli general hostname.