How to get the hostname of the node in Bash?

In Bash, how to get the hostname of the node running the program?

asked May 29, 2017 by dtivl (2,500 points)

1 Answer

In Bash, you can get the hostname of the node in at least 2 ways:

  • Use the variable $HOSTNAME.
  • Get the hostname by command hostname.
  • Get the content of /proc/sys/kernel/hostname

Example:

$ echo $HOSTNAME
host01
$ hs=`hostname`
$ echo $hs
host01
$ cat /proc/sys/kernel/hostname
host01
answered May 29, 2017 by dtivl (2,500 points)

Please log in or register to answer this question.

Related questions

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...