Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Each time I change my IP address I have to go edit /etc/hosts and change the line:

myPc-Vaio  192.168.1.x

I need this change for some programs I am using, so I can't just set the line to 127.0.0.1.

Is there some automation to change that line on each network change?

edit. I am on kubuntu 13.10 and I am using dhcp servers with ubuntu's default dhclient to obtain new ip addresses (default configuration).

share|improve this question
1  
What unix distro are you using? –  Jenny D Apr 3 at 8:06
    
And what's changing your Ip address, are you changing it manually, or is it DHCP and if so, which DHCP client? –  EightBitTony Apr 3 at 8:26
    
edited main question –  Phate Apr 3 at 8:39

1 Answer 1

If you using DHCP, you can trigger the ip address change using 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.

You can write a script to obtains new IP address and replace the /etc/hosts file. Then put it in /etc/dhcp/dhclient-exit-hooks.d/.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.