M99*_*M99 12 linux ubuntu networking
问题:是否有一种简单的方法可以在系统启动时自动在/ etc/hosts中添加DHCP机器的DHCP发布的IP地址和主机名?
背景:我的Linux机器在/ etc/hostname中有一个主机名,当我ping时它不会解析为任何东西.我在/ etc/hosts中手动添加了我的主机名和IP地址,以便我的网络相关的java程序能够运行.
谢谢,
在Ubuntu中,将可执行文件添加到/etc/network/if-up.d目录中.网络管理器配置网络接口后,将执行此目录中的文件.
您可以调整以下脚本:
#!/bin/sh
set -e
if [ "$IFACE" = lo ]; then
exit 0
fi
myHostName=`hostname`
# Remove current line with hostname at the end of line ($ means end of line)
sed -i '/'$myHostName'$/ d' /etc/hosts
ipaddr=$(ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
echo "$ipaddr $myHostName" >>/etc/hosts
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35131 次 |
| 最近记录: |