How to change hostname in Fedora Linux permanently

How do I change the hostname of a Fedora Linux system permanently using the command line tools from localhost to fedora28-nixcraft? How do I change hostname in Fedora Linux permanently from localhost.localdomain to fedora28-nixcraft?

You can use the hostnamectl command to display or set the Fedora Linux system’s host name. In this tutorial, you will learn how to change your hostname on Fedora Linux using various methods.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Fedora Linux
Est. reading time 3 minutes

What is hostname?

A hostname is nothing but your computer or server name. One can use the hostname to identify and define itself on a network. For example, hostname www13 indicates a web server. Sometimes you setup hostname as a fully qualified domain name (FQDN) such as www13.rack01.nixcraft.com or simply www.nixcraft.com.

Rules for naming hostname on the Fedora Linux

Rules are pretty simple. A hostname on Fedora Linux may only contain letters a-z, numerals 0-9, and dashes (-). An example is www-13. The FQDN for that server might be www-13.nixcraft.com.

How To change hostname in Fedora Linux server

Steps to change your hostname on Fedora Linux:

  1. Login to your server: ssh user@server-name
  2. Become a root user: sudo -s or su -
  3. Run command: hostnamectl set-hostname new-name

Let us see steps in details to change a system’s hostname i.e. rename a computer/server name when using a Fedora Linux based system.

Find your current computer name

Type the following hostname command or hostnamectl command:
$ hostname
Sample outputs:

localhost.localdomain

$ hostnamectl
Sample outputs:

   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: c86ab4f4c14c435baff0597747b349d9
           Boot ID: 2145ab2be9de49b6bca1837bb8d5a2f8
    Virtualization: kvm
  Operating System: Fedora 28 (Server Edition)
       CPE OS Name: cpe:/o:fedoraproject:fedora:28
            Kernel: Linux 4.16.3-301.fc28.x86_64

Fedora change hostname

Let us change hostname in Fedora Linux. For example, change hostname to fedora28-nixcraft, enter:
$ sudo hostname fedora28-nixcraft
$ hostname

Sample outputs:

fedora28-nixcraft

As compared to hostnamectl command, hostname command just read or set the hostname for the current session. It only exists for compatibility reasons. Hence, I recommend using the hostnamectl command as discussed below.

How do I change Hostname Permanently on the Fedora Linux?

Edit the file /etc/hostname, enter:
$ sudo vi /etc/hostname
Delete the existing name and replace it with a new name:

fedora28-nixcraft

Save and close the file. You may also need to edit the /etc/hosts file, enter:
$ sudo vi /etc/hosts
Find all references to server1 and replace with server2:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.122.49	fedora28-nixcraft

Save and close the file. Please note that the host name is usually set once at system startup. This is an older method but still works on all versions of Fedora Linux.

How to set the hostname on Fedora Linux

With the latest version of Fedora Linux that uses systemd, it is possible to use a single command as follows:
# hostnamectl set-hostname {NameHere}
# hostnamectl set-hostname fedora28-nixcraft

Sample outputs:
Command to change hostname in Fedora Linux permanently

This entry is 11 of 21 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
  20. Arch Linux - Setting system hostname
  21. Amazon Linux 2 - Setting system hostname

Did you find this article useful?