Ubuntu DHCP配置指南
Ubuntu DHCP Configuration Guide
1. Install the DHCP Server Package
To set up a DHCP server on Ubuntu, first install the isc-dhcp-server package, which provides the ISC DHCP server daemon. Run the following commands to update the package repository and install the software:
sudo apt update &
&
sudo apt install isc-dhcp-server -y
This installs the necessary binaries, configuration files, and dependencies for the DHCP server.
2. Configure the DHCP Server
The main configuration file for the ISC DHCP server is /etc/dhcp/dhcpd.conf. You need to define global options (applied to all subnets) and subnet-specific settings (IP ranges, gateways, DNS servers).
Global Options
Add these lines at the top of the file to set default values for all subnets:
default-lease-time 600;
# Default lease time (10 minutes)
max-lease-time 7200;
# Maximum lease time (2 hours)
option domain-name "example.com";
# Your domain name
option domain-name-servers 8.8.8.8, 8.8.4.4;
# Public DNS servers
authoritative;
# Declare this server as authoritative for the network
default-lease-time: Time (in seconds) a client can use an IP before renewal.max-lease-time: Maximum time a client can hold an IP (even if it requests a longer lease).authoritative: Ensures the server responds to all DHCP requests in its network (prevents rogue servers).
Subnet Configuration
Define a subnet block for your local network. Replace the example values with your actual network details (e.g., 192.168.1.0/24):
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.100 192.168.1.200;
# Available IP range for dynamic allocation
option routers 192.168.1.1;
# Default gateway (router) IP
option subnet-mask 255.255.255.0;
# Subnet mask for the network
}
range: Defines the pool of IPs the server can assign to clients.option routers: The gateway IP clients use to access other networks (e.g., the internet).option subnet-mask: Identifies the network portion of IP addresses.
3. Specify Listening Interfaces
The DHCP server must be configured to listen on the correct network interface (e.g., eth0, enp0s3). Edit the /etc/default/isc-dhcp-server file and modify the INTERFACESv4 line:
INTERFACESv4="eth0" # Replace 'eth0' with your actual interface name
To find your interface name, run ip a or ifconfig—look for the interface connected to your local network.
4. Start and Enable the DHCP Service
After configuring the files, start the DHCP server and enable it to launch at boot:
sudo systemctl start isc-dhcp-server # Start the service
sudo systemctl enable isc-dhcp-server # Enable auto-start on boot
Check the service status to ensure it’s running correctly:
sudo systemctl status isc-dhcp-server
A status of active (running) indicates the server is operational.
5. Verify DHCP Functionality
To confirm the server is assigning IPs, connect a client device (e.g., a laptop or another Ubuntu machine) to the same network and configure it to use DHCP:
- On Ubuntu clients: Edit
/etc/netplan/01-netcfg.yaml(or the appropriate Netplan file) and set the interface to use DHCP:Apply the changes withnetwork: version: 2 renderer: networkd ethernets: eth0: # Replace with your client interface name dhcp4: yessudo netplan apply. - Test IP assignment: On the client, run
ip addr show eth0(replaceeth0with your interface). Look for an IP address within the range specified in the server’ssubnetblock (e.g.,192.168.1.100–192.168.1.200).
6. Optional: Configure Static IP Leases
To assign a fixed IP to a specific device (e.g., a printer or server), add a host block to the /etc/dhcp/dhcpd.conf file. Use the device’s MAC address (find it with ip link show on the client) and desired static IP:
host printer1 {
hardware ethernet 00:1a:2b:3c:4d:5e;
# Replace with the client's MAC address
fixed-address 192.168.1.50;
# Static IP to assign
}
Save the file and restart the DHCP server to apply changes:
sudo systemctl restart isc-dhcp-server
7. Troubleshooting Tips
If clients aren’t receiving IPs, check the following:
- Firewall: Allow UDP ports 67 (DHCP server) and 68 (DHCP client) in your firewall:
sudo ufw allow 67/udp sudo ufw allow 68/udp - Configuration syntax: Validate the
dhcpd.conffile withsudo dhcpd -t(this checks for errors without starting the server). - Logs: View real-time logs to identify issues:
journalctl -u isc-dhcp-server.service -f - Interface mismatch: Ensure the
INTERFACESv4value in/etc/default/isc-dhcp-servermatches the interface connected to the network.
By following these steps, you can set up a functional DHCP server on Ubuntu to automate IP address allocation for your local network.
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Ubuntu DHCP配置指南
本文地址: https://pptw.com/jishu/742296.html
