linux下使用sed修改hosts表

linux下使用sed修改hosts表在每行后添加主机名

需求:
需要在hosts表每行后面添加本机的主机名,因为有时运行程序会出现unknow host的报错

解决办法:

[yukw@mysql2 ~]$ sudo -s
[sudo] password for yukw:
[root@mysql2 yukw]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@mysql2 yukw]# sed 's/$/ '$HOSTNAME'/g' /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 mysql2
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 mysql2
[root@mysql2 yukw]# sed -i 's/$/ '$HOSTNAME'/g' /etc/hosts
[root@mysql2 yukw]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 mysql2
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 mysql2

好了,这就是linux下使用sed修改hosts表在每行后添加主机名的方法了,如有问题可与博主一起交流讨论!

你可能感兴趣的:(linux,sed,linux,sed,hosts)