Learn › Linux Foundations › Important Files and Paths
cat - a hands-on Linux lab on a real virtual machine.
Read the machine's own name from /etc/hostname. One line, one name, read at boot. Learn the file (persistent name) versus the hostname command (live name), how hostnamectl changes both, and how the name lines up with /etc/hosts. Structure-only: the name varies per machine, so the drills teach the shape.
Every machine you have logged into so far has greeted you with a name. Look at your shell prompt: something like student@debian:~$. The part after the @, here debian, is the machine's name. Networks use it. Log files stamp it on every line. It is how one machine says who it is.
But where does that name come from? It is not magic and it is not random. It is written down, in plain text, in one small file, and the system reads that file the moment it boots. This lesson opens that file. It is called /etc/hostname.
The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. /etc/hostname holds a REAL machine's name, so the exact word inside it changes from machine to machine. The name shown in these examples is debian. On the live machine at the end of the module, your machine will show its OWN name. What never changes is the SHAPE of the file, and that shape is the whole lesson.
/etc/hostname is a plain-text file that stores one thing: the machine's own name. The word hostname just means the name given to a computer, the way a nameplate on a door gives a room its name.
It lives in /etc, the directory where Linux keeps its system configuration files. You have already visited neighbours of this file in this module: /etc/passwd for accounts, /etc/hosts for name-to-address mappings. /etc/hostname is the smallest of them all. It holds a single name and nothing else.
When the machine powers on, one of the first things it does is read this file to learn what to call itself. That is why it matters: this one line decides the name you see in your prompt, in your logs, and on the network.
You already know the tool for printing a file's contents to the screen: cat, from the Reading Files work. Point it at the file and it prints what is inside. Because this file holds a single line, cat prints a single line.
Before you run it, decide what you expect: how many lines will come back? Then read the whole nameplate:
cat /etc/hostname
prompt: student@debian:~$ answer: cat /etc/hostname output: debian hint: Type cat, a space, then the full path: cat /etc/hostname
One line. Just the name, debian, and nothing else: no =, no colon, no fields, no comment, no second line. That is the entire file. This is the whole format of /etc/hostname and it is the same on every Linux machine: a single line holding a single name. Only the name itself changes.
That word debian is the name of the machine this example was captured on. On YOUR machine the name will be different: it might read webserver01, laptop, db-prod-2, or whatever name was given when the machine was set up. What stays identical everywhere is the SHAPE: one line, one name, nothing else. Memorise the shape, not the word.
Here is the distinction that trips people up, so slow down for it. There are two versions of the machine's name, and they are not the same thing.
The first is the persistent name: the one written in /etc/hostname. It is called persistent because it survives a reboot. Every time the machine starts, it reads this file to set its name. That is the name you just printed with cat.
The second is the live name: the name the running system is using at this very moment, held in memory. You read the live name with a command, not a file. The command is simply hostname. Run it now and compare it to the file:
hostname
prompt: student@debian:~$ answer: hostname output: debian hint: Just type the word hostname and press Enter, no path this time: hostname
The same name, debian, printed two ways: once by reading the file with cat /etc/hostname, once by asking the running system with hostname. On a settled machine they match, and that is the normal, healthy state. The difference is WHERE each one reads from: cat /etc/hostname reads the file on disk (the boot-time name), while hostname reads the value the kernel is holding in memory right now.
Why keep two names in your head if they usually match? Because there is one moment they do not, and understanding it is the point of this whole lesson.
Editing /etc/hostname by hand does NOT rename the running machine. The file feeds the name in at boot, so a hand-edit only takes effect the next time the machine restarts. Until then, the file says one name and the hostname command still reports the old one. The proper tool changes both at once: hostnamectl set-hostname NAME sets the live name immediately AND writes the new name into /etc/hostname, so the two never drift apart.
You do not need to change anything here. Just read the file one more time and hold the mental model: this file is the name the machine will wake up with.
cat /etc/hostname
prompt: student@debian:~$ answer: cat /etc/hostname output: debian hint: Same command as before: cat /etc/hostname
The name is still shown as debian here only because that is the example machine. The lesson is not the word, it is the RULE: the file is the boot-time name, the hostname command is the live name, and hostnamectl set-hostname is the one move that changes both together. Edit the file alone and nothing renames until reboot.
One file does not live alone. The name in /etc/hostname usually shows up again in its neighbour /etc/hosts, the file that maps names to addresses. On most machines a line near the top of /etc/hosts reads 127.0.1.1 followed by the same name you just saw in /etc/hostname. That line lets the machine find its own name on the network.
You met /etc/hosts earlier in this module, so this is a quick look, not a new lesson. Read it and find the name you already know:
cat /etc/hosts
prompt: student@debian:~$ answer: cat /etc/hosts output: 127.0.0.1 localhost 127.0.1.1 debian hint: Same cat, different file: cat /etc/hosts
There it is again. The 127.0.1.1 line carries the same name, debian, that /etc/hostname holds. That is not a coincidence: when a machine is named, the name is written into both files so the system can resolve its own name to an address. If you ever rename a machine properly, both files end up carrying the new name.
Your /etc/hosts will show your machine's name on the 127.0.1.1 line, not debian, and the exact localhost lines may vary slightly by distribution. The takeaway is the LINK, not the literal text: the name on the 127.0.1.1 line normally matches the single line in /etc/hostname.
Scaffolding off. No command is printed this time. You have everything you need.
You have just logged into an unfamiliar machine and you want the one fact that identifies it: the persistent name it was given, the exact word stored in its nameplate file on disk. Not the live value from a command, the file itself.
prompt: student@debian:~$ answer: cat /etc/hostname output: debian hint: You want to print a file's one line. Think of the reading command, then the full path to the nameplate file: /etc/hostname
That is the everyday move. cat printed the file, and the path /etc/hostname pointed it at the nameplate. One line came back, just the name, because that is all the file ever holds. Your own machine's name will differ, but the shape stays: one line and one name, on any machine. And the command is exactly what a working engineer types to check a machine's persistent name.
You earned this cheat sheet. Every row is something you just ran or learned:
And the one fact about the file to carry with you: /etc/hostname is a single line holding a single name, nothing else. It is the boot-time name; the hostname command is the live name; on a settled machine the two match.
A valid hostname is made of letters, digits, and hyphens, with no spaces, no domain suffix, and no punctuation. /etc/hostname holds just the short name (like webserver01), not a full address like webserver01.example.com. Keep it to one clean word.
The practice terminal has shown you the shape of /etc/hostname: one line, one name, read at boot. You know it is the persistent name, that the hostname command reports the live name, that hostnamectl set-hostname changes both, and that the same name shows up on the 127.0.1.1 line of /etc/hosts. Every command you typed yourself.
The Important Files module ends with one real Linux machine, the Important Files capstone mission, and that is where you read these files for real. A VM boots just for you, with its own accounts, its own name, and its own hardware. It hands you objectives that use exactly what you practiced across this module: reading the files that define who the machine is and what it runs. One difference from here: the mission shows no commands. You read the objective, you recall the command, you type it. That recall is what makes it stick.
Finish the other Important Files lessons, then go read a real machine's nameplate for yourself.
Practice /etc/hostname in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.