For most of you, your hostname in Linux is going to be the same as your username. This article will help you figure out your hostname in Linux, and it should be a relatively short article. If this article looks familiar, it’s because it existed at the old site, but with far less information.
Let’s start at the beginning, by learning what a hostname is.
A hostname is generally meant to be a human-readable way to identify a computer. Yes, the computer is truly identified by the associated IP address, but the hostname resolves to that computer. It is meant to be easily remembered instead of having to remember a bunch of IP addresses. Without it, the web (and your own local networks) would be more difficult to navagate.
In many instances, it’s a FQDN (Fully Qualified Domain Name), such as a domain name like linux-tips.us. However, this site doesn’t have a dedicated IP address, so it’s known by a virtual hostname as a FQDN. If the site had been constructed differently, you’d be able to access it with the IP address. For example, https://172.217.10.14 will take you to Google (after some warnings).
So, your hostname in Linux isn’t always going to be the same as your username. For most of the readers here on L-T.us, it will be. You probably don’t really need to worry a whole lot about things like virtual hosts and stuff like that. But, even as a home user, you can make use of this information.
How is it useful? Well, if I want to SSH into my MSI laptop, I don’t have to use the IP address. Not only that, if the IP address changes then I don’t need to look it up. I just connect with something like:
ssh kgiii-msi.local
You may notice that I added the .local. You too can just add .local with many distros, in others the default might be something like .localdomain – which you can keep as the default or change it easily enough.
So, let’s figure out how to find your hostname in Linux.
Let’s start by cracking open the terminal. You can use your keyboard to do this, just press CTRL + ALT + T and your default terminal should open up.
Now, we can try the following:
cat /proc/sys/kernel/hostname
Or you can try:
hostname
Or you can also try:
uname -n
As near as I can tell, one of those should work on all major distros. If it none of them work for you, please leave a comment letting me know what does work – helping those who come behind you.
There are a number of ways that this information can be used. With many distros, as mentioned above, you can add .local to the hostname and just use that instead of IP addresses. Here’s a picture that shows a few examples:
There are, of course, other things you can do with this information. Those are just a few examples. The great thing about using a hostname in Linux is that you’ll not have to recall IP addresses. You’ll only need to recall a name – a name that you picked.
You can change your hostname, but that goes beyond the scope of this article. I’d expect to see an article about that in the future! In the meantime, if you can think of any ways that you’d use the hostname, please leave a comment.
Thank you, my dear reader, for your viewership. Your comments and feedback keep me motivated and your feedback helps to improve this site. For those who have been keeping track, you can actually now click on large images to see the larger versions. This will only be for future articles, as I’m unlikely to go back through every previous article and change it. But, it was direct feedback that resulted in the change.
If you’d like to contribute, there are a number of ways. You can donate, write an article, unblock ads, buy hosting, and sign up for the newsletter. I promise, I’ll never share your email address with anyone and I won’t send you any spam. Until next time…
Today we'll cover one way to enable or disable your network interface in the Linux…
Today's exercise is a nice and simple exercise where we check your NIC speed in…
Have you ever wanted to easily monitor your wireless connection? Well, now you can learn…
I think I've covered this before with the ls command but this time we'll count…
Today we'll be learning about a basic Linux command that's known as 'uname' and it…
If you've used hardinfo in the past, it may interest you to know that hardinfo…
View Comments
hostname can be confusing for some i guess, since when i first got into linux and configured network , questions like what is your hostname came up- its not obvious its basically the name of your computer on a network.
hostname is also mentioned in /etc/hosts file eg:
$ cat /etc/hosts (05-25 19:05)
# Static table lookup for hostnames.
# See hosts(5) for details.
127.0.0.1 localhost
::1 localhost
127.0.1.1 darkstar.localdomain darkstar
above is my output on Arch Linux where darkstar is my host name . I've seen how did you get your name handle on l'.org i guess we could ask how did you name your PC ?
I'm not sure why I had to manually approve your comment. You should be all set and able to comment without approval - but I'll run through the settings again shortly.
That would make a great question. (You can say linux.org, or even link to it if you wanted, I don't mind at all.)
Maybe, "How did you pick the hostname (or username) for the computers on your network?" Something like that?
This is just a test comment.
All three suggested 'codes' work on Linux Mint 20.1 (Cinnamon)
Nice! I didn't check any of them on Mint, I don't think. I did my testing in VMs of different OSes. I guess I just kinda assumed that it'd work just fine in Mint as it worked in Lubuntu.
You forgot about hostnamectl:
hostnamectl [OPTIONS...] COMMAND ...
Query or change system hostname.
Commands:
status Show current hostname settings
set-hostname NAME Set system hostname
set-icon-name NAME Set icon name for host
set-chassis NAME Set chassis type for host
set-deployment NAME Set deployment environment for host
set-location NAME Set location for host
Options:
-h --help Show this help
--version Show package version
--no-ask-password Do not prompt for password
-H --host=[USER@]HOST Operate on remote host
-M --machine=CONTAINER Operate on local container
--transient Only set transient hostname
--static Only set static hostname
--pretty Only set pretty hostname
See the hostnamectl(1) man page for details.
Thanks for the addition!