Linux 修改 hostname

背景

之前安装Linux系统的时候,没有明确指定。现在因为在做某些实验的时候,为了更好地区分我所登录的每一台服务器。

于是有了此文。

做法

首先修改/etc/hostname,修改为自己想要的名字xxx

同时,修改/etc/hosts,中第二行,否则在执行sudo的时候提示找不到host:

sudo: unable to resolve host xxx

修改以后的效果,假设修改为xxx

$ cat /etc/hosts
127.0.0.1	localhost
127.0.1.1	xxx

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

$ cat /etc/hostname
xxx

重启以后就可以生效了。

如果不方便重启,则切换到 root用户以后,执行(xxx为自己想改的名字):

echo "xxx"> /proc/sys/kernel/hostname

重新登录即可(或者重新执行shell)。

posted @ 2021-01-19 22:11  schips  阅读(459)  评论(0编辑  收藏  举报