If you have ever wanted to find out how you can change hostname of your linux machine, then this tut is for you. So in this tutorial I will show you how you can rename / change your Linux machine hostname without having to reboot for it to take effect. The method that will be shown here will work for the following Linux distros “Ubuntu 16.10, Ubuntu 16.04, Ubuntu 14.04, Linux Mint, CentOS 7, Fedora 21, RHEL 7.
Ubuntu 16.10, Ubuntu 16.04, Ubuntu 14.04, Linux Mint
Change Hostname Temporary
- To change hostname temporary, run following command. Note this would only apply while machine is still on, but when you reboot your machine, hostname will revert back to preview name
sudo hostname NEW_HOSTNAME_HERE
Change Hostname Permanently
- Edit the following files “/etc/hosts” & “/etc/hostname“. First of all, lets make sure “gksu” is installed on your machine by running following command
sudo apt-get install gksu
- Edit both files with following command
gksudo gedit /etc/hosts /etc/hostname
- In the “hosts” file, replace the existing hostname with the new one. Same goes for the second file “hostname“. Note both names MUST be exact same. Save both changes and close files
- Since this tut is about making this changes without restarting the machine, so for the name to take effect, run the following command
systemctl restart systemd-logind.service
CentOS 7, RHEL 7, Fedora 21 and above
There is more than one method of changing the hostname of CentOS, Fedora and RHEL machines but for this tut, we will use a tool called “hostnamectl”. hostnamectl is a command tool that can be used to all 3 class of host name “pretty”, “transcient” and “static” hostnames.
What is Pretty, Transient and Static Hostnames
- Pretty hostname is a high-level hostname which can include special characters (e.g. Linuxhint’s Laptop)
- Transient hostnames are once obtained by network configuration (e.g. DHCP)
- While Static is the conventional hostname most server admins are used to. It is used to initialize the kernel hostname at boot
Using hostnamectl
- To replace all hostname at once, run the following command. Note replace linuxhintOS with your new hostname
hostnamectl set-hostname linuxhintOS
- To each hostname class, run the following command.
hostnamectl set-hostname linuxhintOS --<option>
where <option> can be replaced with either “pretty”, or “transcient” or “Static”, as shown below
hostnamectl set-hostname linuxhintOS --pretty
- Next relaunch the terminal session (Putty or console) and check the hostname
hostnamectl status