linux中如何使/etc/hosts文件中的IP地址自动更新为当前系统的IP

来源:百度知道 编辑:UC知道 时间:2024/05/15 11:27:23
linux系统中我的网络使用的是DHCP自动分配IP地址, 如何使/etc/hosts文件中主机名对应的IP地址自动更新为当前系统的IP,如果每次都自己来改太麻烦了?
就是说我的hosts文件开始是这样的
127.0.0.1 localhost
127.0.1.1 ubuntu-desktop
我每次开机IP地址都不一样,我怎么把这里的127.0.1.1换成用ifconfig命令看到的IP地址,因为别人要用http://ubuntu-desktop:8080/download/来取rmi时的存根类文件
这个文件不会自己把127.0.1.1改成我的IP啊?

1.首先在Linux系统下设置静态的IP地址

vim /etc/network/interfaces #编辑网网卡配置文件
auto lo
iface lo inet loopback
auto eth0 #开机自动连接网络
iface eth0 inet static #static表示使用固定ip,dhcp表述使用动态ip
address 192.168.21.168 #设置ip地址
netmask 255.255.255.0 #设置子网掩码
gateway 192.168.21.2 #设置网关
ctrl+o #保存配置
ctrl+x #退出

2.在/etc/profile 加入脚本

vim /etc/profilegrep -v "hostname(指定的域名)" /etc/hosts > ~/hosts_temp 
cat ~/hosts_temp > /etc/hosts 
LC_ALL=C ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' |cut -d: -f2 | awk '{ print $1}' >> /etc/hosts

3.重启网络使设置生效

/etc/init.d/networking&nbs