RHEL / Centos Linux 7: Change and Set Hostname Command

I‘m a new RHEL (Red Hat Linux) and/or CentOS Linux 7 server user. How can I change the hostname in CentOS 7 using a command line option?

On a CentOS Linux 7 server you can use any one of the following tool to manage hostnames:[donotprint]
Tutorial details
Difficulty Easy (rss)
Root privileges Yes
Requirements None
Time 2m
[/donotprint]

ADVERTISEMENTS

  1. hostnamectl command : Control the system hostname. This is recommended method.
  2. nmtui command : Control the system hostname using text user interface (TUI).
  3. nmcli command : Control the system hostname using CLI part of NetworkManager.

Types of hostnames

The hostname can be configured as follows

  1. Static host name assigned by sysadmin. For example, “server1”, “wwwbox2”, or “server42.cyberciti.biz”.
  2. Transient/dynamic host name assigned by DHCP or mDNS server at run time.
  3. Pretty host name assigned by sysadmin/end-users and it is a free-form UTF8 host name for presentation to the user. For example, “Vivek’s netbook”.

Method #1: hostnamectl command

Let us see how to use the hostnamectl command.

How do I see the host names?

$ hostnamectl
## OR ##
$ hostnamectl status

Sample outputs:

   Static hostname: centos-7-rc
         Icon name: computer
           Chassis: n/a
        Machine ID: b5470b10ccfd49ed8e4a3b0e953a53c3
           Boot ID: f79de79e2dac4670bddfe528e826b61f
    Virtualization: oracle
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-229.1.2.el7.x86_64
      Architecture: x86_64

How do I set the host name?

The syntax is:
# hostnamectl set-hostname Your-New-Host-Name-Here
# hostnamectl set-hostname "Your New Host Name Here" --pretty
# hostnamectl set-hostname Your-New-Host-Name-Here --static
# hostnamectl set-hostname Your-New-Host-Name-Here --transient

To set host name to “R2-D2”, enter:
# hostnamectl set-hostname R2-D2
To set static host name to “server1.cyberciti.biz”, enter:
# hostnamectl set-hostname server1.cyberciti.biz --static
To set pretty host name to “Senator Padme Amidala’s Laptop”, enter:
# hostnamectl set-hostname "Senator Padme Amidala's Laptop" --pretty
To verify new settings, enter:
# hostnamectl status
Sample outputs:

   Static hostname: server1.cyberciti.biz
   Pretty hostname: Senator Padmé Amidala's Laptop
Transient hostname: r2-d2
         Icon name: computer
           Chassis: n/a
        Machine ID: b5470b10ccfd49ed8e4a3b0e953a53c3
           Boot ID: f79de79e2dac4670bddfe528e826b61f
    Virtualization: oracle
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-229.1.2.el7.x86_64
      Architecture: x86_64

How do I delete a particular host name?

The syntax is:
# hostnamectl set-hostname ""
# hostnamectl set-hostname "" --static
# hostnamectl set-hostname "" --pretty

How do I change host name remotely?

Use any one of the following syntax:
# ssh root@server-ip-here hostnamectl set-hostname server1
OR set server1 as host name on a remote server called 192.168.1.42 using ssh:
# hostnamectl set-hostname server1 -H root@192.168.1.42

Method #2: nmtui command

You can set host name using nmtui command which has text user interface for new users:
# nmtui
Sample outputs:

Fig.01: Use nmtui to set hostname on a CentOS 7

Fig.01: Use nmtui to set hostname on a CentOS 7

Use the Down arrow key > select the “Set system hostname” menu option > Press the “Ok” button:
Fig.02: Set hostname

Fig.02: Set hostname

You will see the confirmation box as follows:
Fig.03: New hostname confirmed

Fig.03: New hostname confirmed

Finally, restart hostnamed service by typing the following command
# systemctl restart systemd-hostnamed
To verify changes, enter:
# hostnamectl status
Sample outputs:
Fig.04: CentOS 7 / RHEL View Host Name Details Using hostnamectl Command

Fig.04: CentOS 7 / RHEL View Host Name Details Using hostnamectl Command

Method #3: nmcli command

The nmcli is a command line tool for controlling NetworkManager and reporting network status.

To view the host name using nmcli command:

The syntax is:
# nmcli general hostname

To set the host name using nmcli command:

The syntax is:
# nmcli general hostname R2-D2
# nmcli general hostname server42.cyberciti.biz

Finally, restart the systemd-hostnamed service:
# systemctl restart systemd-hostnamed

This entry is 5 of 19 in the Linux/Unix Set hostname Tutorial series. Keep reading the rest of the series:
  1. Ubuntu Linux Change Hostname (computer name)
  2. Debian Linux: Change Hostname / Computer Name Permanently
  3. Linux change my hostname / computer system name
  4. Linux Change Hostname
  5. RHEL / Centos Linux 7: Change and Set Hostname Command
  6. OpenBSD Change Hostname
  7. CentOS Linux 5/6 Change Hostname Command
  8. Linux setting hostname and domain name of my server
  9. FreeBSD Change Hostname without reboot
  10. SuSe Linux Change Hostname without reboot
  11. How to set the hostname on Fedora Linux
  12. Ubuntu 18.04 LTS change hostname permanently
  13. Ubuntu set hostname permanently (computer name) command
  14. OpenSUSE Linux set hostname permanently (computer name) command
  15. RHEL 8 Linux set hostname permanently (computer name) command
  16. CentOS 8 Linux set hostname permanently (computer name) command
  17. Ubuntu 20.04 LTS set hostname permanently (computer name) command
  18. Set / Change FQDN on Ubuntu 20.04 LTS
  19. Alpine Linux - Setting system hostname
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
12 comments… add one
  • oxtan May 6, 2015 @ 16:31

    or just vi /etc/hostname, edit the file with the new name, logout, log in, the new host name is there.

    • bobv Apr 14, 2016 @ 9:04

      this is the quickest way. thanx!

  • John Sep 29, 2015 @ 2:21

    Thanks for your time and effort this help me sort things out in my head..
    menewbie

  • Vytautas Dec 21, 2015 @ 9:42

    Please check this line.
    # ostnamectl set-hostname “Senator Padmé Amidala’s Laptop” –pretty
    Where is “h” letter?

    • nevillescollop Feb 17, 2016 @ 11:24

      Wer lesen kann, ist stark im Vorteil!

  • Jacques Dec 27, 2016 @ 7:26

    thanks a mil. exactly what i needed! :-)

  • R. Hartman Feb 13, 2017 @ 20:51

    Please check this line.
    # hostnamectl set-hostname Senator Padmé Amidalas Laptop pretty

    It would appear you used an ISO 8859-1 code page to save that line, causing the e-accent () to be mangled to A-tilde+CopyRight (é) when displaying it in UTF-8 context.
    One way to fix this is to use sed to replace the é with as vi will notice the encoding and convert it, showing it correctly.

    Hope the diacritical characters show properly in this comment; code page issues are nasty.

  • R. Hartman Feb 14, 2017 @ 18:14

    No problem. Not sure what you did, looks like you just got rid of the accent, but I now see this, not sure if that? new or I missed it yesterday:

    Method #3: nmcli command
    The nmcli is a command‐line tool

    It’s even worse after copy/paste, so you definitely (still?) have a codepage issue.

    Cheers.

    • 🐧 Vivek Gite Feb 14, 2017 @ 18:25

      It was there y’day too. I just fixed it. I am now looking into why I am getting such problems.

  • R. Hartman Feb 14, 2017 @ 18:20

    Hmmm… saving it makes it look the same. Before posting there was a third control-like character (square block with 0s and 1s in there) behind the sign. As stated, these are nasty issues. I’m actually dealing with some of this professionally at the moment.

    Is your database codepage set to UTF-8?

    I’m using Firefox to visit this site, set to UTF-8. If I change my codepage to ‘Western’ or ISO 8859-1, it actually becomes worse.
    Hope this helps.

  • Naveen Sep 17, 2017 @ 8:43

    Nicely Explained!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.