Change Hostname for Linux Distros Without Restart Your Machine

28/12/2020
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

How and Why to Change Default SSH Port

Secure Shell, also known as SSH is a network protocol which can be used to access the server remotely. Communication taking...
29/12/2020

Install NextCloud on Ubuntu

NextCloud is a free self-hosted file sharing software. It can be accessed from the web browser. Next cloud has apps for...
29/12/2020

Types of Shells in Linux

Overview In this lesson, we will study the types of shells available in Linux and what advantages one shell offers over...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch Vụ Xây Dựng Hệ Thống Peering Với Internet Exchange (IXP)
04/04/2025

Dịch Vụ Triển Khai VPN Site-to-Site & Remote Access
04/04/2025

Dịch Vụ Thiết Lập Hệ Thống Tường Lửa (Firewall)
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ảo Hóa & Cloud
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ceph
04/04/2025