Difference between revisions of "Dhcpd"

From ArchWiki
Jump to: navigation, search
m (Configuration: link to more general article)
(Changed After=network.target to After=network-pre.target and added Before=network.target. With a windows client, windows client would assign fake ipaddress after not receiving it from dhcp server.)
 
(43 intermediate revisions by 21 users not shown)
Line 1: Line 1:
 +
{{Lowercase title}}
 
[[Category:Networking]]
 
[[Category:Networking]]
dhcpd is the [http://www.isc.org/software/dhcp/documentation Internet Systems Consortium] DHCP Server. It is useful for instance on a machine acting as a router on a LAN.
+
[[de:Dhcpd]]
 +
[[ja:Dhcpd]]
 +
[[ru:Dhcpd]]
 +
{{Related articles start}}
 +
{{Related|dhcpcd}}
 +
{{Related articles end}}
 +
 
 +
dhcpd is the [http://www.isc.org/downloads/dhcp/ Internet Systems Consortium] DHCP Server. It is useful for instance on a machine acting as a router on a LAN.
 +
 
 +
{{Note|''dhcpd'' (DHCP '''(server)''' daemon) is not the same as [[dhcpcd]] (DHCP '''client''' daemon).}}
  
 
== Installation ==
 
== Installation ==
[[pacman|Install]] the {{pkg|dhcp}} package, available in the [[official repositories]].
+
 
 +
[[Install]] the {{pkg|dhcp}} package, available in the [[official repositories]].
 +
 
 +
== Usage ==
 +
 
 +
''dhcpd'' includes a unit file {{ic|dhcpd4.service}}, which can be used to [[Enable|control]] the daemon. It starts the daemon for ''all'' [[network interfaces]]. See [[#Listening on only one interface]] for alternative.
  
 
== Configuration ==
 
== Configuration ==
Assign an static IPv4 address to the interface you want to use (usually {{ic|eth0}}). The first 3 bytes of this address cannot be exactly the same as those of another interface.
 
{{bc|
 
# ip link set up dev eth0
 
# ip addr add 139.96.30.100/24 dev eth0 # arbitrary address
 
}}
 
To have your static ip assigned at boot, see [[Network Configuration#Static IP address]].
 
  
The default {{ic|dhcpd.conf}} contains many uncommented examples, so relocate it
+
Assign a static IPv4 address to the interface you want to use (in our examples we will use {{ic|eth0}}). The first 3 bytes of this address cannot be exactly the same as those of another interface.
{{bc|
+
 
# mv /etc/dhcpd.conf /etc/dhcpd.conf.example
+
# ip link set up dev eth0
 +
# ip addr add 139.96.30.100/24 dev eth0 # arbitrary address
 +
 
 +
{{Tip|Usually, the one of next three subnets is used for private networks, which are specially reserved and won't conflict with any host in the Internet:
 +
* {{ic|192.168/16}} (subnet {{ic|192.168.0.0}}, netmask {{ic|255.255.0.0}})
 +
* {{ic|172.16/12}} (subnet {{ic|172.16.0.0}}, netmask {{ic|255.240.0.0}})
 +
* {{ic|10/8}} (for large networks; subnet {{ic|10.0.0.0}}, netmask {{ic|255.0.0.0}})
 +
See also [http://www.ietf.org/rfc/rfc1918.txt RFC 1918].}}
 +
 
 +
To have your static ip assigned at boot, see [[Network configuration#Static IP address]].
 +
 
 +
The default {{ic|dhcpd.conf}} contains many uncommented examples, so relocate it:
 +
 
 +
# mv /etc/dhcpd.conf /etc/dhcpd.conf.example
 +
 
 +
The minimal configuration file may look like:
 +
 
 +
{{hc|/etc/dhcpd.conf|
 +
option domain-name-servers 8.8.8.8, 8.8.4.4;
 +
option subnet-mask 255.255.255.0;
 +
option routers 139.96.30.100;
 +
subnet 139.96.30.0 netmask 255.255.255.0 {
 +
  range 139.96.30.150 139.96.30.250;
 +
}
 
}}
 
}}
Edit the configuration file to contain:
+
 
 +
If you need to provide a fixed IP address for a single specific device, you can use the following syntax
 +
 
 
{{hc|/etc/dhcpd.conf|
 
{{hc|/etc/dhcpd.conf|
# Using the google's dns in the example.
+
option domain-name-servers 8.8.8.8, 8.8.4.4;
# Change it to 139.96.30.100 if you have a dns server installed
+
option domain-name-servers 8.8.8.8;
+
 
option subnet-mask 255.255.255.0;
 
option subnet-mask 255.255.255.0;
 
option routers 139.96.30.100;
 
option routers 139.96.30.100;
 
subnet 139.96.30.0 netmask 255.255.255.0 {
 
subnet 139.96.30.0 netmask 255.255.255.0 {
 
   range 139.96.30.150 139.96.30.250;
 
   range 139.96.30.150 139.96.30.250;
 +
 +
  host macbookpro{
 +
  hardware ethernet 70:56:81:22:33:44;
 +
  fixed-address 139.96.30.199;
 +
  }
 
}
 
}
 
}}
 
}}
  
Start, and optionally, enable for autostart on boot, the {{ic|dhcpd4.service}} daemon.  
+
{{ic|domain-name-servers}} option contains addresses of DNS servers which are supplied to clients. In our example we are using Google's public DNS servers. If you know a local DNS servers (for example, provided by your ISP), you should use it. If you've configured your own DNS on a local machine, then use its address in your subnet (e. g. {{ic|139.96.30.100}} in our example).
  
Read [[Daemons]] for more information.
+
{{ic|subnet-mask}} and {{ic|routers}} defines a subnet mask and a list of available routers on the subnet. In most cases for small networks you can use {{ic|255.255.255.0}} as a mask and specify an IP address of the machine on which you're configuring DHCP server as a router.
  
Now, any computer you connect over ethernet will be assigned an IPv4 address (from {{ic|139.96.30.150}} to {{ic|139.96.30.250}} in this example).
+
{{ic|subnet}} blocks defines options for separate subnets, which are mapped to the network interfaces on which ''dhcpd'' is running. In our example this is one subnet {{ic|139.96.30.0/24}} for single interface {{ic|eth0}}, for which we defined the range of available IP addresses. Addresses from this range will be assigned to the connecting clients.
  
== Tips and Tricks ==
 
 
=== Listening on only one interface ===
 
=== Listening on only one interface ===
If your computer is already part of one or several networks, it could be a problem if your computer starts giving ip addresses to machines from the other networks.
 
  
In order to force the DHCP server to listen only on one of the network interfaces, you need to specify it in the dhcpd command line.  
+
If your computer is already part of one or several networks, it could be a problem if your computer starts giving ip addresses to machines from the other networks. It can be done by either configuring dhcpd or starting it as a daemon with [[Systemd#Using units|systemctl]].
  
{{Accuracy|This appears not to work as of ArchLinux 2013-07-01 - see talk page for details.}}
+
==== Configuring dhcpd ====
  
This is done by editing the configuration file:
+
In order to exclude an interface, you must create an empty declaration for the subnet that will be configured on that interface.
{{hc|/etc/conf.d/dhcp|<nowiki>
+
 
# Assuming the device of your lan is eth1
+
This is done by editing the configuration file (for example):
DHCP4_ARGS="-q eth1"
+
{{hc|/etc/dhcpd.conf|<nowiki>
 +
# No DHCP service in DMZ network (192.168.2.0/24)
 +
subnet 192.168.2.0 netmask 255.255.255.0 {
 +
}
 
</nowiki>}}
 
</nowiki>}}
Another step is to tell the routing table on which interface to listen to for the 255.255.255.255 broadcasts:
 
{{bc|
 
# ip route add 255.255.255.255 dev eth1
 
}}
 
Now, the clients on {{ic|eth1}} will be managed by your DHCP server without having any impact on any client / server on any other ntework interface.
 
  
== Notes ==
+
==== Service file ====
You will see configuration files, etc. related to {{ic|dhcpcd}}. That one is the DHCP '''client''' executable and has nothing to do with {{ic|dhcpd}}.
+
 
 +
There is no service files provided by default to use ''dhcpd'' only on one interface so you need to create one:
 +
 
 +
{{hc|/etc/systemd/system/dhcpd4@.service|<nowiki>
 +
[Unit]
 +
Description=IPv4 DHCP server on %I
 +
Wants=network.target
 +
After=network-pre.target
 +
Before=network.target
 +
 
 +
[Service]
 +
Type=forking
 +
PIDFile=/run/dhcpd4.pid
 +
ExecStart=/usr/bin/dhcpd -4 -q -pf /run/dhcpd4.pid %I
 +
KillSignal=SIGINT
 +
 
 +
[Install]
 +
WantedBy=multi-user.target
 +
</nowiki>}}
 +
 
 +
This is a template unit, which binds it to a particular interface, for example {{ic|dhcpd4@''eth0''.service}} where ''eth0'' is an interface shown with {{ic|ip link}}.
 +
 
 +
=== Use for PXE ===
 +
 
 +
PXE Configuration is done with the following two options:
 +
 
 +
{{hc|/etc/dhcpd.conf|<nowiki>
 +
next-server 192.168.0.2;
 +
filename "/pxelinux.0";
 +
</nowiki>}}
  
==See also==
+
This section can either be in an entire {{ic|subnet}} or just in a {{ic|host}} definition. {{ic|next-server}} is the IP of the TFTP Server, and {{ic|filename}} is the filename of the image to boot. For more information see [[PXE]].
*[[Dhcpcd]]
+

Latest revision as of 22:22, 13 April 2016

Related articles

dhcpd is the Internet Systems Consortium DHCP Server. It is useful for instance on a machine acting as a router on a LAN.

Note: dhcpd (DHCP (server) daemon) is not the same as dhcpcd (DHCP client daemon).

Installation

Install the dhcp package, available in the official repositories.

Usage

dhcpd includes a unit file dhcpd4.service, which can be used to control the daemon. It starts the daemon for all network interfaces. See #Listening on only one interface for alternative.

Configuration

Assign a static IPv4 address to the interface you want to use (in our examples we will use eth0). The first 3 bytes of this address cannot be exactly the same as those of another interface.

# ip link set up dev eth0
# ip addr add 139.96.30.100/24 dev eth0 # arbitrary address
Tip: Usually, the one of next three subnets is used for private networks, which are specially reserved and won't conflict with any host in the Internet:
  • 192.168/16 (subnet 192.168.0.0, netmask 255.255.0.0)
  • 172.16/12 (subnet 172.16.0.0, netmask 255.240.0.0)
  • 10/8 (for large networks; subnet 10.0.0.0, netmask 255.0.0.0)
See also RFC 1918.

To have your static ip assigned at boot, see Network configuration#Static IP address.

The default dhcpd.conf contains many uncommented examples, so relocate it:

# mv /etc/dhcpd.conf /etc/dhcpd.conf.example

The minimal configuration file may look like:

/etc/dhcpd.conf
option domain-name-servers 8.8.8.8, 8.8.4.4;
option subnet-mask 255.255.255.0;
option routers 139.96.30.100;
subnet 139.96.30.0 netmask 255.255.255.0 {
  range 139.96.30.150 139.96.30.250;
}

If you need to provide a fixed IP address for a single specific device, you can use the following syntax

/etc/dhcpd.conf
option domain-name-servers 8.8.8.8, 8.8.4.4;
option subnet-mask 255.255.255.0;
option routers 139.96.30.100;
subnet 139.96.30.0 netmask 255.255.255.0 {
  range 139.96.30.150 139.96.30.250;

  host macbookpro{
   hardware ethernet 70:56:81:22:33:44;
   fixed-address 139.96.30.199;
  }
}

domain-name-servers option contains addresses of DNS servers which are supplied to clients. In our example we are using Google's public DNS servers. If you know a local DNS servers (for example, provided by your ISP), you should use it. If you've configured your own DNS on a local machine, then use its address in your subnet (e. g. 139.96.30.100 in our example).

subnet-mask and routers defines a subnet mask and a list of available routers on the subnet. In most cases for small networks you can use 255.255.255.0 as a mask and specify an IP address of the machine on which you're configuring DHCP server as a router.

subnet blocks defines options for separate subnets, which are mapped to the network interfaces on which dhcpd is running. In our example this is one subnet 139.96.30.0/24 for single interface eth0, for which we defined the range of available IP addresses. Addresses from this range will be assigned to the connecting clients.

Listening on only one interface

If your computer is already part of one or several networks, it could be a problem if your computer starts giving ip addresses to machines from the other networks. It can be done by either configuring dhcpd or starting it as a daemon with systemctl.

Configuring dhcpd

In order to exclude an interface, you must create an empty declaration for the subnet that will be configured on that interface.

This is done by editing the configuration file (for example):

/etc/dhcpd.conf
# No DHCP service in DMZ network (192.168.2.0/24)
subnet 192.168.2.0 netmask 255.255.255.0 {
}

Service file

There is no service files provided by default to use dhcpd only on one interface so you need to create one:

/etc/systemd/system/dhcpd4@.service
[Unit]
Description=IPv4 DHCP server on %I
Wants=network.target
After=network-pre.target
Before=network.target

[Service]
Type=forking
PIDFile=/run/dhcpd4.pid
ExecStart=/usr/bin/dhcpd -4 -q -pf /run/dhcpd4.pid %I
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

This is a template unit, which binds it to a particular interface, for example dhcpd4@eth0.service where eth0 is an interface shown with ip link.

Use for PXE

PXE Configuration is done with the following two options:

/etc/dhcpd.conf
next-server 192.168.0.2;
filename "/pxelinux.0";

This section can either be in an entire subnet or just in a host definition. next-server is the IP of the TFTP Server, and filename is the filename of the image to boot. For more information see PXE.