步骤一:修改主机名配置文件
1)确认修改前的配置
[root@svr5 ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=svr5.tarena.com
2)使用sed修改主机名配置所在行的内容(c整行替换)
[root@svr5 ~]# sed -i \
'/^HOSTNAME/cHOSTNAME=mysvr.tarena.com' /etc/sysconfig/network
3)确认修改结果
[root@svr5 ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=mysvr.tarena.com
4)恢复原有配置
[root@svr5 ~]# sed -i \
'/^HOSTNAME/cHOSTNAME=svr5.tarena.com' /etc/sysconfig/network
[root@svr5 ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=svr5.tarena.com
步骤二:修改hosts文件,添加新的记录
1)确认修改前的配置
[root@svr5 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
2)使用sed修改hosts文件,添加两行新纪录(a追加)
[root@svr5 ~]# sed -i '$a192.168.4.5 svr5.tarena.com svr5\
> 119.75.217.56 www.baidu.com' /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.4.5 svr5.tarena.com svr5
119.75.217.56 www.baidu.com
3)确认修改结果
[root@svr5 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.4.5 svr5.tarena.com svr5
119.75.217.56 www.baidu.com