Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Change Hostname on Ubuntu 18.04?
A hostname is the unique identifier of a device in a network, used to identify and connect to other devices. It is a label given to a computer that enables unique identification when connected to the internet or any network. By default, Ubuntu assigns a hostname during installation, but you may need to change it to reflect the machine's function or location for easier network identification.
Checking Current Hostname
Before changing your Ubuntu 18.04 hostname, check the current configuration using the hostnamectl command. Open a terminal and run
hostnamectl
This displays system information including the current hostname under "Static hostname".
Understanding the Output
The hostnamectl command shows three types of hostnames
Static hostname Permanent name set during installation or manually configured
Transient hostname Dynamic name assigned by DHCP or network services
Pretty hostname Human-friendly descriptive name for display purposes
Additional information includes icon name (for graphical environments), chassis type (machine type), machine ID (unique hardware identifier), and boot ID (current boot cycle identifier).
Changing Hostname Temporarily
Use the hostnamectl command to change the hostname immediately
sudo hostnamectl set-hostname new-hostname
Replace new-hostname with your desired name. This change is temporary and will revert after a system reboot.
Available Options
--prettySets a descriptive "pretty" name--staticSets the static hostname permanently--transientSets a temporary hostname that doesn't survive reboots
Making Permanent Changes
To make hostname changes permanent, edit the /etc/hostname and /etc/hosts files.
Edit /etc/hostname File
sudo nano /etc/hostname
Replace the current hostname with your new hostname, then save and close the file.
Edit /etc/hosts File
sudo nano /etc/hosts
Update all instances of the old hostname with the new hostname. Typically, you'll find entries like
127.0.0.1 localhost 127.0.1.1 old-hostname
Change to
127.0.0.1 localhost 127.0.1.1 new-hostname
Save and close the file. These changes ensure the new hostname persists after system restarts.
Verifying Changes
After changing the hostname, verify the changes using
hostnamectl
Check the "Static hostname" field to confirm it displays your new hostname. For immediate verification without rebooting, you can also use
hostname
Troubleshooting Common Issues
Permission Denied Error
If you encounter permission errors, ensure you're using sudo with administrative privileges
sudo hostnamectl set-hostname new-hostname
Cached Hostname Issues
If the old hostname persists, clear DNS caches
sudo systemctl restart systemd-resolved sudo systemd-resolve --flush-caches
Network Connectivity Issues
If network connectivity is lost after hostname changes, verify your /etc/hosts file configuration and ensure DHCP settings on your network are functioning properly. Restart the network service if needed
sudo systemctl restart networking
Conclusion
Changing the hostname on Ubuntu 18.04 involves using the hostnamectl command for temporary changes or editing system files for permanent changes. This simple process helps improve network organization and system identification. Always verify changes using hostnamectl and address any connectivity issues by checking file configurations and network services.
