How to conect an Ubuntu system to a hardware device using DHCP

xiangyu fu Lv3

Step 1: Connect the Ethernet Cable

  • Plug one end of the Ethernet cable into your Ubuntu machine's Ethernet port and the other end into the hardware device.

Step 2: Configure Ethernet Interface on Ubuntu

  1. Open Network Settings:

    • On Ubuntu, you can use the graphical interface or the terminal to configure network settings. For simplicity, we'll use the terminal.
  2. Identify the Ethernet Interface:

    • First, you need to identify the name of your Ethernet interface. Open a terminal and run:
      1
      ip link

    For example, the output might look like this:

    1
    2
    3
    4
    5
    6
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2: wlp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DORMANT group default qlen 1000
    link/ether f4:5c:89:cd:15:29 brd ff:ff:ff:ff:ff:ff
    3: enx00e04c360297: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 00:e0:4c:36:02:97 brd ff:ff:ff:ff:ff:ff

    So in this case, the Ethernet interface is enx00e04c360297.

  3. Assign a Static IP Address to Your Ubuntu System (Optional):

    • If you want to assign a static IP to your Ubuntu system, you can do it by editing the Netplan configuration file. Open the Netplan configuration file with a text editor:

      1
      sudo nano /etc/netplan/01-netcfg.yaml

    • If the file does not exist, you may have a different configuration file under /etc/netplan/. Look for files with a .yaml extension.

    • Add or modify the following configuration, replacing eth0 with your interface name and the IP address with your desired static IP:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      network:
      version: 2
      renderer: networkd
      ethernets:
      eth0:
      dhcp4: no
      addresses:
      - 192.168.1.1/24
      gateway4: 192.168.1.254
      nameservers:
      addresses:
      - 8.8.8.8
      - 8.8.4.4

    • Save the file and apply the changes with:

      1
      sudo netplan apply

  4. Set Up DHCP Server on Ubuntu:

    • Install the DHCP server package:
      1
      2
      sudo apt-get update
      sudo apt-get install isc-dhcp-server
    • Once installed, configure the DHCP server by editing its configuration file:
      1
      sudo nano /etc/dhcp/dhcpd.conf
    • Add or modify the following settings to define the IP range and settings for your slave device:
      1
      2
      3
      4
      5
      6
      7
      8
      subnet 192.168.1.0 netmask 255.255.255.0 {
      range 192.168.1.100 192.168.1.200;
      option routers 192.168.1.1;
      option domain-name-servers 8.8.8.8, 8.8.4.4;
      option broadcast-address 192.168.1.255;
      default-lease-time 600;
      max-lease-time 7200;
      }
    • Make sure the INTERFACESv4 variable in /etc/default/isc-dhcp-server is set to your Ethernet interface (e.g., eth0 etc. ):
      1
      INTERFACESv4="enx00e04c360297"
  5. Start and Enable the DHCP Server:

    • Start the DHCP server:
      1
      sudo systemctl start isc-dhcp-server
    • Enable the DHCP server to start on boot:
      1
      sudo systemctl enable isc-dhcp-server

Step 3: Connect the Slave Device

  • With the DHCP server running on your Ubuntu machine, the connected hardware device should now be able to obtain an IP address automatically via DHCP when it is connected to the Ethernet interface.

Step 4: Verify the Connection

  1. Check the IP Address Assigned to the Slave Device:
    • You can verify the IP address assigned to the slave device by checking the DHCP leases file:
      1
      cat /var/lib/dhcp/dhcpd.leases
  2. Ping the Slave Device:
    • Once the IP address is assigned, you can ping the device to ensure the connection is working:
      1
      ping 192.168.1.x
    • Replace 192.168.1.x with the IP address of the slave device.

By following these steps, you can configure your Ubuntu machine to assign IP addresses to connected devices via DHCP over an Ethernet connection.

Troubleshooting Tips

if you encounter any issues, here are some troubleshooting tips:

  • Check the status of the DHCP server:

    1
    sudo systemctl status isc-dhcp-server
    The outputs will show if the DHCP server is running and any errors that may have occurred.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    ● isc-dhcp-server.service - ISC DHCP IPv4 server
    Loaded: loaded (/lib/systemd/system/isc-dhcp-server.service; enabled; vend>
    Active: active (running) since Fri 2024-08-30 14:09:52 CEST; 14min ago
    Docs: man:dhcpd(8)
    Main PID: 11841 (dhcpd)
    Tasks: 4 (limit: 9303)
    Memory: 5.5M
    CGroup: /system.slice/isc-dhcp-server.service
    └─11841 dhcpd -user dhcpd -group dhcpd -f -4 -pf /run/dhcp-server/>

    Aug 30 14:13:32 stan-MacBookPro dhcpd[11841]: DHCPACK on 192.168.1.100 to 00:80>
    Aug 30 14:18:32 stan-MacBookPro dhcpd[11841]: DHCPREQUEST for 192.168.1.100 fro>
    Aug 30 14:18:32 stan-MacBookPro dhcpd[11841]: DHCPACK on 192.168.1.100 to 00:80>
    Aug 30 14:18:53 stan-MacBookPro dhcpd[11841]: reuse_lease: lease age 21 (secs) >
    Aug 30 14:18:53 stan-MacBookPro dhcpd[11841]: DHCPDISCOVER from 00:80:e1:00:00:>
    Aug 30 14:18:53 stan-MacBookPro dhcpd[11841]: DHCPOFFER on 192.168.1.100 to 00:>
    Aug 30 14:18:53 stan-MacBookPro dhcpd[11841]: reuse_lease: lease age 21 (secs) >
    Aug 30 14:18:53 stan-MacBookPro dhcpd[11841]: DHCPREQUEST for 192.168.1.100 (19>
    Aug 30 14:18:53 stan-MacBookPro dhcpd[11841]: DHCPACK on 192.168.1.100 to 00:80>

  • Restart the DHCP server: If you make changes to the DHCP configuration file or encounter issues, you can restart the DHCP server:

    1
    sudo systemctl restart isc-dhcp-server
  • Title: How to conect an Ubuntu system to a hardware device using DHCP
  • Author: xiangyu fu
  • Created at : 2024-08-30 11:23:47
  • Updated at : 2024-09-10 20:59:51
  • Link: https://redefine.ohevan.com/2024/08/30/Reviews/local_dhcp/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments