Name or service not known

user1050619 picture user1050619 · Jul 7, 2016 · Viewed 7.9k times · Source

I have been trying to get the ipaddress of the person who logged into the machine using the below code but I get a error.

>>> import socket
>>> socket.gethostbyname_ex(socket.gethostname())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.gaierror: [Errno -2] Name or service not known

The same code works in other linux box. Not sure I fix it.

Answer

Yogesh Jilhawar picture Yogesh Jilhawar · Oct 25, 2017

Error has occurred just because of not setting up hostname properly. Set the hostname at three different places, which are in -

  1. /etc/hostname
  2. /etc/hosts
  3. run command $ hostname

then logout and login again. You are done.