How to get the hostname of the node in Bash?

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

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

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

2 comments:

Leave a Reply

Your email address will not be published. Required fields are marked *