Is it possible to use Ifconfig to switch an interface to DHCP and if not, what is the best way?

131,141

Change from static to dynamic

ifconfig eth0 0.0.0.0 0.0.0.0 && dhclient  

Change back

killall dhclient && ifconfig eth0 10.0.1.22 netmask 255.255.255.0
Share:
131,141

Related videos on Youtube

wilhil
Author by

wilhil

Currently looking for an IT technican / support / consultant job in the UK - If reading this, you probably already know my specialities! Please contact me for CV or details. Email - [email protected] Twitter - @wilhil

Updated on September 18, 2022

Comments

  • wilhil
    wilhil over 1 year

    I have just set up a new Ubuntu server and I am going to be doing a lot of networking tests.

    During setup, I simply skipped the network step.

    I now need to switch between DHCP and an address quite often.

    I know I can easily give a port an address via ifconfig eth0 x.x.x.x netmask x.x.x.x, but, is there an alternative that can be used in order to set a port to use DHCP?

    If this isn't possible, I am guessing I can default in /etc/network/interfaces to dhcp and then just use up/down or restart networking in order to apply... but, I would much rather use ifconfig.

    • Admin
      Admin about 12 years
      @TheX - I am just using Ubuntu Server, I am not actually "serving" anything! I need to switch between DHCP on one network and plugging on to another network where there is no DHCP server and there is a completely different range.
    • Admin
      Admin about 12 years
      @TheX - Sorry... This is for downloading data/tests from one network that does have DHCP enabled... and then switching over to another network where there isn't DHCP.
    • Admin
      Admin about 12 years
      @TheX - I'm sorry, I am not sure if there is something you haven't understood... I know exactly that this is the case! This is why I want to have a static IP and DHCP, and switch between them! In Windows, when testing networks, I just right click on the connection and put in an IP configuration and to get back to DHCP, I just tick the automatically assign box... I just want the Linux variation of that.
    • Admin
      Admin about 12 years
      Oh... I see.. so you are just looking for an easy way to toggle it...
    • Admin
      Admin about 12 years
      "TheX - Precisely! Sorry if I didn't explain, but, I didn't think it was that complicated! I can do this easily in GUI Linux which is very similar to Windows - I just can't figure out how to do it from the command line.
  • RobotHumans
    RobotHumans about 12 years
    you might need an ip route add as well
  • 3bdalla
    3bdalla about 9 years
    Exactly what I need, thank you. But why to provide 0.0.0.0 ? dhclient alone worked for me.
  • RobotHumans
    RobotHumans about 9 years
    It's a habit. So you're saying "unassign whatever address I currently have, then ask for an address" instead of just "ask for an address"
  • Jim Hunziker
    Jim Hunziker over 8 years
    The dhclient should have the interface after it, too, because when the device after ifconfig isn't eth0, you need to specify it.
  • logidelic
    logidelic about 5 years
    If dhclient is already running, do we need to run it again?
  • RobotHumans
    RobotHumans about 5 years
    Yes, you do. It doesn't dynamically pick up new interfaces.
  • logidelic
    logidelic about 5 years
    Thank you @RobotHumans . And for the flipside of the question: Do you need to kill it when going static? What if it's required for other interfaces that are still DHCP?
  • RobotHumans
    RobotHumans about 5 years
    You don't HAVE to, but it might be a good idea to run it explicitly per interface in the many interfaces case.