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.

  • @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. – wilhil Apr 23 '12 at 14:02
  • 1
    @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. – wilhil Apr 23 '12 at 14:10
  • @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. – wilhil Apr 23 '12 at 20:41
  • 1
    Oh... I see.. so you are just looking for an easy way to toggle it... – TheXed Apr 23 '12 at 20:42
  • 1
    "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. – wilhil Apr 23 '12 at 21:24

Change from static to dynamic

ifconfig eth0 && dhclient  

Change back

killall dhclient && ifconfig eth0 netmask
  • you might need an ip route add as well – RobotHumans Apr 23 '12 at 14:08
  • Exactly what I need, thank you. But why to provide ? dhclient alone worked for me. – 3bdalla Mar 8 '15 at 9:34
  • 1
    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" – RobotHumans Mar 8 '15 at 11:28
  • 3
    The dhclient should have the interface after it, too, because when the device after ifconfig isn't eth0, you need to specify it. – Jim Hunziker Dec 21 '15 at 17:20
  • If dhclient is already running, do we need to run it again? – logidelic Feb 13 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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