Linux get address dhcp

получение адреса по 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

Источник

Find DHCP Server IP Address on Linux Command Line

Find DHCP Server IP Address on Linux Command Line

I recently received an email from a reader asking «How do I find the IP address of my DHCP server»? My initial reaction was «Hmmm, good question». That lead me to investigate the fastest way to find your DHCP server IP address from the Linux command line.

What is a DHCP Server?

A DHCP Server is a system on a network that manages dynamic configuration of client systems on the network. DHCP (Dynamic Host Configuration Protocol) is a standard network protocol used on almost all IP networks. It allows for dynamic configuration of network parameters on systems connected to the network. Before DHCP an administrator would have to configure each system individually. For more information on DHCP, see the Resource and Links section below.

Читайте также:  Linux mint сетевая папка windows

How to Find Your DHCP Server IP Address

There is no simple command to show the IP address of the DHCP server on the network. However, there are places it is recorded, if you know where to look. Here are a few ways you can find the IP address of a DHCP server on your network.

NOTE: You will need root or sudo access to run all of these commands.

Find DHCP Server IP Using grep to Find Log Entries

The first method I thought of was to check the logs for the DHCPOFFER. The DHCPOFFER packet would come from the DHCP server and should be written to your logs. Here we use grep with some options (-I to ignore binary files, -R for recursive) to find the log file entry that contains our DHCP server IP address.

[[email protected] ~]$ sudo grep -IR "DHCPOFFER" /var/log/* /var/log/anaconda/syslog:17:15:00,299 INFO dhclient:DHCPOFFER from 10.0.0.1
[[email protected] ~]$ sudo grep -IR "DHCPOFFER" /var/log/* /var/log/anaconda/journal.log:Apr 03 20:02:33 localhost-live dhclient[2464]: DHCPOFFER from 10.0.0.1
[email protected]:~$ sudo grep -IR "DHCPOFFER" /var/log/* /var/log/syslog:Oct 12 10:15:55 ubuntu1904 dhclient[864]: DHCPOFFER of 192.168.122.204 from 192.168.122.1

NOTE: Notice that every distribution stores it in a different log file? That is why we recursively search the whole /var/log/ directory.

Using Journalctl and grep

You can do the same as above using journalctl.

[[email protected] ~]$ sudo journalctl | grep -m1 DHCPACK Apr 20 21:46:28 putor dhclient[1506]: DHCPACK from 10.0.0.1 (xid=0x65f93c03)

To learn more about using journalctl read «Viewing logs with journalctl».

Find DHCP Server IP Address using Leases File

The dhclient utility stores your lease information in leases file. These lease files are kept in different places for different distros, software loads, and different versions.

/var/lib/dhcp/dhclient.[interface].leases /var/lib/dhcp3/dhclient.leases

NOTE: Replace [interface] with the name of your network interface (i.e. eth0, eno1, etc..)

Now use grep to find the DHCP server.

[[email protected] ~]$ sudo grep -m1 "dhcp-server" /var/lib/dhcp/dhclient.eno1.leases option dhcp-server-identifier 10.0.0.1;

If you are using NetworkManager, you can find the lease file location by looking at the running processes (ps aux).

[[email protected] ~]$ ps aux | grep NetworkManager root 967 0.0 0.1 620636 20348 ? Ssl 10:14 0:00 /usr/sbin/NetworkManager --no-daemon root 2603 0.0 0.0 15176 9016 ? S 10:15 0:00 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-eno1.pid -lf /var/lib/NetworkManager/dhclient-dd040512-708c-3858-97c6-d79e66458e36-eno1.lease -cf /var/lib/NetworkManager/dhclient-eno1.conf eno1

Now that you know where the leases file is, you can get the DHCP server IP address from that file.

[[email protected] ~]$ sudo grep -m1 "dhcp-server" /var/lib/NetworkManager/dhclient-dd040512-708c-3858-97c6-d79e66458e36-eno1.lease option dhcp-server-identifier 10.0.0.1;

We can string both of these operations together to simplify the process.

[[email protected] ~]$ sudo cat $(ps aux | grep -o '[/]var/lib/NetworkManager/\S*.lease') | grep -m1 dhcp-server-identifier option dhcp-server-identifier 10.0.0.1;

Find DHCP Server IP Using dhclient Utility

Another way would be to use the dhclient utility with some options. This also requires root or elevated privileges. The drawback of this is that the dhclient will go through the whole DHCP D.O.R.A. process. This also means that your DHCP server, network and client system need to be configured correctly. This is not much help if you are troubleshooting DHCP issues.

[[email protected] ~]$ sudo dhclient -d -nw eno1 Internet Systems Consortium DHCP Client 4.3.6 Copyright 2004-2017 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/eno1/34:e6:d7:0f:a9:83 Sending on LPF/eno1/34:e6:d7:0f:a9:83 Sending on Socket/fallback DHCPDISCOVER on eno1 to 255.255.255.255 port 67 interval 7 (xid=0x1e69d667) DHCPREQUEST on eno1 to 255.255.255.255 port 67 (xid=0x1e69d667) DHCPOFFER from 10.0.0.1 DHCPACK from 10.0.0.1 (xid=0x1e69d667) bound to 10.0.0.2 -- renewal in 3287 seconds.

Conclusion

There you have it, a few different methods to find the IP address of your DHCP server. I am sure there are other ways, as with anything in Linux. If you know of another way please leave it in the comments and I will update the article.

Читайте также:  Dbf формат на линукс

Источник

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/…

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.

Читайте также:  Как установить firefox линукс

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