The hostname of a Server will be set at the time of installation of Debian operating system. If you are creating a virtual machine, it will dynamically assign when instance created. When choosing your hostname you should also make sure to use a Fully Qualified Domain Name (FQDN) and it should be pointed to your server IP address so you can access your server by using it.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux System queries.
In this context, we shall look into how to change hostname on Debian 9 system.
Before proceeding with this Configuration Procedure, ensure that you are logged in as a non-root user with sudo privileges.
By assigning a hostname you can identifies a system on a network. You must make sure that there shouldn't have same hostname on a same network.
To get Current Hostname:
Start by checking current hostname with the below command:
$ hostnamectl
Static hostname: local.linuxapt
Icon name: computer-vm
Chassis: vm
Machine ID: 287b816ad03c4f429f7bb49501983e1c
Boot ID: 7fd5cca4dd5c41f6ac53214e11fa3f57
Virtualization: oracle
Operating System: Debian GNU/Linux 9 (stretch)
Kernel: Linux 4.9.0-8-amd64
Architecture: x86-64
Here, the current hostname is local.linuxapt.
How to Change the Hostname using hostnamectl ?
In Debian 9, there are multiple methods to change hostname. First one is change the hostname using hostnamectl command. Following is the basic syntax to change hostname:
$ sudo hostnamectl set-hostname NEW_HOST_NAME
You can run command as below to change hostname:
$ sudo hostnamectl set-hostname server.linuxapt.com
We changed the hostname from local.linuxapt to server.linuxapt.com using hostnamectl command.
Now the hostname is server.linuxapt.com for server, you can check it again using hostnamectl command.
How to Change the Hostname by Edit /etc/hosts file ?
Another method to change hostname is to edit /etc/hosts file manually. Open /etc/hosts file using your favorite text editor:
$ sudo nano /etc/hosts
Inside the file, change the hostname as per you choice at line that begins with 127.0.0.1.
Save and close file. For this method you need to restart your system.
How to Verify Hostname ?
After changed hostname using second method, you can again check hostname using hostnamectl command and now it should give output with new hostname as below:
Static hostname: server.linuxapt.com
Icon name: computer-vm
Chassis: vm
Machine ID: 287b816ad03c4f429f7bb49501983e1c
Boot ID: 7fd5cca4dd5c41f6ac53214e11fa3f57
Virtualization: oracle
Operating System: Debian GNU/Linux 9 (stretch)
Kernel: Linux 4.9.0-8-amd64
Architecture: x86-64
As you can see above the hostname is successfully changed from local.linuxapt to server.linuxapt.com .
This article covers how to change the hostname on Debian 9 system using different methods. Basically, the hostname is a label that is assigned during the initial server setup and it is used to identify and easily distinguish one server from another.
To check your current hostname:
$ hostname
And to check your Fully Qualified Domain name (FQDN) you can run the following command instead:
$ hostname -f
How to Change your Debian hostname ?
1. To change your hostname, we can simply run the following command:
$ hostname new.hostname.com
2. So in order to change our hostname permanently, you will need to update this file. You can open it with your favorite text editor, for example:
$ nano /etc/hostname
3. Change the hostname, save the file and exit the text editor.