Linux sed 正则匹配 修改 /etc/hosts 对应域名解析的ip

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hknaruto/article/details/89307892

修改指令如下:

echo "127.0.0.1 www.hknaruto.com" >> /etc/hosts
sed -i 's/[0-9\.]\+[ ]\+www.hknaruto.com/127.0.0.2 www.hknaruto.com/g' /etc/hosts

说明:

1. 先写入一条记录用于匹配(根据需要调整或直接不要)

2. 通过sed正则匹配,替换记录值

测试效果

ping -c 1  www.hknaruto.com
PING www.hknaruto.com (127.0.0.2) 56(84) bytes of data.
64 bytes from www.hknaruto.com (127.0.0.2): icmp_seq=1 ttl=64 time=0.023 ms

--- www.hknaruto.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.023/0.023/0.023/0.000 ms

猜你喜欢

转载自blog.csdn.net/hknaruto/article/details/89307892