How to get the computer name (not its hostname)?

The closest equivalent to a human-readable (and human-chosen) name for a computer running Linux is the default hostname stored in /etc/hostname. On some (not all) Linux distributions, this name is entered during installation as the computer's name (but with network hostname constraints, unlike Mac OS X's computer name).


You could use the sudo dmidecode | grep -A3 '^System Information' command. This command reads information from the BIOS and the hardware. Example from my machine:

System Information
    Manufacturer: LENOVO
    Product Name: 20BHA06YGB
    Version: ThinkPad W540

Sources: [1]


Strictly speaking, there is no such thing as network-unrelated "computer name" in Linux, and I actually fail to see the purpose of naming computers which are not on the network.

The reason why your computer has different strings in /etc/hostname, /etc/hosts and uname -n is that DHCP protocol has facilities to provide a hostname along with IP address to a new host. "machine42.work.localnetwork" looks exactly like a name the DHCP server would pick. This string is then saved and returned by gethostname calls.

See also:

How do I change the computer name? (tl;dr echo computername > /etc/hostname) - that's what happens when you pick a name during the installation.

How do I change the hostname without a restart? (tl;dr hostname computername) - that's what happens when you get a DHCP lease with a host name.

Tags:

Linux

Hostname