hostname – get or set hostname

hostname command is the most basic way of confirming your current server’s hostname. executed without any command line parameters, it will most likely show you a short host name (the one without domain name).

Basic hostname command usage

Here’s what happens if I run hostname in my RHEL8 virtual machine:

[greys@rhel8 ~]$ hostname
rhel8

Temporarily set host name with hostname

You can also use hostname command to temporarily change the host name for the server you’re on. You must be root in order to do that:

[greys@rhel8 ~]$ hostname redhat8
hostname: you must be root to change the host name

Like I said, you need to be root or use sudo:

[greys@rhel8 ~]$ sudo hostname redhat8
[sudo] password for greys:

… now that’s the hostname is changed you can use hostname command again, without any parameters, to check the new host name:

[greys@rhel8 ~]$ hostname
redhat8

IMPORTANT: this change will only work until reboot. You need additional steps make changes permanent.