Is it possible to use Ifconfig to switch an interface to DHCP and if not, what is the best way?
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
Related videos on Youtube
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, 2022Comments
-
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 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 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 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 about 12 yearsOh... I see.. so you are just looking for an easy way to toggle it...
-
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 about 12 yearsyou might need an ip route add as well
-
3bdalla about 9 yearsExactly what I need, thank you. But why to provide 0.0.0.0 ? dhclient alone worked for me.
-
RobotHumans about 9 yearsIt'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 over 8 yearsThe
dhclient
should have the interface after it, too, because when the device afterifconfig
isn'teth0
, you need to specify it. -
logidelic about 5 yearsIf
dhclient
is already running, do we need to run it again? -
RobotHumans about 5 yearsYes, you do. It doesn't dynamically pick up new interfaces.
-
logidelic about 5 yearsThank 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 about 5 yearsYou don't HAVE to, but it might be a good idea to run it explicitly per interface in the many interfaces case.