hostname ‐ #system #networking - five4nets/Linux-Knowledgebase GitHub Wiki
Linux Hostname Command Tutorial
This tutorial provides a comprehensive guide to using the hostname
command in Linux. The hostname
command is used to display or set the system's hostname, which is a label assigned to a device on a network. This guide includes explanations, examples, and references for further reading.
Table of Contents
- What is the Hostname Command?
- Syntax and Options
- Common Use Cases and Examples
- Important Notes
- References
What is the Hostname Command?
The hostname
command in Linux is used to view or modify the system's hostname. The hostname is a unique name assigned to a computer or device on a network, helping identify it in network communications. This command is useful for system administrators managing servers or networked devices.
Syntax and Options
The basic syntax of the hostname
command is:
hostname [OPTION]... [HOSTNAME]
Common Options:
-a
,--alias
: Display the alias name of the host (if used).-d
,--domain
: Display the domain name.-f
,--fqdn
,--long
: Display the fully qualified domain name (FQDN).-i
,--ip-address
: Display the IP address(es) associated with the hostname.-s
,--short
: Display the short hostname (without the domain).-I
,--all-ip-addresses
: Display all IP addresses of the host.--help
: Display help information.--version
: Display version information.
Note: Setting a hostname requires root privileges, typically using
sudo
.
Common Use Cases and Examples
Display the Current Hostname
To view the current hostname of the system, simply run:
hostname
Example Output:
myserver
This displays the short hostname of the system.
Display the Fully Qualified Domain Name (FQDN)
To display the FQDN, which includes both the hostname and domain name, use:
hostname -f
Example Output:
myserver.example.com
This is useful when you need the complete network identifier for the system.
Show IP Addresses Associated with the Hostname
To display the IP address(es) associated with the hostname, use:
hostname -i
Example Output:
192.168.1.100
To show all IP addresses (useful for systems with multiple network interfaces), use:
hostname -I
Example Output:
192.168.1.100 10.0.0.1
Set a New Hostname Temporarily
To temporarily change the hostname (until the next reboot), use the command with root privileges:
sudo hostname new-hostname
Example:
sudo hostname webserver01
Verify the change:
hostname
Output:
webserver01
Note: This change is temporary and will revert after a system reboot.
Set a New Hostname Permanently
To permanently change the hostname, you need to modify system configuration files. The process varies slightly depending on the Linux distribution.
On Debian/Ubuntu-based Systems
- Edit the hostname file:
sudo nano /etc/hostname
Replace the current hostname with the new one, e.g., webserver01
. Save and exit.
- Update the
/etc/hosts
file to map the new hostname to the IP address:
sudo nano /etc/hosts
Modify the line with 127.0.1.1
or the relevant IP to include the new hostname, e.g.:
127.0.1.1 webserver01.example.com webserver01
- Apply the changes (a reboot may be required):
sudo hostname -F /etc/hostname
On RHEL/CentOS-based Systems
Use the hostnamectl
command (part of systemd
) for a more modern approach:
sudo hostnamectl set-hostname webserver01
This updates both the transient and persistent hostname. Verify with:
hostnamectl
Example Output:
Static hostname: webserver01
Icon name: computer-vm
Chassis: vm
Machine ID: ...
Boot ID: ...
Operating System: CentOS Linux 8
Kernel: Linux 4.18.0
Architecture: x86-64
Note: Always update
/etc/hosts
if needed to avoid resolution issues.
Important Notes
- Root Privileges: Commands that modify the hostname require
sudo
or root access. - Network Services: Changing the hostname may affect network services like SSH or web servers. Ensure related configurations (e.g.,
/etc/hosts
, DNS) are updated. - Reboot: For permanent changes, a reboot may be necessary to ensure all services recognize the new hostname.
- Compatibility: The
hostname
command is part of theinetutils
ornet-tools
package and is available on most Linux distributions.