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

@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