How to use the command 'hostname' (with examples)
The hostname
command is used to show or set the system’s host name. This command is useful for identifying the name and network addresses of the host, as well as setting a new host name if needed.
Use case 1: Show current host name
Code:
hostname
Motivation: This use case is useful when you want to quickly find out the current host name of your system.
Explanation: This command without any arguments simply outputs the current host name of the system.
Example output:
myhostname
Use case 2: Show the network address of the host name
Code:
hostname -i
Motivation: This use case is helpful when you need to determine the network address associated with the host name.
Explanation: The -i
option is used to display the network address (IP address) of the current host name.
Example output:
192.168.0.100
Use case 3: Show all network addresses of the host
Code:
hostname -I
Motivation: This use case is useful when you want to view all network addresses (IPv4 and IPv6) associated with the host.
Explanation: The -I
option displays all the network addresses (IP addresses) associated with the host.
Example output:
192.168.0.100 2607:f8b0:4004:809::200e
Use case 4: Show the FQDN (Fully Qualified Domain Name)
Code:
hostname --fqdn
Motivation: This use case is helpful when you need to obtain the fully qualified domain name of the host.
Explanation: The --fqdn
option displays the fully qualified domain name (FQDN) of the host.
Example output:
myhostname.example.com
Use case 5: Set current host name
Code:
hostname new_hostname
Motivation: This use case allows you to change the current host name to a new desired name.
Explanation: The command hostname
followed by the desired new host name sets the current host name to the specified value. This can be useful when you want to assign a more meaningful or unique name to your system.
Example output: (none)
(Note: If the command is successful, there will be no output. Run the hostname
command without any arguments to verify the newly set host name.)
Conclusion:
The hostname
command provides various options for displaying and setting the host name of a system. Whether you need to view the current host name, find the network address, obtain the fully qualified domain name, or change the host name, the hostname
command has you covered. Use the different options according to your specific needs for better system identification and management.