Change Hostname for Linux Distros Without Restart Your Machine

Chưa phân loại

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

Please feel free to comment if you run into any issues or have a better setup so we share and update our post. Thanks…

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Best MySQL Clients for Ubuntu

MySQL is very popular and one of the most widely used relational database management systems. Written in C and C++, it...

Install SuperTuxKart in Ubuntu 18.04

SuperTuxKart is a 3D arcade racing game designed for Linux based operating system. It is a multiplatform game and comes...

Linux chmod Example

In this quick tutorial, we will see how we can use chmod command in an Ubuntu machine to find, modify and remove user permissions...