动态更改/etc/hosts

动态更改/etc/hosts

每次更改 IP 地址时,我都必须编辑/etc/hosts并更改以下行:

myPc-Vaio  192.168.1.x

我需要对我正在使用的某些程序进行此更改,因此我不能只将该行设置为127.0.0.1.

是否有一些自动化功能可以在每次网络更改时更改该线路?

编辑。我使用的是 kubuntu 13.10,我使用 dhcp 服务器和 ubuntu 的默认 dhclient 来获取新的 IP 地址(默认配置)。

答案1

如果您使用DHCP,您可以使用以下命令触发 IP 地址更改dhclient-script

NAME

       dhclient-script - DHCP client network configuration script

DESCRIPTION

       The  DHCP  client  network configuration script is invoked from time to
       time by dhclient(8).  This script is used by the  dhcp  client  to  set
       each  interface's initial configuration prior to requesting an address,
       to test  the  address  once  it  has  been  offered,  and  to  set  the
       interface's  final configuration once a lease has been acquired.  If no
       lease is acquired, the script is used to  test  predefined  leases,  if
       any, and also called once if no valid lease can be identified.

       This  script  is  not meant to be customized by the end user.  If local
       customizations are needed, they should be possible using the enter  and
       exit  hooks  provided (see HOOKS for details).   These hooks will allow
       the user to override the default behaviour of the client in creating  a
       /etc/resolv.conf file.

       No  standard  client  script  exists  for  some operating systems, even
       though the actual client may work, so a pioneering user may  well  need
       to  create  a  new  script  or  modify  an  existing  one.  In general,
       customizations specific to a particular computer should be done in  the
       /etc/dhcp3/dhclient.conf file.   If you find that you can't make such a
       customization without customizing /etc/dhcp3/dhclient.conf or using the
       enter and exit hooks, please submit a bug report.

您可以编写脚本来获取新的IP地址并替换该/etc/hosts文件。然后把它放进去/etc/dhcp/dhclient-exit-hooks.d/

相关内容