hostname, hostnamectl, getmac, nbtstat, ipcalc
These tools answer one question: who is this machine? hostname and hostnamectl give its name, getmac its MAC addresses, nbtstat its NetBIOS names, and ipcalc works out the subnet it lives in.
23.1 Which tool, which question, where
| Question | Tool | Where it runs |
|---|---|---|
| Machine name | hostname | Windows, macOS, Linux (built in) |
| Name + metadata | hostnamectl | systemd Linux |
| MAC per adapter | getmac | Windows built in (CMD, PowerShell) |
| NetBIOS names | nbtstat | Windows built in |
| Subnet maths | ipcalc | Linux: apt install ipcalc; macOS: brew |
23.2 hostname: Windows and macOS
- Windows hostname only prints the name; hostname -s fails with 'Use the Network Control Panel Applet' (errorlevel 1).
- Trick seen in the lab: the _CLUSTER_NETWORK_NAME_ environment variable overrides what hostname prints.
- macOS: -f name + domain, -s short, -d domain; sudo hostname NAME lasts until reboot, sudo scutil --set HostName NAME is permanent.
PowerShell
hostname /?hostname -s$env:_CLUSTER_NETWORK_NAME_ = 'N023-DEMO'hostnameOutput
$ hostname /?Prints the name of the current host. hostname$ hostname -ssethostname: Use the Network Control Panel Applet to set hostname.hostname -s is not supported.(errorlevel 1)$ $env:_CLUSTER_NETWORK_NAME_ = 'N023-DEMO'$ hostnameN023-DEMOTerminal
hostname # print the namehostname -f # name + domain (default)hostname -s # short name onlyhostname -d # domain onlysudo hostname NAME # new name until rebootsudo scutil --set HostName NAME # permanent name23.3 Linux hostname options
- -s short name, -f FQDN, -d DNS domain, -y NIS domain.
- -I reads all IPs straight from the interfaces (no DNS); -i resolves the name (hosts file, else DNS).
- -A does a reverse (PTR) lookup for every IP and prints the FQDNs.
- Others: -F FILE sets the name from a file, -b uses localhost if the file is empty; domainname, dnsdomainname, nisdomainname.
- Step 1
1.hostname -I: read IPs from web01's interfaces, no network traffic
- Step 2
2.hostname -i: look up the name in /etc/hosts first
- Step 3
3.Not found: DNS query goes via the router 192.168.43.1
- Step 4
4.DNS server 192.168.43.53 answers with the IP
- Step 5
5.hostname -A: PTR lookup for each IP
- Step 6
6.PTR answer returns the FQDN web01.lab.example
Terminal
hostnamehostname -shostname -fhostname -dhostname -ihostname -Ihostname -Ahostname -yOutput
$ hostnameweb01$ hostname -sweb01$ hostname -fweb01.lab.example$ hostname -dlab.example$ hostname -i192.168.43.10$ hostname -I192.168.43.10$ hostname -Aweb01.lab.example$ hostname -ylab.example23.4 /etc/hostname
- The static name lives in /etc/hostname; the hostname command alone changes it only until reboot.
- hostname -b -F /etc/hostname reloads the name from the file.
- Boot priority: systemd.hostname= kernel option > /etc/hostname > transient (DHCP) > localhost.
Terminal
cat /etc/hostnamehostname demo01hostnamehostname -b -F /etc/hostnamehostname # boot priority:# systemd.hostname= > /etc/hostname# > transient (DHCP) > localhostOutput
$ cat /etc/hostnameweb01$ hostname demo01$ hostnamedemo01$ hostname -b -F /etc/hostname$ hostnameweb0123.5 hostnamectl: three names and metadata
| Name | Stored in | Meaning |
|---|---|---|
| static | /etc/hostname | Survives reboot |
| transient | Kernel (can come from DHCP) | Current running name |
| pretty | /etc/machine-info | "Web Server 01", display only |
- hostnamectl hostname NAME sets all three; --pretty / --static / --transient limit it to one.
- chassis (desktop, laptop, server, vm, container ...), deployment, location and icon-name add metadata.
- If a static name is set, a transient name is ignored (the Hint in the output).
- --json=pretty|short for scripts, -H user@host over SSH, -M for a local container.
Terminal
hostnamectlhostnamectl hostname "Web Server 01" --prettyhostnamectl chassis serverhostnamectl deployment productionhostnamectl location "Rack 2, Shelf 3"hostnamectl icon-name computer-serverhostnamectlOutput
$ hostnamectl Static hostname: web01 Icon name: computer-container Chassis: container Virtualization: wslOperating System: Debian GNU/Linux 12 (bookworm) Architecture: x86-64$ hostnamectl Static hostname: web01 Pretty hostname: Web Server 01 Icon name: computer-server Chassis: server Deployment: production Location: Rack 2, Shelf 3Operating System: Debian GNU/Linux 12 (bookworm)Terminal
hostnamectl --statichostnamectl --prettyhostnamectl --transienthostnamectl hostname web02 --transienthostnamectl --json=prettyOutput
$ hostnamectl --staticweb01$ hostnamectl --prettyWeb Server 01$ hostnamectl --transientweb01$ hostnamectl hostname web02 --transientHint: static hostname is already set, so the specified transient hostname will not be used.$ hostnamectl --json=pretty{ "Hostname" : "web01", "StaticHostname" : "web01", "HostnameSource" : "static", "Chassis" : "server", "Deployment" : "production",23.6 getmac (Windows)
- Shows the MAC (Physical Address) and transport name of every adapter; /v adds the connection name and adapter.
- /fo table|list|csv picks the format, /nh drops the header, /s /u /p query a remote computer.
- 'Media disconnected' means the adapter has no link.
PowerShell
getmac /v /fo listgetmac /fo csv /nhOutput
$ getmac /v /fo listConnection Name: EthernetNetwork Adapter: Realtek PCIe GbE Family ControllerPhysical Address: [MAC hidden]Transport Name: Media disconnected Connection Name: Wi-FiNetwork Adapter: Realtek RTL8723BE 802.11 bgn Wi-Fi AdapterPhysical Address: [MAC hidden]Transport Name: \Device\Tcpip_{GUID}$ getmac /fo csv /nh"[MAC hidden]","Media disconnected"23.7 nbtstat (Windows)
- Shows NetBIOS over TCP/IP: an old Windows naming system still seen in file sharing.
- -A IP / -a NAME: remote name table; -c cache; -n local names; -R purge cache + reload Lmhosts; -RR WINS release + refresh.
- -s / -S sessions (names / IPs); a number repeats every N seconds.
- Step 1
1.Your PC (192.168.1.10) runs nbtstat -A 192.168.1.20
- Step 2
2.NetBIOS node status query on UDP 137
- Step 3
3.Query crosses the switch to PC01
- Step 4
4.PC01 replies with its name table and MAC
- Step 5
5.Reply returns via the switch to your PC
PowerShell
nbtstat -A 192.168.1.20nbtstat -a CORP07nbtstat -cnbtstat -nnbtstat -Rnbtstat -S 5Output
$ nbtstat /?NBTSTAT [ [-a RemoteName] [-A IP address] [-c] [-n] [-r] [-R] [-RR] [-s] [-S] [interval] ] -a (adapter status) Lists the remote machine's name table given its name -A (Adapter status) Lists the remote machine's name table given its -c (cache) Lists NBT's cache of remote [machine] names and their IP addresses -n (names) Lists local NetBIOS names. -R (Reload) Purges and reloads the remote cache name table23.8 ipcalc
- Input forms: ADDR/prefix, ADDR NETMASK, a wildcard mask (0.0.0.63), or a range ADDR1 - ADDR2 (deaggregated into CIDR blocks).
- Output: network, HostMin, HostMax, broadcast, Hosts/Net and the class; -b hides the binary, -n no colour.
- -s 100 50 20 splits a network by host counts; -c prints only the bit count.
Terminal
ipcalc -n 192.168.43.10/24ipcalc -n -b 192.168.43.10 255.255.255.128ipcalc -n -b 192.168.43.10 0.0.0.63ipcalc -n -b 192.168.43.1 - 192.168.43.100Output
$ ipcalc -n 192.168.43.10/24Address: 192.168.43.10 11000000.10101000.00101011. 00001010Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000Wildcard: 0.0.0.255 00000000.00000000.00000000. 11111111=>Network: 192.168.43.0/24 11000000.10101000.00101011. 00000000HostMin: 192.168.43.1 11000000.10101000.00101011. 00000001HostMax: 192.168.43.254 11000000.10101000.00101011. 11111110Broadcast: 192.168.43.255 11000000.10101000.00101011. 11111111Hosts/Net: 254 Class C, Private InternetOutput
$ ipcalc -n -b 192.168.43.10 255.255.255.128Netmask: 255.255.255.128 = 25Network: 192.168.43.0/25HostMin: 192.168.43.1HostMax: 192.168.43.126Broadcast: 192.168.43.127Hosts/Net: 126 Class C, Private Internet$ ipcalc -n -b 192.168.43.10 0.0.0.63WILDCARDNetmask: 255.255.255.192 = 26Wildcard: 0.0.0.63Network: 192.168.43.0/26HostMax: 192.168.43.62Broadcast: 192.168.43.63Hosts/Net: 62 Class C, Private Internet$ ipcalc -n -b 192.168.43.1 - 192.168.43.100deaggregate 192.168.43.1 - 192.168.43.100192.168.43.1/32192.168.43.2/31192.168.43.4/30192.168.43.8/29192.168.43.16/28192.168.43.32/27192.168.43.64/27192.168.43.96/30192.168.43.100/3223.9 Common mistakes
- Thinking the MAC from getmac travels over the internet: a MAC is local-link only; the router rewrites the frame with its own MAC, so a website sees your public IP, never your laptop MAC.
- Changing the name with plain hostname and losing it at reboot.
- Forgetting /etc/hosts after renaming: sudo and some apps then cannot resolve the name.
- Counting all 64 addresses of a /26 as hosts: network and broadcast are not usable (62).
23.10 Interview tips
- Permanent Linux rename: sudo hostnamectl hostname NEW (static + transient, survives reboot), then update /etc/hosts.
- hostname -I = interface IPs without DNS; -A = reverse lookups.
- 192.168.43.10/26: network 192.168.43.0, broadcast 192.168.43.63, 64 - 2 = 62 usable hosts.
- MAC addresses are rewritten at every router hop; only IP (and NAT) reach the far end.