hostnamex
Print the name of the current host system.
Mirrors GNU hostname. Run it as hostnamex, or as gnux hostnamex.
Synopsis
hostnamex [NAME]GNU reference
Description
hostnamex prints the name of the current host, as the operating system reports it. GNU coreutils hostname also sets the name when given NAME (with sethostname(2), as root); gnux never changes system settings, so hostnamex refuses NAME with 'cannot set name to NAME: Operation not supported' and exits 1. Use the system's own tool (hostnamectl, scutil, Rename-Computer) to set it. Note that the hostname program shipped by many Linux distributions is not the coreutils one and has options such as -f and -I; hostnamex has none of them.
Permissions
Needs no permissions to print the name.
Use cases
- Recording which machine produced a log line or a build artifact.
- Naming per-host files or directories.
- Branching a script on the machine it runs on.
- Checking a container's or a CI runner's host name.
- Getting the host name the same way on Linux, macOS and Windows.
Flags
--help display this help and exit
--version output version information and exitExamples
Print the host name
hostnamexTag a log line with the host
echox "$(hostnamex): backup done" >> backup.logName an archive after the machine
cpx backup.tar "backup-$(hostnamex).tar"Run a step only on one machine
[ "$(hostnamex)" = build-01 ] && make releasePrint the user and the host together
echox "$(whoamix)@$(hostnamex)"Compare with the network node name unamex reports
hostnamex; unamex -nSetting the name is refused (exit status 1)
hostnamex new-name || echox 'use hostnamectl instead'Print the host name in upper case
hostnamex | trx a-z A-ZCreate a per-host directory
mkdirx -p "logs/$(hostnamex)"Print the host name in the portable gnux shell
gnux shell -c 'hostnamex'Exit status
| Status | Meaning |
|---|---|
| 0 | success: every operand was processed |
| 1 | failure: an operand could not be processed (diagnosed on standard error), or a command-line mistake |
Help in the terminal
hostnamex --help # options, first examples, exit status
gnux help hostnamex # this page: the full manual
gnux help hostnamex --OPTION # one option in depth, with its examples