Запрос адреса dhcp linux

получение адреса по DHCP и работа с dhclient

Thank you for reading this post, don’t forget to subscribe!

Для управ­ле­ния адре­сом интер­фей­са по про­то­ко­лу DHCP (Dynamic Host Configuration Protocol — про­то­кол дина­ми­че­ской настрой­ки узла) исполь­зу­ет­ся ути­ли­та dhclient .

Полезные опции dhclient

-q — не выво­дить дан­ные в кон­со­ли и в лог, кро­ме ошибок;
-1 — отпра­вить запрос толь­ко один раз; в слу­чае ошиб­ки будет exit 2 ;
-r — осво­бо­дить теку­щий адрес;
-lf — файл базы дан­ных арен­ды; если не ука­зан будет исполь­зо­ван файл по-умол­ча­нию /var/lib/dhclient/dhclient.leases ;
-pf — PID -файл про­цес­са; если не ука­зан — будет исполь­зо­ван /var/run/dhclient.pid ;
-cf — файл кон­фи­гу­ра­ции dhcp -кли­ен­та; если не ука­зан — будет исполь­зо­вать­ся /etc/dhcp/dhclient.conf (в CentOS его всё-же надо созда­вать вручную);
-s — ука­зать сер­вер DHCP для отправ­ки запро­са; если не ука­зан — запрос отправ­ля­ет по всей сети 255.255.255.255 ;
-I — ука­за­ние dhcp -иден­ти­фи­ка­то­ра клиента;
-H — ука­за­ние опции host-name в запро­се к DHCP -сер­ве­ру; стро­ка host-name долж­на содер­жать толь­ко пре­фикс име­ни хсо­та кли­ен­та, к кото­ро­му сер­вер доба­вит ddns или dns имя для пол­че­ния пол­но­го FQDN -име­ни; нель­зя исполь­зо­вать с опци­ей -F ;
-F — ука­зать опцию fqdn.fqdn для отправ­ки сер­ве­ру; нель­зя исполь­зо­вать с опци­ей -H ; опция fqdn.fqdn опре­де­ля­ет пол­ное имя хоста кли­ен­та, кото­рое сер­вер будет исполь­зо­вать для онов­ле­ния DDNS ;
-R [,. ] — ука­зать спи­сок опций, кото­рые кли­ент хочет полу­чить от сер­ве­ра; спи­сок по умол­ча­нию содер­жит такие опции:

subnet-mask, broadcast-address, time-offset, routers, domain-name, domain-name-servers, host-name, nis-domain, nis-servers, ntp-servers, interface-mtu

опция -R не добав­ля­ет спи­сок запро­сов к запро­су по-умол­ча­нию, а пере­за­пи­сы­ва­ет его;
-timeout — ука­зать timeout запро­са вручную;
-v — подроб­ный режим;

Примеры работы с dhclient

Источник

10 dhclient (DHCP Client) command examples in Linux

10 dhclient (DHCP Client) command examples in Linux 1

In this article, we will look into 10 dhclient command examples in Linux. dhclient is a free and open source DHCP client tool used for configuring one or more network interfaces using the Dynamic Host Configuration Protocol, BOOTP protocol, or if these protocols fail, by statically assigning an address. Using dhclient tool, a DHCP client system can request an IP address from a Central DHCP Server which which maintains a list of IP addresses which may be assigned on one or more subnets. Here we will see some of the real time examples of dhclient command in below section.

10 dhclient (DHCP Client) command examples in Linux

10 dhclient (DHCP Client) command examples in Linux

Example 1: How to Check dhclient command version

To check the current version of dhclient tool, you need to run dhclient —version command as shown below.

Читайте также:  Linux откат всех обновлений

10 dhclient (DHCP Client) command examples in Linux 2

Example 2: How to Get a DHCP IP on lease

You can simply run sudo dhclient command to request a DHCP IP on lease from the available DHCP Server.

Please note that if you already have a lease IP generated then running sudo dhclient command will show you RTNETLINK answers: File exists on the output.

cyberithub@ubuntu:~$ sudo dhclient

Example 3: How to use verbose mode(-v)

If you want to use verbose mode then you need to use -v option with dhclient command as shown below.

Please note that you need to either have sudo or root access to run dhclient command. Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.

cyberithub@ubuntu:~$ sudo dhclient -v Internet Systems Consortium DHCP Client 4.4.1 Copyright 2004-2018 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/enp0s8/08:00:27:5f:48:3a Sending on LPF/enp0s8/08:00:27:5f:48:3a Listening on LPF/enp0s3/08:00:27:16:c6:22 Sending on LPF/enp0s3/08:00:27:16:c6:22 Sending on Socket/fallback DHCPREQUEST for 192.168.0.104 on enp0s8 to 255.255.255.255 port 67 (xid=0x4dcd9939) DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 67 (xid=0x606bc4b3) DHCPACK of 192.168.0.104 from 192.168.0.1 (xid=0x3999cd4d) .

Example 4: How to release lease IP from an Interface

If you need to release the leased IP and stop the running DHCP client as previously recorded in the PID file then you need to use -r option. In this example we are releasing IP from enp0s3 network interface using sudo dhclient -r -v enp0s3 command as shown below.

10 dhclient (DHCP Client) command examples in Linux 3

Example 5: How to obtain IPV6 address using dhclient command

If you want to use the DHCPv6 protocol to obtain whatever IPv6 addresses are available along with configuration parameters then you need to use -6 option with dhclient command as shown below.

cyberithub@ubuntu:~$ sudo dhclient -6 -v Internet Systems Consortium DHCP Client 4.4.1 Copyright 2004-2018 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on Socket/enp0s3 Sending on Socket/enp0s3 PRC: Soliciting for leases (INIT). XMT: Forming Solicit, 0 ms elapsed. XMT: X-- IA_NA 27:16:c6:22 XMT: | X-- Request renew in +3600 XMT: | X-- Request rebind in +5400 XMT: Solicit on enp0s3, interval 1070ms. .

Example 6: How to stop the running DHCP client without releasing the current lease

If you need to stop the running DHCP client without releasing the current lease IP then you need to use -x option with dhclient command as shown below.

10 dhclient (DHCP Client) command examples in Linux 4

Example 7: How to Change UDP Listening Port

If you want to change the UDP port number on which the DHCP client should listen and transmit to then you need to use -p option with port number specified as an argument. In this example, we are using UDP Port 556 on which dhclient would listen and transmit to using sudo dhclient -v -p 556 enp0s3 command as shown below.

cyberithub@ubuntu:~$ sudo dhclient -v -p 556 enp0s3 binding to user-specified port 556 Internet Systems Consortium DHCP Client 4.4.1 Copyright 2004-2018 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/enp0s3/08:00:27:16:c6:22 Sending on LPF/enp0s3/08:00:27:16:c6:22 Sending on Socket/fallback DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 555 (xid=0x7513614a) DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 555 (xid=0x7513614a) DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 555 (xid=0x7513614a) DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 555 (xid=0x7513614a) .

Example 8: How to Specify Server IP Address

If you want to change the DHCP Server address from where the IP address needs to be leased out then you need to use -s option and specify the destination server IP address or its fully qualified domain name as shown below. For example, here we are using destination server address as 192.168.0.106 .

cyberithub@ubuntu:~$ sudo dhclient -v -s 192.168.0.106 enp0s3 Internet Systems Consortium DHCP Client 4.4.1 Copyright 2004-2018 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/enp0s3/08:00:27:16:c6:22 Sending on LPF/enp0s3/08:00:27:16:c6:22 Sending on Socket/fallback DHCPREQUEST for 192.168.0.108 on enp0s3 to 192.168.0.106 port 67 (xid=0x6d4db49) DHCPREQUEST for 192.168.0.108 on enp0s3 to 192.168.0.106 port 67 (xid=0x6d4db49) DHCPREQUEST for 192.168.0.108 on enp0s3 to 192.168.0.106 port 67 (xid=0x6d4db49) .

Example 9: How to Check all the options available with dhclient tool

To check all the options available with dhclient command you can use dhclient -h or dhclient —help as shown below.

Читайте также:  Asus battery health charging linux

cyberithub@ubuntu:~$ dhclient -h Usage: dhclient [-4|-6] [-SNTPRI1dvrxi] [-nw] [-p ] [-D LL|LLT] [—dad-wait-time ] [—prefix-len-hint ] [—decline-wait-time ] [—address-prefix-len ] [-s server-addr] [-cf config-file] [-df duid-file] [-lf lease-file] [-pf pid-file] [—no-pid] [-e VAR=val] [-sf script-file] [interface]* dhclient

Example 10: How to Check Man page of dhclient tool

To check the man page of dhclient tool, you need to use man dhclient command as shown below.

cyberithub@ubuntu:~$ man dhclient dhclient(8) System Manager's Manual dhclient(8) NAME dhclient - Dynamic Host Configuration Protocol Client SYNOPSIS dhclient [ -4 | -6 ] [ -S ] [ -N [ -N. ] ] [ -T [ -T. ] ] [ -P [ -P. ] ] -R ] [ -i ] [ -I ] [ -4o6 port ] [ -D LL|LLT ] [ -p port- number ] [ -d ] [ -df duid-lease-file ] [ -e VAR=value ] [ -q ] [ -1 ] [ -r | -x ] [ -lf lease-file ] [ -pf pid-file ] [ --no-pid ] [ -cf con‐ fig-file ] [ -sf script-file ] [ -s server-addr ] [ -g relay ] [ -n ] [ -nw ] [ -w ] [ --dad-wait-time seconds ] [ --prefix-len-hint length ] [ --decline-wait-time seconds ] [ -v ] [ --version ] [ if0 [ . ifN ] ] DESCRIPTION The Internet Systems Consortium DHCP Client, dhclient, provides a means for configuring one or more network interfaces using the Dynamic Host Configuration Protocol, BOOTP protocol, or if these protocols fail, by statically assigning an address.

Источник

How do I request a new IP address from my DHCP server using Ubuntu Server?

I know there is a simple command for this, but how do I tell my Ubuntu server instance to request a new IP address from the DHCP server on eth0?

4 Answers 4

this answer still works but is it the current recommended method or has this been superseded by anything better? is this the official method? how does Ubuntu Server internally create the network connection if you have an ethernet connection available at install time?

@warsong as I remember from past, Install screen will let you choose between manually entering IP and DHCP. From docs I can now see, that by default it uses DHCP and shows you the address: ubuntu.com/tutorials/install-ubuntu-server#7-networking Cannot comment if dhclient is the most up-to-date command to be used. As for ifconfig , there is info that it is old and that ip should be used ( ip link show ). ubuntu.com/blog/…

Читайте также:  Tesseract ocr linux install

To release the current IP address:

the OP may wants to run this inside a screen and actually do a «sudo dhclient -r && sudo dhclient » otherwise connectivity may be lost and he needs to get on site to restore the network link. That is if there is no out of bands management installed.

Just restarting dhclient will usually reassign the same IP address if the server doesn’t object. If you release the address first, the server and client know to renegotiate a new one (although it could possibly be the same one you had).

Another issue I ran in to was that dhclient kept requesting the same IP from the DHCP server. Running the command dhclient -r did not resolve this. Therefore, after reading the man pages more thoroughly, I found that if I edited the file /var/lib/dhcp3/dhclient.leases to remove all lease references to the specific adaptor, in my case eth0 . Then running sudo dhclient eth0 worked as expected and assigned me a new/different IP.

My situation was perhaps unique, but it required that my server pull an address from a higher range than was previously issued. The DHCP server was configured to do this, unless a specific IP address was requested regardless of the range. Since dhclient was specifically requesting the old IP address, based on the information in the dhclient.leases file, the DHCP server was always returning the same address.

Agree: situation I have had and just had to remind myself about again is the situation where you have a system which was getting a random IP address, but to which you assign a new (different) statically leased IP address, assigned by MAC. The issue is that if your DHCP client still has a valid lease in dhclient.leases, it does not actually ask the server for a new IP address: it just reuses the lease which it thinks is still valid. So dhclient -r effectively does nothing. Deleting leases file first, followed by dhclient -r, and all is well.

When you run «sudo /etc/init.d/dhcp3-server restart» does it give something like:

Stopping DHCP server: dhcpd3. Starting DHCP server:dhcpd3.

Then just be sure, check if your client sends out a request for a lease, take a look at your /var/logs/deamon.log file. There should be a bunch of DHCP requests in there. If not, the problem is with your client (and we will take it from there).

If your client is ok, just forget about firestarter for the moment and let’s try to get it working without it. To make sure your DHCP server is up and running. Type «sudo /etc/init.d/dhcp3-server stop» and then «sudo /etc/init.d/dhcp3-server start». Then it should be running, assuming it’s properly installed on your box.

I have included a copy of my dhcpd.conf file.

Finally, take a look at your server’s /var/log/messages file if it gets the requests and replies to it with a valid lease. It also might give you an idea what’s wrong with your configuration file (if there’s something wrong with it ofcourse). If you can’t make anything of this, post your messages file and I will take a look lateron.

Источник

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