The hostname command in Linux is an essential tool for every system administrator and developer. It allows you to manage and configure your system’s hostname, which is vital for identifying machines on a network. Whether you need to temporarily rename a server or configure it permanently for production, mastering this command is key.
In this guide, we’ll walk you through how to check and change your Linux system’s hostname, both temporarily and permanently. You can also watch our step-by-step video tutorial below to follow along visually.
What is a Hostname?
Before diving into the commands, it’s important to understand what a hostname is. In simple terms, a hostname is a unique name assigned to your Linux system that identifies it within a network. This could be something like my-server
or web-server-01
, depending on the machine’s role.
The hostname is used by other machines to identify your system in a network, and it is essential for managing and configuring multiple systems, especially in larger environments.
Checking the Current Hostname
To check your system’s current hostname, simply run the following command:
This will output the current hostname of your Linux system. For example, you may see something like my-server
or another name you assigned during installation.
Changing the Hostname Temporarily
If you need to change your system’s hostname temporarily (without rebooting), you can use the hostname command with the new hostname as an argument. For example:
Now, when you run the hostname
command again, you’ll see the updated hostname. However, this change will only last until the system reboots.
Real-life Use Case:
Imagine you need to quickly rename a server for a maintenance window or testing purposes. This command allows you to do so without disrupting the system’s running services.
Changing the Hostname Permanently
To make the hostname change permanent, you need to modify the system’s configuration files.
- Edit the /etc/hostname file:
Replace the existing hostname with your new one.
- Restart the systemd-hostnamed service:
After doing this, check the hostname again using the hostname
command to confirm the change.
Updating /etc/hosts for Local Resolution
For proper local resolution, it’s important to update the /etc/hosts file. This ensures the new hostname works with localhost.
- Open the /etc/hosts file:
- Add your new hostname next to
127.0.0.1
and::1
. For example:
- Save the changes and exit.
Now, the system can resolve the hostname properly when accessing it via localhost
.
Verifying the Hostname
To verify the hostname change, use these two commands:
The hostnamectl
command provides more detailed information about the current hostname configuration, including the static hostname and related settings.
Advanced Configuration: Using hostname Command
For more advanced configuration, you can use the hostnamectl command, which offers an easier and more streamlined way to manage hostnames. To set the hostname permanently, use the following command:
This command will automatically update the /etc/hostname file and apply the changes without needing to restart the systemd-hostnamed
service manually.
Useful Hostname Command Options
The hostname command also comes with some useful options:
- Get IP address:
This will display the IP address of your system. It’s useful for managing multiple servers or when you need a quick check of your network configuration.
- Fully Qualified Domain Name (FQDN):
This option shows the FQDN of your system, which is helpful when working with DNS and network configurations.
Conclusion
The hostname command in Linux is a powerful tool that every Linux user should master. Whether you need to change the hostname temporarily or permanently, the steps are simple yet essential for managing your system effectively. By understanding how to configure the /etc/hostname and /etc/hosts files, you ensure proper local resolution and network identification for your machine.
Best Practices:
- Always use descriptive hostnames for your systems, especially in larger environments. For example, naming servers based on their roles, such as
db-server-01
,web-server-01
, andbackup-server-01
, can help you quickly identify them.
For more detailed steps, watch our tutorial video here.
FAQs
1. How do I check my system’s current hostname in Linux?
Simply run the hostname
command in your terminal.
2. How can I change my hostname temporarily?
Use the command sudo hostname new-hostname
to change it temporarily.
3. How do I make the hostname change permanent?
Edit the /etc/hostname file and update the hostname, then restart the systemd-hostnamed
service.
4. Why do I need to update the /etc/hosts file?
Updating /etc/hosts ensures that the system resolves the hostname correctly when accessed locally.
5. What is the difference between hostname
and hostnamectl
?
The hostnamectl command provides a more advanced and streamlined way to manage hostnames, including setting the permanent hostname without needing to edit files manually.