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
5 ‘hostname’ Command Examples for Linux Newbies
The hostname command in Linux is essential for viewing and managing your system's network identity. It allows you to display or set the hostname of your system, which is crucial for network identification and communication. This article explores practical hostname command examples that will help Linux newcomers master this fundamental tool.
What is a Hostname?
A hostname is a unique name that identifies a computer on a network. It consists of letters, numbers, and hyphens, and often includes a domain name. For example, web-server or ubuntu.example.com are valid hostnames. The hostname command displays or modifies this system identifier.
1. View Current Hostname
The simplest use of the hostname command is to display your system's current hostname. Open a terminal and run:
hostname
Example output:
ubuntu-desktop
2. Set Hostname Temporarily
You can set a new hostname temporarily using the hostname command. This change will be lost after a system restart:
sudo hostname new-hostname
Verify the change:
hostname
Example output:
new-hostname
3. Set Hostname Permanently
To make permanent hostname changes, modify the system configuration files:
Step 1: Edit the hostname file
sudo nano /etc/hostname
Step 2: Update the hosts file
sudo nano /etc/hosts
Locate the line with 127.0.0.1 and update the hostname entry. Save both files and restart your system.
Modern approach: Use hostnamectl for immediate permanent changes:
sudo hostnamectl set-hostname new-hostname
4. Display FQDN (Fully Qualified Domain Name)
The FQDN includes both the hostname and domain name, providing the complete network address:
hostname -f
Example output:
ubuntu-desktop.example.com
5. Display IP Address Associated with Hostname
To view the IP address linked to your hostname:
hostname -I
Example output:
192.168.1.100
Additional Hostname Command Options
Check Remote Machine Hostname
Verify the hostname of a remote system using SSH:
ssh user@remote-machine hostname
Display System Information with uname
Alternative method to display hostname:
uname -n
Domain Name Operations
View or set the NIS/YP domain name:
domainname sudo domainname new-domain-name
Common Use Cases
| Scenario | Command | Purpose |
|---|---|---|
| Network troubleshooting | hostname -I |
Identify system IP address |
| Server configuration | hostnamectl set-hostname |
Set permanent hostname |
| Remote administration | ssh user@host hostname |
Verify remote system identity |
| DNS verification | hostname -f |
Check FQDN resolution |
Conclusion
The hostname command is a fundamental Linux tool for managing system network identity. Whether you need to view the current hostname, make temporary changes, or configure permanent settings, these examples provide the foundation for effective hostname management. Mastering this command is essential for Linux system administration and network troubleshooting.
