Linux dhcp fixed address

How to declare fixed addresses for each subnet in isc-dhcp-server?

On Ubuntu, I’ve written some host declaration in each subnet with isc-dhcp-server, and each fixed address for each network interface are successfully leased. There are plugged two network cards on this DHCP server. But how to make corrections for this warning?

dhcpd[11328]: WARNING: Host declarations are global. They are not limited to the scope you declared them in.

This post on the same warning message answers host declarations are out of subnet definitions. I don’t think that is true in my case where two cards involved.

1 Answer 1

Host definitions are always global.

So I have 3 networks on my router; «LAN» 10.0.0.0/24, «guest» 10.100.100.0/24 and «IoT» 10.100.200.0/24

My dhcpd.conf has the following sort of config

subnet 10.0.0.0 netmask 255.255.255.0 < authoritative; option routers 10.0.0.1; blah; >subnet 10.100.100.0 netmask 255.255.255.0 < authoritative; option routers 10.100.100.1; blah; >subnet 10.100.200.0 netmask 255.255.255.0 < authoritative; option routers 10.100.200.1; blah; >host machine1 < hardware ethernet xx:xx:xx:xx:xx:xx; fixed-address 10.0.0.13; option host-name "machine1"; >host machine2

DHCPd correctly works out that machine1 is on LAN and machine2 is on the IoT subnet and sends the correct configuration (netmask, default route, DNS server etc etc) relevant to that subnet.

If you have a machine that can connect to multiple interfaces and you want them to get different addresses then you can list the host multiple times. For example, my cellphone:

Now it’ll will get a different address, depending on what network it is on.

If there’s no static entry for that network then it’ll get a dynamic address. If there’s no free addresses on the subnet then it won’t get assigned any address.

Источник

How to install and configure DHCP server in Linux

Configure DHCP on Linux

DHCP (stands for dynamic host configuration protocol) dynamically assigns unique IP addresses and other information to client systems. Some of the information that is assigned by DHCP includes hostname, domain name, IP address of the default gateway and DNS server, etc. It saves the system administrator from the hassle of manually assigning IP addresses and related information to each of the client systems. Usually, in the home or small company setup, routers serve the role of a DHCP server. However, in a large setup, we need to configure a dedicated server to serve as a DHCP server. In Linux, you can easily set up a DHCP server and assigns IP addresses to clients.

Читайте также:  Android linux передача файлов wifi

In this tutorial, we will demonstrate how to configure DHCP in Linux to automatically assign IP addresses to clients.

How DHCP works

Let’s explain a little about how DHCP works:

Consider a scenario where the client and DHCP server are connected to the same wired network.

  1. When a client machine is turned on, it sends a broadcast message DHCPDISCOVER to every device on the network.
  2. When this message reached the DHCP server, it sends back a DHCPOFFER message with an IP address to client.
  3. The client then sends a DHCPREQUEST to DHCP server to request for this IP address.
  4. The DHCP server then sends DHCPACK to acknowledge the request and grants the client with an IP address.

Prerequisites

  • Minimum two Linux machines (For DHCP server and client)
  • User with sudo privileges
  • Static IP configured on DHCP server

Note:

For the demonstration purpose, we have used three Linux machines which were on the same LAN. You can think of them as all are connected through each other via switch.

DHCP server

For DHCP server, we have used:

DHCP client

For DHCP client, we have used two machines:

DHCP Server Installation

First, update the system repository index in your system. Issue the below command in Terminal to do so:

Now issue the following command in Terminal to install DHCP server:

$ sudo apt-get install isc-dhcp-server -y

install dhcp linux

DHCP Server Configuration

Before configuring a Linux system as a DHCP server, make sure it has a static IP address configured so that clients can easily connect to it. Otherwise, it will be difficult for the clients to find which DHCP server to connect to.

In the /etc/default/isc-dhcp-server file, you will need to specify the interface on which DHCP server will listen to. Issue the following command in Terminal to edit this file:

$ sudo nano /etc/default/isc-dhcp-server

In the INTERFACESv4 entry, specify the interface you want the DHCP server to listen to such as ens33, eth0, eth1, etc.

INTERFACESv4="interface_name"

In our scenario, the interface is ens33, so the entry would change to:

install dhcp linux

Once you have configured the file, save, and close it.

Now we will configure DHCP Server using its default configuration file located at /etc/dhcp/dhcpd.conf.

Edit the /etc/dhcp/dhcpd.conf file as follows:

$ sudo nano /etc/dhcp/dhcpd.conf

Uncomment the following line by removing the # symbol before it.

install dhcp linux

Also, add the following lines in the file:

default-lease-time 600; max-lease-time 7200; # subnet subnet 192.168.9.0 netmask 255.255.255.0 < # range of subnet range 192.168.9.5 192.168.9.15; # gateway address option routers 192.168.9.1; # DNS server address option domain-name-servers 8.8.8.8, 8.8.4.4; >

Once you have configured the file, save, and close it.

Assign a static IP address to clients from DHCP server

The DHCP server issues dynamic IP addresses to the clients by default. However, you can also configure it to assign fixed or static IP address to clients. Fixed addresses are required for some network devices like printers, routers, or when you need to remotely connect a device from outside. To assign a static IP address to a specific machine, you will need its MAC address.

Читайте также:  Linux list mounted disks

Let’s say we want to assign a client with MAC address 00:0c:29:c1:d5:d4 a fixed IP 192.168.1.13 every time.

Edit the /etc/dhcp/dhcpd.conf file and add host entry containing the MAC address of the client and the fixed IP address you want to assign to the client:

Note: Fixed IP address should not be from the range of the Dynamic IP range.

Now, every time the client with the matching MAC address (00:0c:29:c1:d5:d4) sends a DHCPDISCOVER message to the DHCP server, will receive the same IP address 192.168.9.13.

Restart DHCP service

Once you are done with the configurations, restart the DHCP server service. Issue the below command in Terminal to do so:

$ sudo systemctl restart isc-dhcp-server.service

Now verify the status of the DHCP server using the below command:

$ sudo systemctl status isc-dhcp-server.service

The following output shows the DHCP service is active and running successfully without any errors.

DHCP Client Configuration

Now we will configure two client machines. One client will be assigned random IP from the subnet range defined in the configuration file. The other client will be assigned the static IP.

By default, dynamic IP addressing is configured in Ubuntu OS. However, if it is using the static IP address, you can configure it to obtain an IP address from the DHCP server.

Client 1- Obtain Dynamic IP address from the DHCP server

First, find your network interface name using the following command:

Once you have found the interface name, issue the following command in Terminal to configure the network interface:

$ sudo nano /etc/network/interfaces

Insert the below lines in the file:

auto ens33 iface ens33 inet dhcp

Save and close the file and restart the network-manager service using the following command in Terminal:

$ sudo systemctl restart network-manager.service

Now run the ifconfig command to verify the IP address of your client.

You can see in the following output that the client has received the IP address 192.168.9.5 which is from the subnet range defined in the DHCP server configuration.

In order to find the IP address of the DHCP server, issue the following command in Terminal:

$ sudo grep -R “DHCPOFFER” /var/sys/log

From the output, you can verify the IP address is assigned by 192.168.9.1 which is the IP address of our DHCP server.

In case, there is no IP address obtained by the client machine from the DHCP server, then use the following commands to release/renew the IP address:

$ sudo dhclient -r 

$ sudo dhclient -v

Now run the ifconfig command to verify the IP address of the client. This time hopefully the client will receive the IP address from the DHCP server.

Читайте также:  Символ операционных систем linux

Client 2- Obtain Static IP address from the DHCP server

For our second client machine, we have added the static/fixed IP address entry in the DHCP server configuration file. On the client machine, first configure the interface to acquire the IP address from the DHCP server.

Find your network interface name using the following command:

Once you have found the interface name, issue the following command in Terminal to configure the network interface:

$ sudo nano /etc/network/interfaces

Insert the below lines in the file:

auto ens33

iface ens33 inet dhcp

Now save and close the file and restart the network-manager service using the following command in Terminal:

$ sudo systemctl restart network-manager.service

Now run the ifconfig command to verify the IP address of your client.

You can see in the following output that the client has received the fixed IP address 192.168.9.13 from the DHCP server. This is the same fixed IP address we have added in the DHCP configuration file.

In order to find the IP address of the DHCP server, issue the following command in Terminal:

$ sudo grep -R “DHCPOFFER” /var/sys/log

From the output, you can verify the IP addresses are assigned by 192.168.9.1 which is our DHCP server.

In case, there is no IP address obtained by the client machine from the DHCP server, then use the following commands to release/renew IP address:

$ sudo dhclient –r -v

$ sudo dhclient -v

Now run the ifconfig command to verify the IP address of the client. This time hopefully the client will receive the IP address from the DHCP server.

View assigned IP addresses by DHCP server

From the DHCP server, you can also verify the dynamic addresses assigned by it to the clients.

In the DHCP server machine, run the following command in Terminal to list the addresses assigned by the DHCP server:

In the following output, you will see only the IP addresses dynamically assigned by the DHCP server to a client. Remember, it will not list the static IP addresses that are issued by the DHCP server.

In this article, you have learned how to install and configure the DHCP server in the Linux system and assign dynamic and fixed IP addresses to the clients. It takes just a few simple steps to configure the DHCP server in a Linux machine. Once you have configured it, it will automatically assign the assigning IP addresses and related information to each machine on the network.

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.

Источник

Оцените статью
Adblock
detector