您的位置:首页 > 运维架构 > Linux

到底如何修改linux主机名?

2010-07-31 16:00 253 查看
其实,对于linux主机来说呢,修改主机名是个非常基本的操作。

但我的青涩年代还是被这个问题困惑了下。

哎,成长总是要付出点代价的。对任何问题都要深入研究啊。

不发牢骚了,上“酸菜”:

修改主机名的三个步骤:

1. 利用hostname命令修改内存中的主机名

如:

#hostname mylinux

将主机名修改为mylinux。

用此命令修改的是内存中存在的主机名,执行此命令后,退出系统,在登录进来就可以看到修改后的主机名。

通过hostname 工具来设置主机名只是临时的,下次重启系统时,此主机名将不会存在;

显示主机IP:

# hostname -i

192.168.1.100

2.修改文件/etc/sysconfig/network

刚刚装好系统的linux主机此文件一般是这样的:

# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=localhost.localdomain

只要将HOSTNAME=localhost.localdomain 修改为HOSTNAME=mylinux,然后保存退出就好了。

linux系统启动时,就从此文件中读取主机名。

因此只用hostname命令修改主机名不改动此文件的话,等主机重启了,就会变回原来的主机名。

3. 修改/etc/hosts

/etc/hosts contains a list of IP addresses and the hostnames that they correspond to. In general, /etc/hosts only contains entries for your local machine, and perhaps other ``important'' machines (such as your nameserver or gateway). Your local name server will provide address-to-name mappings for other machines on the network, transparently.

For example, if your machine is loomer.vpizza.com with the IP address 128.253.154.32, your /etc/hosts would look like:

127.0.0.1               localhost
128.253.154.32          loomer.vpizza.com loomer


If you're only using loopback, the only line in /etc/hosts should be for

127.0.0.1, with both localhost and your hostname after it.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: