wiki-grav/pages/02.linux/dhcp-server-and-routing/default.en.md

129 lines
2.0 KiB
Markdown
Raw Normal View History

---
2022-12-16 11:46:32 +01:00
title: "DHCP Server and Routing"
visible: true
---
[toc]
2022-12-16 11:46:32 +01:00
## Installation
2022-12-16 11:46:32 +01:00
2023-02-19 15:21:41 +01:00
```sh
apt install isc-dhcp-server
```
## 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
```
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-12-16 11:46:32 +01:00
Edit `/etc/network/interfaces`
```
2023-02-19 15:21:41 +01:00
auto {INTERFACE}
iface {INTERFACE} inet static
address {ADDRESS}
network {NETADDRESS}
netmask {NETMASK}
broadcast {BROADCAST}
```
2023-02-19 15:21:41 +01:00
Enable the interface
2023-02-19 15:21:41 +01:00
```sh
ifup {INTERFACE}
```
Restart DHCP Server
```sh
systemctl restart isc-dhcp-server.service
```
### 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
```
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
```
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
```
### Enable DHCP-managed fixed IP address
2022-12-16 11:46:32 +01: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_
[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-12-16 11:46:32 +01: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
2023-02-19 15:21:41 +01:00
```sh
dhclient -v
```
Release IP
```sh
# dhclient -v -r
```