Skip to Content
gnux

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 exit

Examples

hostnamex

Tag a log line with the host

echox "$(hostnamex): backup done" >> backup.log

Name an archive after the machine

cpx backup.tar "backup-$(hostnamex).tar"

Run a step only on one machine

[ "$(hostnamex)" = build-01 ] && make release
echox "$(whoamix)@$(hostnamex)"

Compare with the network node name unamex reports

hostnamex; unamex -n

Setting the name is refused (exit status 1)

hostnamex new-name || echox 'use hostnamectl instead'
hostnamex | trx a-z A-Z

Create a per-host directory

mkdirx -p "logs/$(hostnamex)"
gnux shell -c 'hostnamex'

Exit status

StatusMeaning
0success: every operand was processed
1failure: 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

See also

On this page