hostname and hostnamectl Command
In your shell, you might see your host name but it requires a special configuration for your shell prompt. This is not the best way for us to determine our host name. Why? First, your shell might not be configured to show your host name. Or even misconfigured to show it wrong! Second, how can you get your Linux host name information and use it inside a shell script?
To learn your Linux hostname, you can use hostname
command.
You can use hostname newhostname
to change your hostname, can you?
You might be asking "Why my host name keeps changing after reboot in Linux?". Well, because hostname
command changes your host name temporarily. If this is what you want, go for it.
How to Change Host Name Permanently in Linux?
To achieve this, we'll use hostnamectl
command.
root@localhost ~]# hostnamectl
Static hostname: localhost.localdomain
Icon name: computer-vm
Chassis: vm
Machine ID: afcc248f7e79e74fae68fa641b940b71
Boot ID: c40fb2c23ab54d6d9ffbec52cd127851
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1160.41.1.el7.x86_64
Architecture: x86-64
So, what are these? Icon name is used my some graphical applications to visualize your host. Chassis works like the same. But it determines the host type like desktop, tablet, server, embedded etc. Now let's change the host name and see how it goes. You can change your host name in Linux via hostnamectl set-hostname new_host_name
command:
root@localhost ~]# hostnamectl set-hostname "Jack's Laptop"
root@localhost ~]# hostnamectl
Static hostname: jackslaptop
Pretty hostname: Jack's Laptop
Icon name: computer-vm
Chassis: vm
Machine ID: afcc248f7e79e74fae68fa641b940b71
Boot ID: c40fb2c23ab54d6d9ffbec52cd127851
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1160.41.1.el7.x86_64
Architecture: x86-64
As you can see, the output has changed. Now we can see a "Pretty hostname". Pretty hostname is a name that you want to see in your computer. It's a bit flexible. Uppercase, lowercase, spaces and even a special character included in this host name. This is a high-level host name. On the other hand, "Static hostname" is used to initialize the kernel host name at boot. Your static host name resides at /etc/hostname
file.
How to Change Some Other Host Properties in Linux?
Sometimes the host name itself might not be enough to address a computer. Hence you might want to give some additional info:
root@localhost ~]# hostnamectl set-hostname "Blog Web Server (Apache)"
root@localhost ~]# hostnamectl set-deployment development
root@localhost ~]# hostnamectl set-location "Amsterdam DC AX21 3rd Shelf"
root@localhost ~]# hostnamectl set-chassis server
root@localhost ~]# hostnamectl status
Static hostname: blogwebserverapache
Pretty hostname: Blog Web Server (Apache)
Icon name: computer-server
Chassis: server
Deployment: development
Location: Amsterdam DC AX21 3rd Shelf
Machine ID: afcc248f7e79e74fae68fa641b940b71
Boot ID: c40fb2c23ab54d6d9ffbec52cd127851
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1160.41.1.el7.x86_64
Architecture: x86-64
We can use hostnamectl set-deployment
to set the deployment mode like "staging", "production", "development" etc. By using hostnamectl set-location
, we can address the physical location of a machine. hostnamectl set-chassis
can help us to set the device's chassis type. When we check the host name information of our Linux machine via hostnamectl status
; we can easily say that this is a server located in Amsterdam Data Center AX21 cabinet 3rd shelf, used for development purposes and runs an Apache web server for our blog.
All of these information are stored in /etc/machine-info
file.