Understanding and Using the hostname Command in Ubuntu
Understanding and Using the hostname
Command in Ubuntu
Welcome, Ubuntu users! Today, we're diving into a simple yet powerful command: hostname
. This command allows you to view and change your computer's hostname, which is essentially its name on a network. Let's break it down.
What is a Hostname?
Think of your hostname as your computer's "nickname." It helps identify your machine on a network, making it easier to communicate with other devices. By default, Ubuntu assigns a hostname during installation, but you can easily change it.
Viewing Your Hostname
To see your current hostname, simply open your terminal and type:
hostname
This will display the current hostname.
Changing Your Hostname (Temporarily)
To change your hostname temporarily (meaning it will revert after a reboot), use:
sudo hostname new-hostname
Replace new-hostname
with your desired name. For example:
sudo hostname my-ubuntu-machine
Making Changes Permanent
To make your hostname change permanent, you'll need to edit the /etc/hostname
file.
-
Open the file:
Bashsudo nano /etc/hostname
-
Replace the existing hostname with your desired new hostname.
-
Save the file (Ctrl+X, Y, Enter).
-
Update
/etc/hosts
:Bashsudo nano /etc/hosts
Find the line that looks like:
127.0.0.1 your-old-hostname
Change
your-old-hostname
to yournew-hostname
. It should look like this:127.0.0.1 your-new-hostname
Also verify that the line containing your public IP address also has your new hostname listed.
-
Save the file (Ctrl+X, Y, Enter).
-
Reboot your system to apply the changes:
Bashsudo reboot
Useful hostname
Options
The hostname
command also provides some useful options:
-
-i
or--ip-address
: Displays the IP address(es) associated with the hostname.Bashhostname -i
-
-I
or--all-ip-addresses
: Displays all IP addresses of all network interfaces. This is useful when you have multiple network connections.Bashhostname -I
-
-f
or--fqdn
: Displays the Fully Qualified Domain Name (FQDN).Bashhostname -f
-
-s
or--short
: Displays the short hostname (the part before the first dot).Bashhostname -s
Summary Table
Example | Description |
hostname | Displays the current hostname. |
hostname -i | Displays the IP address(es) associated with the hostname. |
hostname -I | Displays all IP addresses of all network interfaces. |
hostname -f | Displays the Fully Qualified Domain Name (FQDN). |
hostname -s | Displays the short hostname. |
In Conclusion
The hostname
command is a simple but essential tool for managing your Ubuntu system's identity on a network. By understanding how to view and change your hostname, you can better manage your system and network connections. Happy Ubuntu-ing!
Need Ubuntu Expertise?
If you need help with your Ubuntu projects or have any questions, feel free to reach out to us!
Email us at: info@pacificw.com
Image: Gemini
Comments
Post a Comment