linux gethostbyname 返回null

程序部署到客户端机器上不能运行,debug发现是gethostbyname 返回null,导致获取不到本机IP。

经查是因为修改机器名称后没有修改/etc/hosts文件

查看机器名如下:

$ cat /etc/hostname 

abcd123


查看/etc/hosts找不到abcd123对应的iP地址

$ cat /etc/hosts
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes

ff02::2 ip6-allrouters

解决办法:

为abcd123指定一个IP地址,如下

$ cat /etc/hosts

127.0.0.1 localhost
127.0.1.1 abcd123
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

猜你喜欢

转载自blog.csdn.net/wangcm04/article/details/80285056