Skip to content

Instantly share code, notes, and snippets.

@mohamadaliakbari
Last active November 6, 2023 19:33
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mohamadaliakbari/1cb9400984094541581fff07143e1c9d to your computer and use it in GitHub Desktop.
Save mohamadaliakbari/1cb9400984094541581fff07143e1c9d to your computer and use it in GitHub Desktop.
Run dhclient on Startup in Ubuntu 18.04

dhclient is the Dynamic Host Configuration Protocol (DHCP) Client one would use to allow a client to connect to a DHCP server.

$ sudo nano /etc/rc.local

#!/bin/bash
dhclient
exit 0

$ sudo chmod 755 /etc/rc.local

$ sudo systemctl enable rc-local

$ sudo systemctl restart rc-local

$ sudo systemctl status rc-local

@typelogic
Copy link

Thanks. This trick solved my Ubuntu 18.04.2 LTS server. Instead of having to manually do each time, ip link set enp0s8 up && dhclient enp0s8. Must fill-in also necessary 2 lines in /etc/network/interfaces

@dcamarena
Copy link

Thanks. Even if I use static ip for my system, it solved my issue in getting the enterprise DNS servers from the DHCP Server.

@SolarGranola
Copy link

Changed my life. 👍

@melvinga
Copy link

Thank you for helping me solve my issue: Ubuntu 18.04.4 LTS connected to home router, but not connecting to the internet unless 'sudo dhclient' is run after bootup.

@p1-ra
Copy link

p1-ra commented Feb 26, 2020

Ubuntu 18.04 (server) is using systemd-networkd as default, if the service is enable systemctl status systemd-networkd, the proper way to enable dhcp is to use the systemd network configuration files (/etc/systemd/network/*)
https://www.freedesktop.org/software/systemd/man/systemd.network.html

As exemple, the following configuration file will enable DHCP to any nic that have a name that's matching enp*

cat /etc/systemd/network/20-dhcp.network
[Match]
Name=enp*

[Network]
DHCP=ipv4

@SolarGranola
Copy link

Ubuntu 18.04 (server) is using systemd-networkd as default, if the service is enable systemctl status systemd-networkd, the proper way to enable dhcp is to use the systemd network configuration files (/etc/systemd/network/*)
https://www.freedesktop.org/software/systemd/man/systemd.network.html

As exemple, the following configuration file will enable DHCP to any nic that have a name that's matching enp*

cat /etc/systemd/network/20-dhcp.network
[Match]
Name=enp*

[Network]
DHCP=ipv4

How would you do this in Ubuntu 16.04? This is what systemctl status returns for me:

systemd-networkd.service - Network Service
   Loaded: loaded (/lib/systemd/system/systemd-networkd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
   Docs: man:systemd-networkd.service(8)

@p1-ra
Copy link

p1-ra commented Mar 4, 2020 via email

@andrewroth
Copy link

Nobody seems to mention how ridiculous it is that ubuntu doesn't "just work" with auto dhcp like it used to. Odd.

@wilkinsmr
Copy link

Making a rc.local file to run dhclient is not the correct solution. You will most likely end-up with multiple network renders doing DHCP client operations and cause problems with future upgrades.

Ubuntu switched to netplan in release 17.10 (see Migrating to Netplan) which by default uses

  • NetworkManager for Ubuntu Desktop
  • systemd-networkd for Ubuntu Server

You can change the network "renderer" (networkd or NetworkManager) for any/all interfaces in the yaml config files under /etc/netplan (see netplan reference). You can also configure DHCP client options in netplan, but @p1-ra provided a better approach for cases using systemd-networkd which gives you more control (see Network Configuration for systemd.networkd)

In cases where NetworkManager is running, there is no need to abandon it, it's actually pretty good these days (unlike the old days when the first thing we did on a new install was to disable it) and it does DHCP client by default! However, OOTB it runs its own internal DHCP client which ignores /etc/dhcp/dhclient.conf and any hooks you may have added.
If you want NetworkManager to run dhclient instead, just edit /etc/NetworkManager/NetworkManager.conf and add a new line for "dhcp=dhclient" under the main section.

/etc/NetworkManager/NetworkManager.conf

[main]
dhcp=dhclient

(see NetworkManager.conf for details and other options)

When NetworkManager is restarted with systemctl restart NetworkManager it merges /etc/dhcp/dhclient.conf with its own interface specific configuration under /var/lib/NetworkManager. You can see dhclient running and the locations for conf, leases, etc. in your process list e.g.

ps -fC dhclient
	/sbin/dhclient -d -q \
		-sf /usr/lib/NetworkManager/nm-dhcp-helper \
		-pf /run/NetworkManager/dhclient-wlp58s0.pid \
		-lf /var/lib/NetworkManager/dhclient-02d2ec1b-e3f4-40f4-abbc-679afea5041b-wlp58s0.lease \
		-cf /var/lib/NetworkManager/dhclient-wlp58s0.conf wlp58s0
	/sbin/dhclient -d -q -6 -N \
		-sf /usr/lib/NetworkManager/nm-dhcp-helper \
		-pf /run/NetworkManager/dhclient6-wlp58s0.pid \
		-lf /var/lib/NetworkManager/dhclient6-02d2ec1b-e3f4-40f4-abbc-679afea5041b-wlp58s0.lease \
		-cf /var/lib/NetworkManager/dhclient6-wlp58s0.conf wlp58s0

(wlp58s0 in this example is my wifi interface)

@ttbek
Copy link

ttbek commented Jun 11, 2020

This is inane complexity for the sake of complexity. No one needs a "network renderer" We need straightforward configuration for the things we want. Not 20 different "network rendering" components all throwing a fit because you did something outside of their auspices. Even Avahi decided it was a good idea to mess with my bridge configuration. I'm pretty sure I didn't even choose to install it, it was a nasty gift that came with my upgrade to 20.04.
But yes, rc.local is probably not the place to do this. Should probably create a systemd unit for it once you finally get all the interfering stuff out of the way.
Look, probably if I were starting from scratch it would be easy to just make the Netplan config, but I'm not starting from scratch, I'm starting with a complicated manually created network and it would be working perfectly fine if this newly installed insanity didn't keep butting in.

@ChicagoJay
Copy link

I had this same problem with Ubuntu Server 20.04 LTS. The fix works, so you are a deity, and you have my sincerest gratitude - but WHY do I need it? I have other VMs that are running Ubuntu 20.04, that don't need this fix. They needed IPv6 turned off, to avoid the 2 minute boot delay, but that was it. DHCPv4 works fine.

Thanks!

@mark-e-deyoung
Copy link

mark-e-deyoung commented Dec 11, 2020

Helped me get DHCP working for IP over InfiniBand (ipoib) interfaces in a MAAS cluster. The DHCP process in systemd-networkd used by netplan doesn't seem to work with ipoib while dhclient does.

See: netplan not sending DHCP request of IPoIB interfaces.

@ac101m
Copy link

ac101m commented Mar 14, 2021

I have a bunch of machines running 16.04 (which will be going EOL in a month and a half) that need to be upgraded.
I have run into this issue with ipoib on another machine running 20.04 and am now dreading the upgrade.

In all the years since I started using linux, I never encountered a situation where /etc/network/interfaces either bugged out or wasn't capable of doing what I needed it to. I just don't understand why this needed to be changed! Its things like this that give me second thoughts about ubuntu.

@mickaelbaron
Copy link

Hi,

In my case, DHCP server was provided by a Windows Server. To fix the problem, you need to apply this workaround (https://netplan.io/examples/#integration-with-a-windows-dhcp-server).

Details with Ubuntu 20.04 LTS

  • Edit /etc/netplan/00-installer-config.yaml
  • Add dhcp-identifier key into current network configuration
network:
  ethernets:
    eth0:
      dhcp-identifier: mac
      dhcp4: true
  version: 2
  • Restart Netplan configuration
$ sudo netplan generate
$ sudo netplan apply

@leeanh2612
Copy link

Hi,

In my case, DHCP server was provided by a Windows Server. To fix the problem, you need to apply this workaround (https://netplan.io/examples/#integration-with-a-windows-dhcp-server).

Details with Ubuntu 20.04 LTS

  • Edit /etc/netplan/00-installer-config.yaml
  • Add dhcp-identifier key into current network configuration
network:
  ethernets:
    eth0:
      dhcp-identifier: mac
      dhcp4: true
  version: 2
  • Restart Netplan configuration
$ sudo netplan generate
$ sudo netplan apply

Thanks man you are boss <3

@jialeif
Copy link

jialeif commented Oct 26, 2022

[Match]
Name=enp*

[Network]
DHCP=ipv4

Great helps

@gbc921
Copy link

gbc921 commented Sep 19, 2023

Ubuntu 18.04 (server) is using systemd-networkd as default, if the service is enable systemctl status systemd-networkd, the proper way to enable dhcp is to use the systemd network configuration files (/etc/systemd/network/*) https://www.freedesktop.org/software/systemd/man/systemd.network.html

As exemple, the following configuration file will enable DHCP to any nic that have a name that's matching enp*

cat /etc/systemd/network/20-dhcp.network
[Match]
Name=enp*

[Network]
DHCP=ipv4

Awesome!
Had to do that for the Cloud Image of Ubuntu Server 22.04 LTS (Jammy)

Then later sudo systemctl daemon-reload && sudo systemctl enable systemd-networkd --now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment