How to get the hostname of the node?

How to get the hostname of the node running the program?

asked Apr 1 by zma (2,200 points)
edited Apr 5 by zma

5 Answers

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.

Example:

$ echo $HOSTNAME
host01
$ hs=`hostname`
$ echo $hs
host01
answered Apr 4 by zma (2,200 points)

Another way:

cat /proc/sys/kernel/hostname
commented Apr 9 by zma (2,200 points)

In Python, you can get the hostname by the socket.gethostname() library function in the socket module:

import socket

hostname = socket.gethostname()

Example:

$ python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostname()
'host001'
answered Apr 5 by zma (2,200 points)
edited Apr 20 by zma

In C, you may use the gethostname function.

#include <unistd.h>

int gethostname(char *name, size_t namelen); 

The gethostname() function shall return the standard host name for the current machine. The namelen argument shall specify the size of the array pointed to by the name argument. The returned name shall be null-terminated, except that if namelen is an insufficient length to hold the host name, then the returned name shall be truncated and it is unspecified whether the returned name is null-terminated.

Host names are limited to {HOST_NAME_MAX} bytes.

One example:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>

int main(void) {
  char hostname[HOST_NAME_MAX + 1];
  gethostname(hostname, HOST_NAME_MAX + 1);

  printf("hostname: %s\n", hostname);

  return EXIT_SUCCESS;
}

Execution result:

$ gcc a.c -o a
$ ./a
hostname: host001
answered Apr 9 by zma (2,200 points)
edited Apr 9 by zma

In C++, the C way works too. However, with Boost, you can use the boost::asio::ip::host_name() function to get the hostname as a std::string:

namespace boost {
namespace asio {
namespace ip {

/// Get the current host name.
BOOST_ASIO_DECL std::string host_name();

...

More at http://www.boost.org/doc/libs/1_63_0/boost/asio/ip/host_name.hpp

One example:

The boost-host-name.cpp

#include <iostream>
#include <algorithm>
#include <boost/asio.hpp>

namespace ip = boost::asio::ip;

int main()
{
  boost::asio::io_service io_service;

  std::string h = ip::host_name();
  std::cout << "hostname: " << h << '\n';
}

Build and run it:

$ g++ boost-host-name.cpp -o host-name -lboost_system
$ ./host-name
hostname: host001
answered Apr 9 by zma (2,200 points)
edited Apr 12 by zma

In Go, you can use the os.Hostname() function to get the hostname of the node.

func Hostname() (name string, err error)

Hostname returns the host name reported by the kernel.

One example is as follows. The main.go source code:

package main

import (
	"fmt"
	"os"
)

func main() {
	name, err := os.Hostname()
	if err != nil {
		panic(err)
	}

	fmt.Println("hostname:", name)
}

Run it:

$ go run main.go
hostname: host001
answered Apr 9 by zma (2,200 points)

Please log in or register to answer this question.

Related questions

4 answers
7 answers
asked Mar 31 by zma (2,200 points)
5 answers
asked Mar 31 by zma (2,200 points)
Welcome to Do This In Various Langs (dtivl), where you can ask questions and receive solutions in various programming languages.
Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...