Viewing and setting the host name on a Linux or OS X system

To view the host name for a Linux or OS X system you are logged into, you can use the uname command with the -n option. On a Linux system, you can also use --nodename in lieu of -n, but --nodename won't work on OS X. If the host name has not been set, you will see localhost.localdomain displayed.
$ uname --nodename
localhost.localdomain

You can also use the hostname command on a Linux or OS X system to show or set the host name. To show the host name, issue the command without any parameters

$ hostname
localhost.localdomain

On some versions of Linux, e.g., CentOS and Ubuntu, you can also view the host name using the sysctl command; it won't work on OS X, however.

To set the host name, issue the command hostname new_name from the root account or using sudo where new_name is the name you wish to give the system. E.g.:

[root@localhost ~]# hostname
localhost.localdomain
[root@localhost ~]# hostname moonpoint
[root@localhost ~]# hostname
moonpoint
[root@localhost ~]# uname -n
moonpoint
[root@localhost ~]# sysctl kernel.hostname
kernel.hostname = moonpoint
[root@localhost ~]#

The sysctl command can also be used to set the host name on systems where sysctl kernel.hostname works using sysctl kernel.hostname=new_name.

However, you will only be changing the "transient" host name by that method. If you reboot, the commands will return the value they returned before. If you issue the hostnamectl command with the status argument, it will show you whether you have a permanent or transient change to the host name.

# hostnamectl status
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 72863e389b584a4dab36fae7f3bffda2
           Boot ID: b9c1755c17d24493a177efc679fc9774
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 4.4.0-x86_64-linode63
      Architecture: x86_64
[root@localhost ~]# hostname moonpoint
[root@localhost ~]# hostnamectl status
   Static hostname: localhost.localdomain
Transient hostname: moonpoint
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 72863e389b584a4dab36fae7f3bffda2
           Boot ID: b9c1755c17d24493a177efc679fc9774
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 4.4.0-x86_64-linode63
      Architecture: x86_64
[root@localhost ~]#

In the above example, you can see that after I issued the command hostname moonpoint to change the host name to moonpoint, hostnamectl status is now reporting "Transient hostname: moonpoint", whereas previously it displayed "Static hostname: localhost.localdomain". I can use the hostnamectl set-hostname command to make the change permanent, so that it persists across a reboot. E.g., as shown below, when I issue the command hostnamectl set-hostname moonpoint, I see that the hostnamectl status command shows the new name as "Static hostname".

[root@localhost ~]# hostnamectl set-hostname moonpoint
[root@localhost ~]# hostnamectl status
   Static hostname: moonpoint
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 72863e389b584a4dab36fae7f3bffda2
           Boot ID: b9c1755c17d24493a177efc679fc9774
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 4.4.0-x86_64-linode63
      Architecture: x86_64
[root@localhost ~]#

And I see the new value in /etc/hostname, where the host name is stored on CentOS and Ubuntu Linux systems, but not on OS X systems, has been changed. If I just update the name with hostname new_name or sysctl kernel.hostname=new_name, I would not see the name changed in /etc/hostname. And, if I reboot the system, I see the prompt change from "root@localhost" before the reboot to "root@moonpoint" after the reboot when I log in as root.

Before:

[root@localhost ~]# reboot

After:

[root@moonpoint ~]#

If I changed the name with the hostname command, the prompt did not change.

If you wish to permanently change the host name on an Apple OS X system, use the command sudo scutil --set HostName new_name.

 

Firstrade newegg.com

Justdeals Daily Electronics Deals1x1 px