Linux修改主机名

关键位置:

/etc/hosts /etc/sysconfig/network

/etc/hostname

 

修改主机名新方法(未测试):hostnamectl set-hostname 新主机名

 

腾讯云Centos7服务器修改/etc/hosts

第一次在腾讯云购买了服务器,然后我需要修改/etc/hosts文件测试安装iRedmail邮件服务器,一打开/etc/hosts文件便如以下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

# Your system has configured 'manage_etc_hosts' as True.

# As a result, if you wish for changes to this file to persist

# then you will need to either

# a.) make changes to the master file in /etc/cloud/templates/hosts.redhat.tmpl

# b.) change or remove the value of 'manage_etc_hosts' in

#     /etc/cloud/cloud.cfg or cloud-config from user-data

# The following lines are desirable for IPv4 capable hosts

127.0.0.1 VM_0_12_centos VM_0_12_centos

127.0.0.1 localhost.localdomain localhost

127.0.0.1 localhost4.localdomain4 localhost4

 

# The following lines are desirable for IPv6 capable hosts

::1 VM_0_12_centos VM_0_12_centos

::1 localhost.localdomain localhost

::1 localhost6.localdomain6 localhost6

 

修改方式和常规的直接修改方式的不一样,直接对/etc/hosts文件进行修改,比如直接添加 127.0.0.1  mail.chenxie.net 到/etc/hosts ,保存后虽然ping mail.chenxie.net可以通,但是iRedmail的安装检测脚本通不过,而且重启系统后添加的记录就消失了。

 

文件注释说,我的系统配置了manage_etc_hosts为True,如果要修改hosts的话

a. 需要先对/etc/cloud/templates/hosts.redhat.tmpl进行修改,

b. 然后修改或删除manage_etc_hosts的值在/etc/cloud/cloud.cfg文件中。

 

第一步,修改/etc/cloud/templates/hosts.redhat.tmpl文件:

直接添加你要在/etc/hosts文件中出现的文件内容:

127.0.0.1 mail.chenxie.net

 

关于第二步,修改manage_etc_hosts的值,在/etc/cloud/cloud.cfg文件中没有找到,那就忘掉吧。

 

最后reboot重启你的机器生效。

 

说明:安装iRadMail还要修改主机名,本次没有仔细看文档也吃了点亏,修改/etc/hostname,然后reboot生效。

 

修改主机名(/etc/hostname和/etc/hosts区别)

ubuntu永久修改主机名

 

1、查看主机名

 

在Ubuntu系统中,快速查看主机名有多种方法:

其一,打开一个GNOME终端窗口,在命令提示符中可以看到主机名,主机名通常位于“@”符号后;

其二,在终端窗口中输入命令:hostname或uname –n,均可以查看到当前主机的主机名。

 

2、临时修改主机名

 

命令行下运行命令:“hostname 新主机名”

其中“新主机名”可以用任何合法字符串来表示。不过采用这种方式,新主机名并不保存在系统中,重启系统后主机名将恢复为原先的主机名称。

例子:hostname ubuntu-temp

这样主机名字就临时被修改为ubuntu-temp,但是终端下不会立即显示生效后的主机名,重开一个终端窗口(通过ssh连接的终端需要重新连接才可以);

 

 

3、永久修改主机名

 

在Ubuntu系统中永久修改主机名也比较简单。主机名存放在/etc/hostname文件中,修改主机名时,编辑hostname文件,在文件中输入新的主机名并保存该文件即可。重启系统后,参照上面介绍的快速查看主机名的办法来确认主机名有没有修改成功。

 

值的指出的是,在其它Linux发行版中,并非都存在/etc/hostname文件。如Fedora发行版将主机名存放在/etc/sysconfig/network文件中。所以,修改主机名时应注意区分是哪种Linux发行版。

 

3、/etc/hostname与/etc/hosts的区别

/etc/hostname中存放的是主机名,hostname文件的一个例子:

v-jiwan-ubuntu-temp

 

/etc/hosts存放的是域名与ip的对应关系,域名与主机名没有任何关系,你可以为任何一个IP指定任意一个名字,hostname文件的一个例子:

127.0.0.1       localhost

127.0.1.1       v-jiwan-ubuntu

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值