2022-05-20 21:19:06 +02:00
|
|
|
---
|
2022-12-16 11:46:32 +01:00
|
|
|
title: "DHCP Server and Routing"
|
2022-11-19 15:25:20 +01:00
|
|
|
visible: true
|
2022-05-20 21:19:06 +02:00
|
|
|
---
|
|
|
|
|
2022-06-06 18:36:45 +02:00
|
|
|
[toc]
|
2022-12-16 11:46:32 +01:00
|
|
|
|
2022-05-20 21:19:06 +02:00
|
|
|
## Installation
|
2022-12-16 11:46:32 +01:00
|
|
|
|
2023-02-19 15:21:41 +01:00
|
|
|
```sh
|
|
|
|
apt install isc-dhcp-server
|
|
|
|
```
|
2022-05-20 21:19:06 +02:00
|
|
|
|
|
|
|
## Configuration
|
2022-12-16 11:46:32 +01:00
|
|
|
|
2023-02-19 15:21:41 +01:00
|
|
|
Edit `/etc/default/isc-dhcp-server`
|
|
|
|
|
|
|
|
```
|
|
|
|
INTERFACESv4="{INTERFACE 1} {INTERFACE 2}"
|
|
|
|
```
|
2022-12-16 11:46:32 +01:00
|
|
|
|
|
|
|
Edit `/etc/dhcp/dhcpd.conf` to set a subnet
|
2022-05-20 21:19:06 +02:00
|
|
|
|
|
|
|
```
|
2023-02-19 15:21:41 +01:00
|
|
|
subnet {NETADDRESS} netmask {SUBNETMASK} {
|
|
|
|
range {FIRST DHCP} {LAST DHCP};
|
|
|
|
option subnet-mask {SUBNETMASK};
|
|
|
|
option routers {GATEWAY};
|
|
|
|
option domain-name "{NAME}";
|
|
|
|
option domain-name-servers {DNS SERVER};
|
2022-05-20 21:19:06 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-12-16 11:46:32 +01:00
|
|
|
Edit `/etc/network/interfaces`
|
|
|
|
|
2022-05-20 21:19:06 +02:00
|
|
|
```
|
2023-02-19 15:21:41 +01:00
|
|
|
auto {INTERFACE}
|
|
|
|
iface {INTERFACE} inet static
|
|
|
|
address {ADDRESS}
|
|
|
|
network {NETADDRESS}
|
|
|
|
netmask {NETMASK}
|
|
|
|
broadcast {BROADCAST}
|
2022-05-20 21:19:06 +02:00
|
|
|
```
|
|
|
|
|
2023-02-19 15:21:41 +01:00
|
|
|
Enable the interface
|
2022-05-20 21:19:06 +02:00
|
|
|
|
2023-02-19 15:21:41 +01:00
|
|
|
```sh
|
|
|
|
ifup {INTERFACE}
|
|
|
|
```
|
|
|
|
|
|
|
|
Restart DHCP Server
|
|
|
|
|
|
|
|
```sh
|
|
|
|
systemctl restart isc-dhcp-server.service
|
|
|
|
```
|
2022-05-20 21:19:06 +02:00
|
|
|
|
|
|
|
### Enable routing
|
2022-12-16 11:46:32 +01:00
|
|
|
|
2023-02-19 15:21:41 +01:00
|
|
|
```sh
|
|
|
|
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.d/80-forwarding.conf
|
|
|
|
sysctl -p /etc/sysctl.d/80-forwarding.conf
|
|
|
|
```
|
2022-05-20 21:19:06 +02:00
|
|
|
|
2023-02-19 15:21:41 +01:00
|
|
|
```sh
|
|
|
|
iptables -t nat -A POSTROUTING -o (WAN interface) -j MASQUERADE
|
|
|
|
iptables -A FORWARD -i (LAN interface) -j ACCEPT
|
|
|
|
```
|
2022-05-20 21:19:06 +02:00
|
|
|
|
|
|
|
Make iptables permanent
|
2023-02-19 15:21:41 +01:00
|
|
|
Select `Yes` during the installation to save current rules
|
|
|
|
|
|
|
|
```sh
|
|
|
|
apt install iptables-persistent
|
|
|
|
```
|
2022-05-20 21:19:06 +02:00
|
|
|
|
|
|
|
### Enable DHCP-managed fixed IP address
|
2022-12-16 11:46:32 +01:00
|
|
|
|
2022-05-20 21:19:06 +02:00
|
|
|
```
|
|
|
|
host (hostname) {
|
|
|
|
hardware ethernet (mac);
|
|
|
|
fixed-address (ip address);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Dynamic DNS
|
2022-12-16 11:46:32 +01:00
|
|
|
|
|
|
|
_Needs a supported DNS like BIND or PowerDNS_
|
2022-05-20 21:19:06 +02:00
|
|
|
[Configure your DNS server to accept updates](https://wiki.realstickman.net/e/en/linux/services/powerdns)
|
|
|
|
Add the following snippet to your `/etc/dhcp/dhcpd.conf` file
|
2022-12-16 11:46:32 +01:00
|
|
|
How to generate the key is also described in the DNS article
|
2022-05-20 21:19:06 +02:00
|
|
|
|
2022-12-16 11:46:32 +01:00
|
|
|
```
|
2022-05-20 21:19:06 +02:00
|
|
|
ddns-updates on;
|
|
|
|
ddns-update-style interim;
|
|
|
|
update-static-leases on;
|
|
|
|
|
|
|
|
ddns-domainname "testpdns";
|
|
|
|
ddns-rev-domainname "in-addr.arpa.";
|
|
|
|
|
|
|
|
key "(keyname)" {
|
|
|
|
algorithm hmac-md5;
|
|
|
|
secret "(key)";
|
|
|
|
};
|
|
|
|
|
|
|
|
zone testpdns {
|
|
|
|
primary 127.0.0.1;
|
|
|
|
key (keyname);
|
|
|
|
}
|
|
|
|
|
|
|
|
zone 7.168.192.in-addr.arpa. {
|
|
|
|
primary 127.0.0.1;
|
|
|
|
key (keyname);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Client
|
2022-12-16 11:46:32 +01:00
|
|
|
|
2023-02-19 15:21:41 +01:00
|
|
|
DHCP Request
|
2022-05-20 21:19:06 +02:00
|
|
|
|
2023-02-19 15:21:41 +01:00
|
|
|
```sh
|
|
|
|
dhclient -v
|
|
|
|
```
|
|
|
|
|
|
|
|
Release IP
|
|
|
|
|
|
|
|
```sh
|
|
|
|
# dhclient -v -r
|
|
|
|
```
|