DHCP: Diferència entre les revisions

De Guifi.net - Wiki Català

m (1 revisió)
m (Enllaços externs)
Línia 2.422: Línia 2.422:
 
*[http://www.tcpipguide.com/free/t_DHCPGeneralOperationandClientFiniteStateMachine.htm DHCP General Operation and Client Finite State Machine]
 
*[http://www.tcpipguide.com/free/t_DHCPGeneralOperationandClientFiniteStateMachine.htm DHCP General Operation and Client Finite State Machine]
  
[[Category:DissenyXarxesLinux]] [[Category:LinuxAdministracioAvancada]]
+
[[Categoria:Xarxa]]

Revisió de 17:22, 29 des 2010

25px Aquesta wiki forma part dels materials d'un curs
Curs: DissenyXarxesLinux, LinuxAdministracioAvancada
Fitxers: ServidorsDHCPiDNS.pdf (ServidorsDHCPiDNS.odp)
Repositori SVN: https://anonymous@svn.projectes.lafarga.cat/svn/iceupc/LinuxAdministracioAvan%c3%a7ada
Usuari: anonymous
Paraula de pas: sense paraula de pas
Autors: Sergi Tur Badenas

Contingut

Introducció

Una de les feines més pesades del administrador de xarxes, es haver d'anar a equip a equip per tal de realitzar la configuració IP de les màquines d'una xarxa. Ja sigui per què s'ha d'incorporar un nou equip a la xarxa o per canvi d'estructura de la xarxa, si la xarxa creix es fa més complicat mantenir-la. Es pot posar fi a les molésties que tot això implica, configurant en pocs minuts la xarxa amb DHCP.

El grup de treball de la Internet Engineering Task Force, va decidir resoldre el problema desenvolupant un Protocol per Configuració Dinàmica de Terminals de xarxa anomenat DHCP. Aquest protocol esta basat en un altre d'anterior (BOOTP Boostrap Protocol) però amb unes característiques millors. El protocol està descrit en el RFC 2131.

El servidor DHCP proporciona les dades TCP/IP que ordinadors clients necessiten per connectar-se a la xarxes (adreça IP, màscara de xarxa, gateway, servidors DNS, etc.) facilitant el manteniment del ordinadors clients i si hi hagués algun canvi de configuració no caldria anar a cada equip modificant les dades TCP/IP.

Altes protocols similars

Actualment DHCP és el protocol més utilitzat per configurar nodes de xarxa però històricament s'han utilitzat altres sistemes/protocols com:

  • Bootp: És un altre sistema per obtenir una IP de forma automàtica.
  • RARP: Reverse ARP és el protocol invers a ARP i permet obtenir una adreça IP a partir d'una adreça MAC. És un sistema "similar" a utilitzar DHCP amb assignació fixa d'adreces IP segons la MAC dels clients.

Altres protocols relacionats són:

Per a més informació consulteu la secció See Also de l'article sobre DHCP de la wikipedia.

Què és?

DHCP és un servei que es fa servir per:

  • entregar IPs als clients de la xarxa
  • compatibilizar amb BOOTP per el booteix de màquines Diskless

DHCP té dos components principals:

  • "protocol" que serveix per entregar configuracions específiques per cada host desde un servidor DHCP.
  • "mecanisme" que serveix per la localització de direccions de xarxa a hosts.

Però sobre tot, DHCPP és un model de client-servidor.

Com funciona?

  • L'interacció entre client-servidor molt resumida del seu funcionament és la següent (més informació, RFC1531 - RFC2131):
  1. El client envia un paquet de broadcast a tota la xarxa amb destinació el port 67. Aquest missatge s'anomena DHCPDISCOVER i s'utilitza per localitzar el servidor de DHCP de la xarxa.
  2. El servidor DHCP que està escoltant pel port 67 contesta amb el paquet DHCPOFFER que conté les dades TCP/IP que el client necessita, aquest paquet també s'envia a tota la xarxa (cal tenir en compte que el node client encara no esta configurat. El port de destinació és el 68.
  3. El client, que està escoltant per el port 68, rep les dades i envía un missatge DHCPREQUEST per comunicar-li al servidor que acepta les dades.
  4. El servidor envía el paquet DHCPAK per dir-li que està conforme.

Fitxer:DHCPProtocol.png

Consulteu l'apartat Technical Details de l'article sobre DHCP a la wikipedia.

Paquets DHCPINFORM

Utilitzar per clients Windows per obtenir informació sobre paràmetres de xarxa DHCP, tot i no estar configurats per DHCP.

Recursos:

Ports i Protocol

DHCP utilitza el protocol UDP.

El port que utilitza el servidor de DHCP és el 67.

Al fitxer /etc/services el port apareix amb el nom del protocol bootp.

$ cat /etc/services | grep bootp
bootps          67/tcp                          # BOOTP server
bootps          67/udp
bootpc          68/tcp                          # BOOTP client
bootpc          68/udp

Com podeu veure el client també utilitza un port concret: el port 68.

Per comprovar si un servidor de DHCP funciona podeu utilitzar la comanda NMAP:

$ sudo nmap -sU 192.168.1.1 -p 67
Starting Nmap 4.20 ( http://insecure.org ) at 2008-01-24 12:56 CET
Interesting ports on mygateway1.ar7 (192.168.1.1):
PORT   STATE         SERVICE
67/udp open|filtered dhcps
MAC Address: 00:15:E9:CA:34:A5 (D-Link) 

Nmap finished: 1 IP address (1 host up) scanned in 0.650 seconds

Servidors DHCP Autoritaris

authoritative: adjetivo. 1   (de confianza, oficial) autorizado,-a. 2   Pol, etc, autoritario,-a

Un servidor DHCP és autoritari si un client demana una IP desconeguda o incorrecta al servidor per a un determinat segment de xarxa i el servidor envia un DHCPNAK indicant al client que deixi d'utilitzar aquesta IP.

Per tal que el servidor tingui aquest comportament cal utilitzar la directiva:

authoritative;
NOTA: El servidor DHCP d'ISC ens avisa durant la instal·lació que el seu servidor a la versió 3 és per defecte no autoritari.
IMPORTANT: Si no hi han més servidors de DHCP a la xarxa cal posar authoritative en la configuració de DHCP.

Recursos:

Característiques

Protocol).

  • És un protocol de xarxa, on un servidor proveeix dels paràmetres necessaris de configuració i assignació

d'adreces IP a les màquines d'una xarxa.

  • És un estàndard en xarxes que també podem trobar a tots els sistemes operatius

Avantatges i inconvenients

  • Avantatges

Al instal·lar un sistema DHCP en la xarxa, treu treball de configuració per la xarxa, només es configura un servidor per entregar els números IP per els clients de xarxa. Totes les màquines demanen informació de la xarxa i es configuren automàticament, s'entregan tots els paràmetres bàsics de TCP/IP. És vell però molt recomanable per a una administració fàcil.

  • Desavantatges

Al entregar els números IP dins de la xarxa, havent un DNS, no hi ha un pont intermig entre DNS i DHCP directe. Es té que agregar les màquines "a mà" en el DNS.

Terminologia. Definició de conceptes

  • range (scope): conjunt d'adreces IP que gestiona un servidor DHCP.
  • lease o préstec: és el període de temps que s'assigna una adreça IP a un client de la xarxa. Els clients poden ampliar els préstecs en peticions posteriors. També és possible que els clients informin al servidor quan l'adreça IP queda lliure. Els clients poden demanar un préstec "infinit" i depèn de la configuració del servidor que aquests préstecs siguin acceptats o no.
  • binding: és una col·lecció de paràmetres de configuració que inclou com a mínim una adreça IP que s'associa (bound) a un client DHCP. Els bindings són gestionats pels servidors de DHCP.
  • exclusion: és una adreça o rang d'adreces que no poden ser gestionades per un servidor DHCP. Per exemple si s'exclou el rang 192.168.0.1-192.168.0.10 l'única forma que un ordinador pot obtenir aquesta IP és assignant-se-la estàticament. Amb el servidor DHCP del ISC les exclusions no es defineixen implícitament, és a dir, totes les màquines que no estan als rangs definits no són gestionades pel servidor DHCP.
  • reservation: és una IP que s'ha reservat a una adreça MAC concreta. Consulteu: DHCP#Adre.C3.A7a_IP_fixa_per_MAC
  • rogue DHCP servers: http://en.wikipedia.org/wiki/Rogue_DHCP, http://freshmeat.net/projects/roguedetect/

NOTA: La terminologia exclusions i reservation no s'indica en cap moment al RFC de DHCP. Són termes utilitzats per servidors DHCP privatius com els de Microsoft

Instal·lació

Instal·lació del client

La majoria de distribucions Linux modernes ja porten instal·lat el client de DHCP. Cal tenir en compte que la majoria de xarxes LAN SoHo (Small Office Home Office) es configuren amb DHCP.

En tot cas el paquet que proporciona el suport client per a DHCP, a sistemes de la família Debian és dhcp3-client:

$ sudo apt-get install dhcp3-client

Els fitxers proporcionats són:

$ dpkg -L dhcp3-client
/.
/etc
/etc/dhcp3
/etc/dhcp3/dhclient-enter-hooks.d
/etc/dhcp3/dhclient-enter-hooks.d/debug
/etc/dhcp3/dhclient-exit-hooks.d
/etc/dhcp3/dhclient-exit-hooks.d/debug
/etc/dhcp3/dhclient-exit-hooks.d/rfc3442-classless-routes
/etc/dhcp3/dhclient.conf
/etc/apparmor.d
/etc/apparmor.d/force-complain
/etc/apparmor.d/sbin.dhclient3
/etc/network
/etc/network/if-pre-up.d
/var
/var/run
/var/lib
/var/lib/dhcp3
/usr
/usr/share
/usr/share/man
/usr/share/man/man5
/usr/share/man/man5/dhclient.conf.5.gz
/usr/share/man/man5/dhclient.leases.5.gz
/usr/share/man/man8
/usr/share/man/man8/dhclient3.8.gz
/usr/share/man/man8/dhclient-script.8.gz
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/dhcp3-client
/usr/share/doc
/usr/share/doc/dhcp3-client
/usr/share/doc/dhcp3-client/README.Debian
/usr/share/doc/dhcp3-client/copyright
/usr/share/doc/dhcp3-client/NEWS.Debian.gz
/usr/share/doc/dhcp3-client/changelog.Debian.gz
/sbin
/sbin/dhclient3
/sbin/dhclient-script
/usr/share/man/man8/dhclient.8.gz
/sbin/dhclient


Instal·lació del servidor

A sistemes de la família Debian es pot instal·lar el servidor DHCP del ISC des dels repositoris utilitzant l'ordre apt-get:

$ sudo apt-get install dhcp3-server

Configuració DHCP

Configuració Client

La configuració del client és la més fàcil. Per configurar un equip GNU/Linu conectat a un servidor DHCP, només es necessari dir-li a la targeta de xarxa que treballarà amb DHCP.

  • Configuració targeta de xarxa:
  1. Anar a Sistema - Administració - Xarxa
  2. Seleccionar interfície Ethernet i fen clic a Propietats.
  3. Es pot canviar la forma en que l'interfície es configura (DHCP o manualment).

Consulteu l'article:

Exemple de fitxer interfaces

Un exemple de com configurar el fitxer interfaces per tal d'utilitzar dhcp:

$ cat /etc/network/interfaces
auto lo
iface lo inet loopback 

auto eth0
iface eth0 inet dhcp


dhcp3-client

El paquet amb el client de dhcp és dhcp3-client. Normalment ja esta instal·lat. Si executem

$ dpkg -L dhcp3-client 
/.
/etc
/etc/dhcp3
/etc/dhcp3/dhclient-enter-hooks.d
/etc/dhcp3/dhclient-enter-hooks.d/debug
/etc/dhcp3/dhclient-exit-hooks.d
/etc/dhcp3/dhclient-exit-hooks.d/debug
/etc/dhcp3/dhclient.conf
/var
/var/run
/var/lib
/var/lib/dhcp3
/usr
/usr/share
/usr/share/man
/usr/share/man/man5
/usr/share/man/man5/dhclient.conf.5.gz
/usr/share/man/man5/dhclient.leases.5.gz
/usr/share/man/man8
/usr/share/man/man8/dhclient3.8.gz
/usr/share/man/man8/dhclient-script.8.gz
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/dhcp3-client
/usr/share/doc
/usr/share/doc/dhcp3-client
/usr/share/doc/dhcp3-client/README.Debian
/usr/share/doc/dhcp3-client/copyright
/usr/share/doc/dhcp3-client/changelog.Debian.gz
/sbin
/sbin/dhclient3
/sbin/dhclient-script
/lib
/lib/dhcp3-client
/lib/dhcp3-client/call-dhclient-script
/usr/share/man/man8/dhclient.8.gz
/sbin/dhclient

Podem veure els fitxers que formen part del paquet.

Fitxers executables:

$ dpkg -L dhcp3-client | grep bin
/sbin
/sbin/dhclient3
/sbin/dhclient-script
/sbin/dhclient

Fitxers de configuració:

$ dpkg -L dhcp3-client | grep etc
/etc
/etc/dhcp3
/etc/dhcp3/dhclient-enter-hooks.d
/etc/dhcp3/dhclient-enter-hooks.d/debug
/etc/dhcp3/dhclient-exit-hooks.d
/etc/dhcp3/dhclient-exit-hooks.d/debug
/etc/dhcp3/dhclient.conf

Comanda dhclient

La comanda dhclient és el client DHCP.

Recursos:

dhclient3

Les comandes dhclient i dhclient3 són equivalents. De fet dhclient és un enllaç simbòlic a dhclient3:

$ ls -la /sbin/dhclient
lrwxrwxrwx 1 root root 9 2008-11-18 08:54 /sbin/dhclient -> dhclient3 

Per tant, de fet estem utilitzant la versió 3 de la comanda dclient.

Tal i com ens confirma l'ordre file:

$ file /sbin/dhclient3
/sbin/dhclient3: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped

dhclient3 és un executable binari.

El client es configura al fitxer /etc/dhcp3/dhclient.conf.

L'ordre dhclient sense opcions intenta configurar totes les targetes de xarxa que suportin broadcast:

$ sudo dhclient

Podem indicar només una targeta de xarxa amb:

$ sudo dhclient eth0

Un cop s'ha aconsguit una configuració el client de dhcp passa a executar-se a segon terme, sent un domini que s'encarrega de la gestió del client DHCP (per exemple és l'encarregat de renovar el préstec si el temps de préstec caduca). Del manual:

The DHCP client will normally run in the foreground until it has configured an interface, and then will revert to running in the background.   To run force  dhclient
to  always  run as a foreground process, the -d flag should be specified.  This is useful when running the client under a debugger, or when running it out of inittab
on System V systems.

Podeu veure el client en execució amb:

$ ps aux | grep dhclient
root     13097  0.0  0.0   2144   312 ?        Ss   18:25   0:00 dhclient eth0

Si l'heu executat manualment o:

$ ps aux | grep dhclient
root      1398  0.0  0.0   2140   944 ?        S    11:31   0:00 /sbin/dhclient -d -sf /usr/lib/NetworkManager/nm-dhcp-client.action -pf /var/run 
/dhclient-eth0.pid -lf /var/lib/dhcp3/dhclient-b7d96c9c-0c95-49fd-9ea5-7f6a6563ed62-eth0.lease -cf /var/run/nm-dhclient-eth0.conf eth0

si ho fa Network Manager o:

$ ps aux | grep dhclient
root     13948  0.0  0.0   2144   308 ?        Ss   18:44   0:00 dhclient3 -e IF_METRIC=100 -pf /var/run/dhclient.eth0.pid -lf /var/lib/dhcp3/dhclient.eth0.leases eth0

NOTA: Tingueu en compte que podeu perfectament tenir dos clients en execució si combineu comandes amb sistemes com Network-manager o la configuració clàsica de xarxa a Linux. Heu de tenir en compte que això pot provocar que us caduqui un configuració manual per culpa de la configuració del sistema. Podeu matar els clients de sobres amb kill

Els missatges de configuració inicials apareixen per la línia d'ordres però un cop passa a executar-se com dimoni els missatges s'envien a syslog.

El client dhcp utilitza els següents fitxers:

Es poden canviar aquests fitxers per defecte amb els paràmetres -cf, -lf, -pf i -sf.

La configuració de les interfícies de xarxa les fa el fitxer /sbin/dhclient-script. POdeu obtenir més informació sobre el fitxer al manual:

$ man dhclient-script

Aquest fitxer no està pensat per ser modificat. Si cal fer algun canvi s'han de modificar els DHCP Hooks:




Si es vol modificar el comportament de la configuració de xarxa



Segons el manual:

$ man dhclient:
 If the client is killed by a signal (for example at shutdown or reboot) it won't execute the dhclient-script (8) at exit. However if you shut the client down  grace‐
      fully with -r or -x it will execute dhclient-script (8) at shutdown with the specific reason for calling the script set.

Comanda dhclient-script

$ cat /sbin/dhclient-script
Contingut de /sbin/dhclient-script

Consulteu DHCP hooks

Comanda dhcping

Instal·lació:

$ sudo apt-get install dhcping

Segons el manual:

$ man dhcping
...
dhcping - send a DHCP request to DHCP server to see if it's up and running

És una ordre més un manual:

$ dpkg -L dhcping
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/dhcping
/usr/share/doc/dhcping/copyright
/usr/share/doc/dhcping/changelog.Debian.gz
/usr/share/man
/usr/share/man/man8
/usr/share/man/man8/dhcping.8.gz
/usr/sbin
/usr/sbin/dhcping

Per exemple:

$ dhcping -h 01:02:03:04 -s 192.168.1.30
This program should be installed setuid root or ran by root only.  See SECURITY for more information.

      On your DHCP server, add these lines to the dhcpd.conf:

        host <your monitoring host FQDN> {
           hardware ethernet <your monitor host mac address>;
           fixed-address     <your monitoring host IP address>;
        }

      Then try it:

      $ dhcping -c your monitoring host IP address \
                -s your DHCP server IP address \
                -h your monitor host mac address

      It will either respond with "no answer" or "Got answer from: your DHCP server IP address"

      The DHCP server logfile will give:
       DHCPREQUEST for 192.168.1.1 from 00:20:18:56:29:8f via ed0
       DHCPACK on 192.168.1.1 to 00:20:18:56:29:8f via ed0
       DHCPRELEASE of 192.168.1.1 from 00:20:18:56:29:8f via ed0 (found)

      Running in DHCPINFORM mode with -i:

      If you see "DHCPINFORM from 192.168.1.1 via xl0: not authoritative for subnet 192.168.1.0", you should add the authoritative statement to the subnet, See
      dhcpd.conf(5) for details.

      When running in very verbose mode, dhcping tries to dump all data of the send and received DHCP packets. It will first dump the packet in hex-format, then decodes
      the header and finally the options.

Estats del client

Procés complet. Amb inicialització:

$ sudo dhclient eth0
Internet Systems Consortium DHCP Client V3.0.5
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/ 

Listening on LPF/eth0/00:30:1b:b7:cd:b6
Sending on   LPF/eth0/00:30:1b:b7:cd:b6
Sending on   Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 6
DHCPOFFER from 192.168.1.1
DHCPREQUEST on eth0 to 255.255.255.255 port 67
DHCPACK from 192.168.1.1
bound to 192.168.1.4 -- renewal in 1540 seconds. 

Procés de rebinding:

$ sudo dhclient eth0
There is already a pid file /var/run/dhclient.pid with pid 22064
killed old client process, removed PID file
Internet Systems Consortium DHCP Client V3.0.5
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/

Listening on LPF/eth0/00:30:1b:b7:cd:b6
Sending on   LPF/eth0/00:30:1b:b7:cd:b6
Sending on   Socket/fallback
DHCPREQUEST on eth0 to 255.255.255.255 port 67
DHCPACK from 192.168.1.1
bound to 192.168.1.4 -- renewal in 1639 seconds.

El client DHCP és un procés que és manté en execució pendent de controlar el client DHCP (renovar el préstec). Podeu veure el procés executant:

$ ps aux | grep dhcp
dhcp     22147  0.0  0.0   2420   556 ?        Ss   11:09   0:00 dhclient eth0
sergi    22172  0.0  0.0   2988   764 pts/1    R+   11:10   0:00 grep dhcp

Si matem el procés:

$ sudo kill 2214 

I executem:

$ sudo dhclient eth0
There is already a pid file /var/run/dhclient.pid with pid 22147
removed stale PID file
Internet Systems Consortium DHCP Client V3.0.5
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/

Listening on LPF/eth0/00:30:1b:b7:cd:b6
Sending on   LPF/eth0/00:30:1b:b7:cd:b6
Sending on   Socket/fallback
DHCPREQUEST on eth0 to 255.255.255.255 port 67
DHCPACK from 192.168.1.1
bound to 192.168.1.4 -- renewal in 1535 seconds.

No és suficient amb matar el procés i esborrar el fitxer PID: /var/run/dhclient.pid. Recordeu que el client guarda els seus préstecs al fitxer:

$ cat /var/lib/dhcp3/dhclient.leases
lease {
  interface "eth0";
  fixed-address 192.168.1.4;
  option subnet-mask 255.255.255.0;
  option dhcp-lease-time 3600;
  option routers 192.168.1.1;
  option dhcp-message-type 5;
  option dhcp-server-identifier 192.168.1.1;
  option domain-name-servers 192.168.1.3,192.168.1.1;
  option host-name "ubuntu-sala";
  renew 4 2008/1/24 10:36:39;
  rebind 4 2008/1/24 11:03:34;
  expire 4 2008/1/24 11:11:04;
}

Podeu forçar tornar a inicialitzar executant:

$ sudo dhclient -r eth0
There is already a pid file /var/run/dhclient.pid with pid 134519120
Internet Systems Consortium DHCP Client V3.0.5
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/ 

Listening on LPF/eth0/00:30:1b:b7:cd:b6
Sending on   LPF/eth0/00:30:1b:b7:cd:b6
Sending on   Socket/fallback
DHCPRELEASE on eth0 to 192.168.1.1 port 67

Aquesta comanda envia un RELEASE al servidor. Ara el client començara des de l'estat d'inicialització:

$ sudo dhclient eth0
There is already a pid file /var/run/dhclient.pid with pid 134519120
Internet Systems Consortium DHCP Client V3.0.5
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/  

Listening on LPF/eth0/00:30:1b:b7:cd:b6
Sending on   LPF/eth0/00:30:1b:b7:cd:b6
Sending on   Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 4
DHCPOFFER from 192.168.1.1
DHCPREQUEST on eth0 to 255.255.255.255 port 67
DHCPACK from 192.168.1.1
bound to 192.168.1.4 -- renewal in 1456 seconds. 


http://www.tcpipguide.com/free/diagrams/dhcpfsm.png

Proves amb DHCP

Per tal de fer proves pot ser interessant enviar peticions DHCP a màquines concretes en comptes de enviar missatges de difusió. Podem utilitzar el paràmetre -s

NOTA: Habitualment aquesta opció no funciona. Consulteu DHCP#Com_comprovar_un_servidor_DHCP_en_un_entorn_amb_m.C3.BAltiples_servidors

$ sudo dhclient -s 192.168.1.1 eth0
There is already a pid file /var/run/dhclient.pid with pid 134519120
Internet Systems Consortium DHCP Client V3.0.5
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/  

Listening on LPF/eth0/00:30:1b:b7:cd:b6
Sending on   LPF/eth0/00:30:1b:b7:cd:b6
Sending on   Socket/fallback
DHCPREQUEST on eth0 to 192.168.1.1 port 67
DHCPACK from 192.168.1.1
bound to 192.168.1.4 -- renewal in 1619 seconds.

Com comprovar un servidor DHCP en un entorn amb múltiples servidors

Si tenim múltiples servidors de DHCP en un mateix segment de xarxa és difícil fer pràctiques ja que a priori no és possible determinar quin serà el servidor que es configurarà primer.

Podem però facilitar la feina configurant el client. Curiosament no existeix (AFAIK) un paràmetre per indicar al client quin servidor de DHCP ha d'utilitzar, però si que podem indicar quins servidors de DHCP NO volem utilitzar. Això es fa amb la directiva reject del fitxer /etc/dhcp3/dhclient.conf:

$ cat /etc/dhcp3/dhclient.conf
.....
reject 192.168.1.1;

On 192.168.1.1 és un servidor de DHCP que no volem utilitzar. Vegem un exemple del que succeix amb una configuració com l'anterior:

$ sudo dhclient br0
killed old client process, removed PID file
There is already a pid file /var/run/dhclient.pid with pid 13584
.............
DHCPREQUEST on br0 to 255.255.255.255 port 67
DHCPACK from 192.168.1.1 rejected.
...
DHCPDISCOVER on br0 to 255.255.255.255 port 67 interval 8
DHCPOFFER from 192.168.1.6
DHCPREQUEST on br0 to 255.255.255.255 port 67
DHCPOFFER from 192.168.1.1 rejected.
DHCPACK from 192.168.1.6
bound to 192.168.1.199 -- renewal in 32725 seconds.


Préstecs

A la carpeta:

/var/lib/dhcp3

Es guarden els préstecs del client.Per exemple, una màquina en un disc dur extraible que es connecta a varies xarxes diferents:

$ cat  /var/lib/dhcp3/dhclient.leases
lease {
  interface "eth1";
  fixed-address 192.168.204.2;
  option subnet-mask 255.255.255.0;
  option dhcp-lease-time 3600;
  option routers 192.168.204.1;
  option dhcp-message-type 5;
  option dhcp-server-identifier 192.168.204.1;
  option domain-name-servers 192.168.204.1,213.4.132.1;
  option host-name "profaula4";
  option domain-name "aula4.iescopernic.com";
  renew 2 2008/1/15 18:01:47;
  rebind 2 2008/1/15 18:29:11;
  expire 2 2008/1/15 18:36:41;
}
 lease { 
  interface "eth2";
  fixed-address 192.168.12.20;
  option subnet-mask 255.255.255.0;
  option routers 192.168.12.1;
  option dhcp-lease-time 86400;
  option dhcp-message-type 5;
  option domain-name-servers 192.168.0.7,195.235.113.3,195.235.96.90;
  option dhcp-server-identifier 192.168.12.1;
  option broadcast-address 192.168.12.255;
  option domain-name "iescopernic.com";
  renew 6 2007/12/15 03:15:18;
  rebind 6 2007/12/15 13:48:02;
  expire 6 2007/12/15 16:48:02;
}
lease {
  interface "eth3";
  fixed-address 192.168.201.2;
  option subnet-mask 255.255.255.0;
  option routers 192.168.201.1;
  option dhcp-lease-time 3600;
  option dhcp-message-type 5;
  option domain-name-servers 192.168.201.1,195.235.113.3;
  option dhcp-server-identifier 192.168.201.1;
  option host-name "profaula1";
  option domain-name "aula1";
  renew 1 2007/12/10 21:18:04;
  rebind 1 2007/12/10 21:41:33;
  expire 1 2007/12/10 21:49:03;
}
lease {
  interface "eth6";
  fixed-address 192.168.206.2;
  option subnet-mask 255.255.255.0;
  option dhcp-lease-time 3600;
  option routers 192.168.206.1;
  option dhcp-message-type 5;
  option dhcp-server-identifier 192.168.206.1;
  option domain-name-servers 192.168.206.1,195.235.113.3;
  option host-name "profaula6"; 
  option domain-name "aula6";
  renew 5 2007/11/30 16:52:55;
  rebind 5 2007/11/30 17:20:37;
  expire 5 2007/11/30 17:28:07;
}
lease {
  interface "eth4";
  fixed-address 192.168.202.2;
  option subnet-mask 255.255.255.0;
  option dhcp-lease-time 3600;
  option routers 192.168.202.1;
  option dhcp-message-type 5;
  option dhcp-server-identifier 192.168.202.1;
  option domain-name-servers 192.168.202.1,213.4.132.1;
  option host-name "profaula2";
  option domain-name "aula2.iescopernic.com";
  renew 5 2007/11/30 16:07:24;
  rebind 5 2007/11/30 16:31:05;
  expire 5 2007/11/30 16:38:35; 
}
lease {
  interface "eth5";
  fixed-address 192.168.207.2;
  option subnet-mask 255.255.255.0;
  option routers 192.168.207.1;
  option dhcp-lease-time 3600;
  option dhcp-message-type 5;
  option domain-name-servers 192.168.207.1,195.235.113.3;
  option dhcp-server-identifier 192.168.207.1;
  option host-name "profaula";
  option domain-name "aula7.iescopernic.com";
  renew 5 2007/11/30 16:28:42;
  rebind 5 2007/11/30 16:53:49;
  expire 5 2007/11/30 17:01:19;
}
lease {
  interface "eth0";
  fixed-address 192.168.208.2;
  option subnet-mask 255.255.255.0;
  option routers 192.168.208.1;
  option dhcp-lease-time 3600;
  option dhcp-message-type 5;
  option domain-name-servers 192.168.208.1,213.4.132.1;
  option dhcp-server-identifier 192.168.208.1;
  option host-name "profaula8";
  option domain-name "aula8.iescopernic.com";
  renew 5 2007/11/30 17:14:46;
  rebind 5 2007/11/30 17:37:43;
  expire 5 2007/11/30 17:45:13;
}
lease {
  interface "eth1";
  fixed-address 192.168.204.2;
  option subnet-mask 255.255.255.0;
  option routers 192.168.204.1;
  option dhcp-lease-time 3600;
  option dhcp-message-type 5;
  option domain-name-servers 192.168.204.1,213.4.132.1;
  option dhcp-server-identifier 192.168.204.1;
  option host-name "profaula4";
  option domain-name "aula4.iescopernic.com";
  renew 2 2008/1/15 18:04:17;
  rebind 2 2008/1/15 18:29:24;
  expire 2 2008/1/15 18:36:54;
}  

Consulteu:

$ man dhclient.leases

Per obtenir més informació

Documentació

Client Windows

Amb l'ordre ipconfig es pot alliberar i tornar a demanar una connexió. Per alliberar:

> ipconfig /release

Per tornar a demanar configuració IP:

> ipconfig /renew

Configuració Servidor

Descarregar el servidor DHCP de l'ISC:

$ sudo apt-get install dhcpd

Instal·lat el programari del servidor, s'haurà de configurar dos arxius importants: /etc/dhcpd.conf, i el /var/state/dhcp/dhcp.leases. El primer serveix per configurar el servidor com es vulgui, mentre que el segon es una base de dades creada per el servidor, amb les assignacions de IP que es van realitzant. Ens permet verificar la activitat del servidor i permet portar la conta de les IPs que va donant als diferents clients, per conservar-les en cas de caiguda.

Verificar que existeix l'archiu /var/lib/dhcp/dhcpd.leases (en versions anteriors de dhcp per RedHat aquest arxiu es tenia que crear-lo a mà. I era una de les raons perquè dhcpd no iniciava).

DHCPD.CONF

  • Abans de modificar /etc/dhcpd.conf s'ha d'entendre una mica els paràmetres de configuració més comuns:
#subnet 192.5.5.0 netmask 255.255.255.224 {
#  range 192.5.5.26 192.5.5.30;
#  option domain-name-servers bb.home.vix.com, gw.home.vix.com;
#  option domain-name "vix.com";
#  option routers 192.5.5.1;
#  option subnet-mask 255.255.255.224;
#  option broadcast-address 192.5.5.31;
#  default-lease-time 600;
#  max-lease-time 7200;
#}
  1. Descriu la xarxa i es posible definir més d'una subxarxa, en el cas que comparteixin la mateixa infraestructura física.
  2. El server dhcpd proporcionarà la primera IP que estigui lliure entre el rang especificant, s'anomena asignació dinàmica de IPs.
  3. Indica el domini dels servidors DNS que es faran servir en els clients DHCP.
  4. Indica el nom de domini que es fara servir en la DNS.
  5. Port de sortida (indica els routers de la xarxa)
  6. Indica la màscara de xarxa.
  7. Indica la direcció de broadcast.
  8. La IP estara per un temps determinat cuan el temps s'acabi el client tindra que solicitar una altre IP.
  • Una llista del valors (de segons a minuts):
600 - 10 minuts
7200 - 2 hores
86400 - 1 dia
604800 - 1 setmana
2592000 - 1 mes
31104000 - 1 any
62208000 - 2 anys

DHCP.LEASES

  • El fitxer dhcp.leases es registran les IPs que es van asignant als ordinadors clients, cuant aquest es masa gran crea una copia en un fitxer dhcpd.leases~, aquests fitxers estan normalment en /etc o /var.

Paràmetres d'execució

En sistemes d'inicialització basats en System V, es poden especificar paràmetres extres d'execució del servidor als fitxes de la carpeta /etc/default:

$ cat /etc/default/dhcp3-server 
# Defaults for dhcp initscript
# sourced by /etc/init.d/dhcp
# installed at /etc/default/dhcp3-server by the maintainer scripts  

#
# This is a POSIX shell fragment
#

# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
#	Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACES=""

En el nostre cas li estem indicant que escolti peticions provinents de totes les interfícies de xarxa.

Dubte: que passa amb les interfícies virtuals fetes amb IP aliasing, cal posar-les?

Consultar l'estat d'execució del servidor DHCP

$ ps aux | grep dhcpd
dhcpd     5869  0.0  0.1   2988  1548 ?        Ss   Feb05   0:00 /usr/sbin/dhcpd3 -q -pf /var/run/dhcp3-server/dhcpd.pid -cf  /etc/dhcp3/dhcpd.conf

Recursos:


Adreça IP fixa per MAC

Per assignar una IP fixa segons l'adreça MAC (exemple extret del fitxer d'exemple de Debian):

host fantasia {
  hardware ethernet 08:00:07:26:c0:a5;
  fixed-address 192.168.0.5;
}

Es poden tenir diverses IP, aleshores només s'assignarà la IP del rang que correspon (la petició haurà arribat per una targeta de xarxa que pertany a certa subxarxa). També es pot assignar un nom de màquina DNS que resolgui a una iP concreta.

Consulteu el manual:

$ man dhcpd.conf

          fixed-address address [, address ... ];

         The fixed-address declaration is used to assign one or more fixed IP addresses to a client.  It should only appear in  a  host  declaration.   If  more  than  one
         address is supplied, then when the client boots, it will be assigned the address that corresponds to the network on which it is booting.  If none of the addresses
         in the fixed-address statement are valid for the network to which the client is connected, that client will not match the host declaration containing that  fixed-
         address declaration.  Each address in the fixed-address declaration should be either an IP address or a domain name that resolves to one or more IP addresses.

Exemple de configuració. Aula amb adreces IP fixes

Un departament d'informàtica amb assignació fixa per MAC:

$ sudo cat /etc/dhcp3/dhcpd.conf
ddns-update-style none;

option domain-name-servers 192.168.0.7, 80.58.61.250, 80.58.61.254;
option domain-name "iescopernic.com";
default-lease-time 86400;
max-lease-time 604800; 

authoritative;  


# *******************   Ordinadors Departament **************
#subnet 192.168.12.0 netmask 255.255.255.0
include "/etc/dhcp3/dhcpd-departament.conf";

El fitxer inclòs:

$ cat /etc/dhcp3/dhcpd-departament.conf
subnet 192.168.12.0 netmask 255.255.255.0 {
        range 192.168.12.100 192.168.12.199;
        option subnet-mask 255.255.255.0;
        option broadcast-address 192.168.12.255;
        option routers 192.168.12.1;
        option domain-name-servers 192.168.0.7, 195.235.113.3, 195.235.96.90;
}

host impresoraLaserjet2420 {
        hardware ethernet 00:14:38:D4:55:48;
        fixed-address 192.168.12.50;
} 

host pc01 {
            hardware ethernet 00:30:05:EB:39:9D;
            fixed-address 192.168.12.11;
}

host pc02 {    
            hardware ethernet 00:30:05:EB:A3:98;
            fixed-address 192.168.12.12; 
}  

host pc03 {    
            hardware ethernet 00:30:05:EB:3A:E7;
            fixed-address 192.168.12.13; 
}  

host pc04 {    
            hardware ethernet 00:30:05:EB:3A:EA;
            fixed-address 192.168.12.14; 
}   

host pc05 {    
            hardware ethernet 00:30:05:EB:39:A4;
            fixed-address 192.168.12.15; 
}  

host pc06 {    
            hardware ethernet 00:30:05:EB:3B:52;
            fixed-address 192.168.12.16; 
}  

host pc07 {    
            hardware ethernet 00:30:05:EB:3A:CF;
            fixed-address 192.168.12.17; 
}   

host pc08 {    
            hardware ethernet 00:30:05:EB:A4:12;
            fixed-address 192.168.12.18; 
}   

host pc09 {    
            hardware ethernet 00:30:05:EB:A3:9B;
            fixed-address 192.168.12.19; 
}  

host pc10 {    
            hardware ethernet 00:30:05:EB:A3:8D;
            fixed-address 192.168.12.20; 
}

Fitxer d'exemple dhcpd.conf de Debian

$ cat /etc/dhcp3/dhcpd.conf
#
# Sample configuration file for ISC dhcpd for Debian
#
# $Id: dhcpd.conf,v 1.1.1.1 2002/05/21 00:07:44 peloy Exp $
# 

# The ddns-updates-style parameter controls whether or not the server will
# attempt to do a DNS update when a lease is confirmed. We default to the
# behavior of the version 2 packages ('none', since DHCP v2 didn't
# have support for DDNS.)
ddns-update-style none; 

# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

default-lease-time 600;
max-lease-time 7200; 

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative; 

# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;

# No service will be given on this subnet, but declaring it helps the 
# DHCP server to understand the network topology.

#subnet 10.152.187.0 netmask 255.255.255.0 {
#}

# This is a very basic subnet declaration.

#subnet 10.254.239.0 netmask 255.255.255.224 {
#  range 10.254.239.10 10.254.239.20;
#  option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
#}

# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.

#subnet 10.254.239.32 netmask 255.255.255.224 {
#  range dynamic-bootp 10.254.239.40 10.254.239.60;
#  option broadcast-address 10.254.239.31;
#  option routers rtr-239-32-1.example.org;
#}

# A slightly different configuration for an internal subnet.
#subnet 10.5.5.0 netmask 255.255.255.224 {
#  range 10.5.5.26 10.5.5.30;
#  option domain-name-servers ns1.internal.example.org;
#  option domain-name "internal.example.org";
#  option routers 10.5.5.1;
#  option broadcast-address 10.5.5.31;
#  default-lease-time 600;
#  max-lease-time 7200;
#}

# Hosts which require special configuration options can be listed in
# host statements.   If no address is specified, the address will be
# allocated dynamically (if possible), but the host-specific information
# will still come from the host declaration. 

#host passacaglia {
#  hardware ethernet 0:0:c0:5d:bd:95;
#  filename "vmunix.passacaglia";
#  server-name "toccata.fugue.com";
#}

# Fixed IP addresses can also be specified for hosts.   These addresses
# should not also be listed as being available for dynamic assignment.
# Hosts for which fixed IP addresses have been specified can boot using
# BOOTP or DHCP.   Hosts for which no fixed address is specified can only
# be booted with DHCP, unless there is an address range on the subnet
# to which a BOOTP client is connected which has the dynamic-bootp flag
# set.
#host fantasia {
#  hardware ethernet 08:00:07:26:c0:a5;
#  fixed-address fantasia.fugue.com;
#}

# You can declare a class of clients and then do address allocation
# based on that.   The example below shows a case where all clients
# in a certain class get addresses on the 10.17.224/24 subnet, and all
# other clients get addresses on the 10.0.29/24 subnet. 

#class "foo" {
#  match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
#} 

#shared-network 224-29 {
#  subnet 10.17.224.0 netmask 255.255.255.0 {
#    option routers rtr-224.example.org;
#  }
#  subnet 10.0.29.0 netmask 255.255.255.0 {
#    option routers rtr-29.example.org;
#  }
#  pool {
#    allow members of "foo";
#    range 10.17.224.10 10.17.224.250;
#  }
#  pool {
#    deny members of "foo";
#    range 10.0.29.10 10.0.29.230;
#  }
#}


Servidor WINS

Es poden configurar els servidors de WINS dels clients amb l'opció:

option netbios-name-servers 192.168.10.66, 192.168.10.97;

També podem modificar el tipus de nodes:

option netbios-node-type 2;

Segons la taula:

  • 1: b-node
  • 2: p-node
  • 4: m-node
  • 8: h-node

Consulteu NetBIOS_Name_Server#Tipus_de_nodes_NetBIOS.

Fitxers de log

$ tail -n 100 /var/log/syslog | grep dhcpd 
Sep 19 16:01:26 server1 dhcpd: DHCPDISCOVER from 00:0c:76:8b:c4:16 via eth0
Sep 19 16:01:26 server1 dhcpd: DHCPOFFER on 192.168.0.229 to 00:0c:76:8b:c4:16 (matze) via eth0
Sep 19 16:01:27 server1 dhcpd: DHCPDISCOVER from 00:0c:76:8b:c4:16 (matze) via eth0
Sep 19 16:01:27 server1 dhcpd: DHCPOFFER on 192.168.0.229 to 00:0c:76:8b:c4:16 (matze) via eth0
Sep 19 16:01:31 server1 dhcpd: DHCPDISCOVER from 00:0c:76:8b:c4:16 (matze) via eth0
Sep 19 16:01:31 server1 dhcpd: DHCPOFFER on 192.168.0.229 to 00:0c:76:8b:c4:16 (matze) via eth0
Sep 19 16:01:31 server1 dhcpd: Wrote 1 leases to leases file.
Sep 19 16:01:31 server1 dhcpd: DHCPREQUEST for 192.168.0.229 (192.168.0.100) from 00:0c:76:8b:c4:16 (matze) via eth0
Sep 19 16:01:31 server1 dhcpd: DHCPACK on 192.168.0.229 to 00:0c:76:8b:c4:16 (matze) via eth0

Préstecs (leases)

$ cat /var/lib/dhcp3/dhcpd.leases
# All times in this file are in UTC (GMT), not your local timezone.   This is
# not a bug, so please don't ask about it.   There is no portable way to
# store leases in the local timezone, so please don't request this as a
# feature.   If this is inconvenient or confusing to you, we sincerely
# apologize.   Seriously, though - don't ask.
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-V3.0.5

lease 192.168.12.197 {
  starts 5 2007/06/22 14:20:56;
  ends 6 2007/06/23 14:20:56;
  tstp 6 2007/06/23 14:20:56;
  binding state free;
  hardware ethernet 00:17:a4:42:a8:5c;
}
lease 192.168.12.192 {
  starts 4 2007/06/28 15:19:44;
  ends 5 2007/06/29 15:19:44;
  tstp 5 2007/06/29 15:19:44;
  binding state free;
  hardware ethernet 00:c0:9f:60:33:51;
}
lease 192.168.12.191 {
  starts 4 2007/06/28 16:19:10;
  ends 5 2007/06/29 16:19:10;
  tstp 5 2007/06/29 16:19:10;
  binding state free;
  hardware ethernet 00:0e:9b:cd:74:4c;
  uid "\001\000\016\233\315tL";
}
lease 192.168.12.193 {
  starts 4 2007/06/28 16:35:59;
  ends 5 2007/06/29 16:35:59;
  tstp 5 2007/06/29 16:35:59;
  binding state free;
  hardware ethernet 00:c0:9f:60:33:51;
}   
..................

Accés a un servidor DHCP fora del segment de xarxa dels clients

Cal utilitzar un BOOTP/DHCP Relay Agent als routers. Paquet dhcp3-relay.

TODO


Exemple AulaLinux

Obtenir les MAC:

$ sudo nmap 147.83.75.129/25
.....

Consultar les MAC:

$ sudo arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
147.83.75.143            ether   00:C0:A8:F9:7D:3D   C                     eth1
147.83.75.141            ether   00:C0:A8:F9:86:6C   C                     eth1
147.83.75.129            ether   00:0E:D6:A9:54:00   C                     eth1
147.83.75.131            ether   00:16:35:3C:3B:D1   C                     eth1
147.83.75.136            ether   00:C0:A8:FB:5E:A1   C                     eth1
147.83.75.144            ether   00:C0:A8:F9:8B:43   C                     eth1
147.83.75.142            ether   00:C0:A8:F9:82:1C   C                     eth1
147.83.75.146            ether   00:C0:A8:F9:7B:87   C                     eth1
147.83.75.145            ether   00:C0:A8:F9:86:59   C                     eth1

Instal·lar el servidor de DHCP:

$ sudo apt-get install dhcp3-server
S'està llegint la llista de paquets... Fet
S'està construint l'arbre de dependències... Fet
S'instal·laran els següents paquets NOUS:
  dhcp3-server
0 actualitzats, 1 nous a instal·lar, 0 a eliminar i 22 no actualitzats.
Es necessita obtenir 292kB d'arxius.
Després de desempaquetar s'usaran 836kB d'espai en disc addicional.
Des:1 http://ftp.rediris.es etch/main dhcp3-server 3.0.4-13 [292kB]
292kB descarregats en 0s (982kB/s)
S'estan preconfigurant els paquets...
S'està seleccionant el paquet dhcp3-server prèviament no seleccionat.
(S'està llegint la base de dades ... hi ha 90512 fitxers i directoris instal·lats actualment.)
S'està desempaquetant dhcp3-server (de .../dhcp3-server_3.0.4-13_i386.deb) ...
S'està configurant dhcp3-server (3.0.4-13) ...
Generating /etc/default/dhcp3-server...
Starting DHCP server: dhcpd3 failed to start - check syslog for diagnostics.
invoke-rc.d: initscript dhcp3-server, action "start" failed.

L'error és per què cal configurar el servidor.

Fitxer:NoAuthoritativeServer.png

Fem una copia de seguretat del fitxer per defecte de configuració:

$ sudo mv /etc/dhcp3/dhcpd.conf /etc/dhcp3/dhcpd.conf.old

Editem el fitxer

$ sudo joe /etc/dhcp3/dhcpd.conf

El millor és utilitzar una plantilla per començar:

ddns-update-style none;
option domain-name-servers 80.58.61.250, 80.58.61.254;
#Si tenim un servidor de DNS propi a la xarxa la configuració sería:
#option domain-name-servers ip_servidor_dns 80.58.61.250, 80.58.61.254;
#option domain-name "casa";
default-lease-time 86400;
max-lease-time 604800;
authoritative; 
subnet 192.168.1.0 netmask 255.255.255.0 {
        range 192.168.1.100 192.168.1.199;
        option subnet-mask 255.255.255.0;
        option broadcast-address 192.168.1.255;
        option routers 192.168.1.1;
}

I la modifiquem al nostre gust. Per exemple per l'aula Linux:

ddns-update-style none;
option domain-name-servers  147.83.21.41, 147.83.2.3;
option domain-name "aulalinux";
default-lease-time 86400;
max-lease-time 604800;
authoritative; 
subnet 147.83.75.129 netmask 255.255.255.128 {
        range 192.168.1.130 192.168.1.150;
        option subnet-mask 255.255.255.254;
        option broadcast-address 147.83.75.255;
        option routers 147.83.75.129;
}

#HOSTS FIXES
host ali01 {
       option host-name "ali01.aulalinux";
       hardware ethernet 00:16:35:3C:3B:D1;
       fixed-address 147.83.75.131;
       }

host ali02 {
       option host-name "ali02.aulalinux";
       hardware ethernet XXXXXX;
       fixed-address 147.83.75.132;
       }
.........

Després de modificar el fitxer cal tornar a iniciar el servidor:

$ sudo /etc/init.d/dhcp3-server restart

Xarxes compartides (shared networks). Múltiples interfícies virtuals en una mateixa targeta de xarxa física

Si voleu utilitzar un servidor DHCP en una targeta de xarxa que té múltiples IP lògiques (IP Aliasing) cal definir una xarxa compartida. Per exemple un targeta de xarxa com la següent:

$ ifconfig | grep -A 1 alumnat
alumnat   Link encap:Ethernet  HWaddr 00:16:3e:00:02:b1  
          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
--
alumnat:1 Link encap:Ethernet  HWaddr 00:16:3e:00:02:b1  
          inet addr:192.168.2.1  Bcast:192.168.2.255  Mask:255.255.255.0
--
alumnat:2 Link encap:Ethernet  HWaddr 00:16:3e:00:02:b1  
          inet addr:192.168.3.1  Bcast:192.168.3.255  Mask:255.255.255.0
--
alumnat:3 Link encap:Ethernet  HWaddr 00:16:3e:00:02:b1  
         inet addr:192.168.4.1  Bcast:192.168.4.255  Mask:255.255.255.0

La configuració seria:

shared-network alumnat {

#Alumnat planta baixa
subnet 192.168.1.0 netmask 255.255.255.0 {
        option subnet-mask 255.255.255.0;
        range dynamic-bootp 192.168.1.200 192.168.1.254;
        default-lease-time 21600;
        max-lease-time 43200;
        option domain-name-servers      192.168.1.1,192.168.0.46;
        option domain-name "iesebre.com";
        option routers      192.168.1.1; 
        option netbios-name-servers 192.168.0.8; 

        filename "pxelinux.0";
}

#Alumnat planta 1
subnet 192.168.2.0 netmask 255.255.255.0 {              
        option subnet-mask 255.255.255.0;               
        range dynamic-bootp 192.168.2.200 192.168.2.254;
        default-lease-time 21600;
        max-lease-time 43200;
        option domain-name-servers      192.168.2.1,192.168.0.46;
        option domain-name "iesebre.com";  
        option routers      192.168.2.1;   
        option netbios-name-servers 192.168.0.8; 

        filename "pxelinux.0";
}

    
#Alumnat planta 2
subnet 192.168.3.0 netmask 255.255.255.0 {
        option subnet-mask 255.255.255.0; 
       range dynamic-bootp 192.168.3.200 192.168.3.254;
       default-lease-time 21600;
       max-lease-time 43200;
       option domain-name-servers      192.168.3.1,192.168.0.46;
       option domain-name "iesebre.com";            
       option routers      192.168.3.1;              
       option netbios-name-servers 192.168.0.8;      

        filename "pxelinux.0";
}   
    
#Alumnat planta 3
subnet 192.168.4.0 netmask 255.255.255.0 {
        option subnet-mask 255.255.255.0; 
        range dynamic-bootp 192.168.4.200 192.168.4.254;
        default-lease-time 21600;
        max-lease-time 43200;
        option domain-name-servers      192.168.4.1,192.168.0.46;
        option domain-name "iesebre.com";            
        option routers      192.168.4.1;             
        option netbios-name-servers 192.168.0.8;      

        filename "pxelinux.0";
}   
    
}   

Tingueu en compte que al fitxer /etc/default/dhcp3-server només indiqueu la interfície física.

$ sudo cat /etc/default/dhcp3-server 
# Defaults for dhcp initscript
# sourced by /etc/init.d/dhcp
# installed at /etc/default/dhcp3-server by the maintainer scripts 

#
# This is a POSIX shell fragment
#

# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
#	Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACES="intranet alumnat professorat gestio"


Consulteu IP Aliasing.

Establir els dominis de cerca

Si només s'estableix

option domain-name "domini.com"

Aleshores el client tindrà al fitxer /etc/resolv.conf:

$ cat /etc/resolv.conf
...
domain domini.com
search domini.com

TODO...

subnet 192.168.202.0 netmask 255.255.255.0 {
        option subnet-mask              255.255.255.0;
        range dynamic-bootp 192.168.202.200 192.168.202.254;
        default-lease-time 21600;
        max-lease-time 43200;
        option domain-name-servers      192.168.202.1,192.168.0.4;
        option domain-name "informatica.iesebre.com";
        option domain-search "iesebre.com";
        option routers      192.168.202.1;
        option netbios-name-servers 192.168.202.1;  

        filename "pxelinux.0";
}

Els clients quedaran quelcom similar a:

$ cat /etc/resolv.conf
domain informatica.iesebre.com
search informatica.iesebre.com iesebre.com.
nameserver 192.168.202.1
nameserver 192.168.0.4

DHCP hooks

Recordeu que el fitxer dhclient-script és invocat pel client de DHCP (dhclient) en el següents moments:

  • Establir la configuració inicial de les targetes de xarxa abans de demanar una adreça per DHCP
  • Per comprovar les adreces un cop han estat ofertes (DHCP_OFFER)
  • Per establir la configuració final de la targeta de xarxa un cop un préstex ha estat adquirit.
  • Si no s'obté cap prèstec, també s'encarrega d'utilitzar els préstecs predefinits o fins i tot utilitzar algún préstec àntic

Segons el manual:

$ man dhclient-script
...
HOOKS
      When it starts, the client script first defines a shell function, make_resolv_conf , which is later used to create  the  /etc/resolv.conf  file.    To  override  the
      default behaviour, redefine this function in the enter hook script.

      On  after  defining the make_resolv_conf function, the client script checks for the presence of an executable /etc/dhcp3/dhclient-enter-hooks script, and if present,
      it invokes the script inline, using the Bourne shell '.' command.   The entire environment documented under OPERATION is available to this script, which  may  modify
      the  environment  if  needed to change the behaviour of the script.   If an error occurs during the execution of the script, it can set the exit_status variable to a
      nonzero value, and /sbin/dhclient-script will exit with that error code immediately after the client script exits.

      After all processing has completed, /sbin/dhclient-script checks for the presence of an executable /etc/dhcp3/dhclient-exit-hooks script, which if present is invoked
      using  the  '.'  command.  The exit status of dhclient-script will be passed to dhclient-exit-hooks in the exit_status shell variable, and will always be zero if the
      script succeeded at the task for which it was invoked.   The rest of the environment  as  described  previously  for  dhclient-enter-hooks  is  also  present.    The
      /etc/dhcp3/dhclient-exit-hooks script can modify the valid of exit_status to change the exit status of dhclient-script.

És a dir l'script no està pensat per ser modificat directament, cal canviar o el fitxer de configuració /etc/dhcp3/dhclient.conf o cal utilitzar HOOKS.

Els hooks s'utilitzen per a:

  • Establir el fitxer /etc/resolv.conf (funció de shell make_resolv_conf)
  • Es pot canviar aquest funció redefinint-la a algun script hook d'entrada (enter hook script, fitxers de la carpeta dhclient-enter-hooks.d)
  • Quan dhclient ha acabat totes les operacions es crida al fitxers de /etc/dhcp3/dhclient-exit-hooks.d
  • Alguns dels serveis que utilitzen els Hooks són Samba (Configurar el servidor WINS), NTP (configurar els servidors d'hora), entre d'altres.

Es poden passar les següents variables d'entorn:

MEDIUM,  PREINIT,  BOUND,  RENEW, REBIND, REBOOT, EXPIRE, FAIL, STOP, RELEASE, NBI and TIMEOUT.

Per saber quin tipus d'operació cal realitzar. Del manual:

OPERATION
       When  dhclient  needs  to  invoke  the client configuration script, it defines a set of variables in the environment, and then invokes /sbin/dhclient-script.  In all
       cases, $reason is set to the name of the reason why the script has been invoked.   The following reasons  are  currently  defined:  MEDIUM,  PREINIT,  BOUND,  RENEW,
       REBIND, REBOOT, EXPIRE, FAIL, STOP, RELEASE, NBI and TIMEOUT.

Es controlen amb dos carpetes del client:

$ ls -l /etc/dhcp3/
...  
dhclient-enter-hooks.d  
dhclient-exit-hooks.d 

Recursos:

Depurar el client

$ cat /etc/dhcp3/dhclient-enter-hooks.d$ cat debug 
# $Id: debug-enter,v 1.2 2003/06/18 18:11:21 peloy Exp $
#
# The purpose of this script is just to show the variables that are
# available to all the scripts in this directory. All these scripts
# are called from /etc/dhcp3/dhclient-script, which exports all the
# variables shown before. If you want to debug a problem with your DHCP
# setup you can enable this script and take a look at
# /tmp/dhclient-script.debug.

# To enable this script set the following variable to "yes"
RUN="no"

if [ "$RUN" = "yes" ]; then
	echo `date`: entering dhclient-enter-hooks.d, dumping variables. \
		>> /tmp/dhclient-script.debug

	for i in reason interface medium alias_ip_address new_ip_address \
		new_subnet_mask new_domain_name new_domain_search \
		new_domain_name_servers \
		new_routers new_static_routes old_ip_address old_subnet_mask \
		old_domain_name old_domain_search old_domain_name_servers \
		old_routers old_static_routes; do 

		echo $i=\'${!i}\' >> /tmp/dhclient-script.debug
	done

	echo '--------------------------' >> /tmp/dhclient-script.debug
fi

dhcpagent

DHCP Hook for static-routes

No actualitzar mai /etc/resolv.conf per DHCP

Configuració de l'hora per DHCP. NTP Servers

Al servidor cal especificar l'opció:

option ntp-servers 192.168.1.100;

Consulteu l'article NTP.

Recursos:

Client DHCP

El client DHCP del ISC configura el client ntpdate amb el fitxer:

$ cat /etc/dhcp3/dhclient-exit-hooks.d/ntpdate
NTPDATE_CONF=/etc/default/ntpdate
NTPDATE_DHCP_CONF=/etc/default/ntpdate.dhcp

 
ntp_servers_setup_remove() {
	rm -f $NTPDATE_DHCP_CONF
} 


ntp_servers_setup_add() {
	if [ -e $NTPDATE_DHCP_CONF ] && [ "$new_ntp_servers" = "$old_ntp_servers" ]; then
		return

fi

	if [ -z "$new_ntp_servers" ]; then
		ntp_servers_setup_remove
		return
	fi

	tmp=$(mktemp "$NTPDATE_DHCP_CONF.XXXXXX") || return
	chmod --reference=$NTPDATE_CONF $tmp
	chown --reference=$NTPDATE_CONF $tmp

	(
	  echo "# NTP server entries received from DHCP server"
	  echo "NTPSERVERS='$new_ntp_servers'"
	) >>$tmp
	
	mv $tmp $NTPDATE_DHCP_CONF
}


ntp_servers_setup() {
	case $reason in
		BOUND|RENEW|REBIND|REBOOT)
			ntp_servers_setup_add
			;;
		EXPIRE|FAIL|RELEASE|STOP)
			ntp_servers_setup_remove
			;; 
	esac
}
 

ntp_servers_setup


i el servidor NTP gràcies al fitxer:

$ cat /etc/dhcp3/dhclient-enter-hooks.d/ntp
NTP_CONF=/etc/ntp.conf
NTP_DHCP_CONF=/etc/ntp.conf.dhcp


ntp_server_restart() { 
	invoke-rc.d ntp try-restart
} 

ntp_servers_setup_remove() {
	if [ ! -e $NTP_DHCP_CONF ]; then
		return
	fi
	rm -f $NTP_DHCP_CONF
	ntp_server_restart
} 

ntp_servers_setup_add() {
	if [ -e $NTP_DHCP_CONF ] && [ "$new_ntp_servers" = "$old_ntp_servers" ]; then
		return
	fi

	if [ -z "$new_ntp_servers" ]; then
		ntp_servers_setup_remove
		return
	fi

	tmp=$(mktemp "$NTP_DHCP_CONF.XXXXXX") || return
	chmod --reference=$NTP_CONF $tmp
	chown --reference=$NTP_CONF $tmp

	(
	  echo "# This file was copied from $NTP_CONF with the server options changed"
	  echo "# to reflect the information sent by the DHCP server.  Any changes made"
	  echo "# here will be lost at the next DHCP event.  Edit $NTP_CONF instead."
	  echo
	  echo "# NTP server entries received from DHCP server"
	  for server in $new_ntp_servers; do
		echo "server $server iburst"
	  done
	  echo
	  sed -r -e '/^ *(server|peer).*$/d' $NTP_CONF
	) >>$tmp
	
	mv $tmp $NTP_DHCP_CONF

	ntp_server_restart
} 

ntp_servers_setup() {
	case $reason in
		BOUND|RENEW|REBIND|REBOOT)
			ntp_servers_setup_add
			;;
		EXPIRE|FAIL|RELEASE|STOP)
			ntp_servers_setup_remove
			;;
	esac
} 
 

ntp_servers_setup

Configuració del servidor WINS

Consulteu Samba (Samba#Configuraci.C3.B3_amb_DHCP) i WINS (WINS#Configurar_el_servidor_Wins_amb_DHCP).

Exemple de configuració DHCP

Configuració temporal

Durant el primer trimestre del 2008-09 i al principi del segon, s'han anat retirant IPCOP i configurant DHCP aula a aula. Actualment hi ha un fitxer de configuració per aula:

El fitxer principal de configuració és /etc/dhcp3/dhcpd.conf:
ddns-update-style none

option domain-name-servers 192.168.0.7, 80.58.61.250, 80.58.61.254;
option domain-name "iescopernic.com";
default-lease-time 86400;
max-lease-time 604800;

authoritative;  


# *******************   Ordinadors Departament **************
#subnet 192.168.12.0 netmask 255.255.255.0
include "/etc/dhcp3/dhcpd-departament.conf";

# *******************   Ordinadors Aula 1      **************
# subnet 192.168.1.0 netmask 255.255.255.0
include "/etc/dhcp3/dhcpd-aula1.conf";

# *******************   Ordinadors Aula 2      **************
# subnet 192.168.2.0 netmask 255.255.255.0
include "/etc/dhcp3/dhcpd-aula2.conf";  

# *******************   Ordinadors Aula 3      **************
# subnet 192.168.3.0 netmask 255.255.255.0
include "/etc/dhcp3/dhcpd-aula3.conf"; 

# *******************   Ordinadors Aula 4      **************
# subnet 192.168.4.0 netmask 255.255.255.0
include "/etc/dhcp3/dhcpd-aula4.conf";

# *******************   Ordinadors Aula 5      **************
# subnet 192.168.5.0 netmask 255.255.255.0
include "/etc/dhcp3/dhcpd-aula5.conf";

# *******************   Ordinadors Aula 6      **************
# subnet 192.168.6.0 netmask 255.255.255.0
include "/etc/dhcp3/dhcpd-aula6.conf";

# *******************   Ordinadors Aula 7      **************
# subnet 192.168.6.0 netmask 255.255.255.0
include "/etc/dhcp3/dhcpd-aula7.conf";

# *******************   Ordinadors Aula 8      **************
# subnet 192.168.8.0 netmask 255.255.255.0
include "/etc/dhcp3/dhcpd-laboratori.conf"; 

Aquest fitxers s'ha anat obtenint a mà. Un cop estiguin totes les aules, es passarà a la configuració definitiva on tota la configuració del centre està en un únic fitxer que es pot generar amb un script.

Per passar a la configuració definitiva caldrà executar:

$ sudo cp dhcpd.conf.definitiu dhcpd.conf

Configuració definitiva

El servei de DHCP de les aules del departament d'informàtica està centralitzat al router on estan connectades totes les aules. A aquesta màquina l'anomenem:

Router del centre
  • IP: 192.168.02
  • Port SSH: 22

Els fitxers de configuració es troben a:

/etc/dhcp3

El fitxer principal de configuració és /etc/dhcp3/dhcpd.conf:

# *******************   Totes les aules     **************
# subnet 192.168.1.0 netmask 255.255.255.0
include "/etc/dhcp3/dhcpd-AulesInformaticaiescopernic.conf";


# *******************   Ordinadors Departament **************
#subnet 192.168.12.0 netmask 255.255.255.0
include "/etc/dhcp3/dhcpd-departament.conf";

Com podeu veure, hi ha un fitxer específic de configuració per al departament i un altre per a les aules del departament d'informàtica. El fitxer /etc/dhcp3/dhcpd-AulesInformaticaiescopernic.conf es genera amb la comanda:

$/etc/dhcp3/generadorFitxersDHCP/generador.sh > /etc/dhcp3/dhcpd-AulesInformaticaiescopernic.conf

Les adreces IP s'assignen de la següent forma (on x és el número d'aula):

  • 192.168.x.1: Gateway
  • 192.168.x.2: Ordinador del profe
  • 192.168.x.3: pc01 (1r PC alumnes)
  • 192.168.x.18: pc16 (Últim PC alumnes)
  • 192.168.x.20: Switch Cisco (surt al DHCP tot i que estan configurats estàticament)
  • 192.168.x.21: Punt d'accés si n'hi ha(surt al DHCP tot i que estan configurats estàticament)

A altres màquines com portàtils, se'ls hi assigna adreces en el rang:

range 192.168.x.25-50;

Les dades generals es troben al principi de l'script generador, definides per variables:

#!/bin/bash

PREFIX_FITXER_MACS="macs_aula";
EXTENSIO_FITXER_MACS="txt";
PREFIX_FITXERS_CONF="dhcpd-aula";
SUFIX_FITXERS_CONF=".conf"; 

#Exemple Fitxer CONF: dhcpd-aula6.conf
#Exemple Fitxer MACS: macs_aula 

MASK="255.255.255.0";
BROADCAST_SUFIX=".255"
SUBNET_PREFIX="192.168.";
SUBNET_SUFIX=".0";
MIN_IP=".25";
MAX_IP=".50";
ROUTER_IP=".1"; 

DNS1="192.168.0.7";
DNS2="195.235.113.3";
DNS3="195.235.96.90"; 
 

PREFIX_PC="pc";
PREFIX_PROFE="profe";
PREFIX_PUNT_ACCES="pa";
PREFIX_SWITCH_CISCO="cisco"; 

SUFIX_IP_PROFE=".2"; 

TERMINACIO_IP_PROFE="2"
TERMINACIO_IP_SWITCH_CISCO="20";
TERMINACIO_IP_PUNT_ACCES="21"; 
NOTA: Es podria assignar uns temps de prèstecs més grans a les màquines fixes i uns de més petits als portàtils i altres màquines?

Aquest fitxers es poden generar automàticament amb un script que trobareu a la carpeta:

/etc/dhcp3/generadorFitxersDHCP

i que s'anomena:

generador.sh

Per executar-lo:

 $/etc/dhcp3/generadorFitxersDHCP/generador.sh > /etc/dhcp3/dhcpd-AulesInformaticaiescopernic.conf

Aquest generador "s'alimenta" de les dades dels fitxers:

/etc/dhcp3/generadorFitxersDHCP/macs_aula1.txt
/etc/dhcp3/generadorFitxersDHCP/macs_aula2.txt
/etc/dhcp3/generadorFitxersDHCP/macs_aula3.txt
/etc/dhcp3/generadorFitxersDHCP/macs_aula4.txt
/etc/dhcp3/generadorFitxersDHCP/macs_aula5.txt
/etc/dhcp3/generadorFitxersDHCP/macs_aula6.txt
/etc/dhcp3/generadorFitxersDHCP/macs_aula7.txt
/etc/dhcp3/generadorFitxersDHCP/macs_aula8.txt

Que contenen les MACs de les màquines del centre, incloent la IP de profe, del punt d'accés (si hi ha, s'esta pensat en treure'ls? - --Sergi 19:58, 21 gen 2009 (CET) -) i del switch Cisco.

A mode d'exemple el contingut del fitxer de l'aula1:

# **** Macs de l'Aula 1 ****
# No toqueu els comentaris d'aquest fitxer. Només modifiqueu el valors de
# les MAC allà on correspongui.
# Primer va la MAC del professor. Després del punt d'accés i el commutador 
# Cisco. Despreś ve una llista ordenada de les MAC de la classe
# Cada aula disposa d'un mapa que està a l'armari de comunicacions de l'aula
# També podeu consultar els mapes a la wiki del departament:
# https://www.iescopernic.com/departament/index.php/Configuraci%C3%B3_de_la_xarxa._Aules
#
#Punt accés
pa 00:80:5A:4B:89:C8
#Switch cisco
cisco 00:15:FA:9B:A7:C0
#Profe
profe 00:30:05:eb:38:55
#Alumnes
pc1 00:0f:fe:de:90:e7 
pc2 00:0f:fe:de:83:89  
pc3 00:0f:fe:de:81:7d  
pc4 00:0f:fe:de:85:57  
pc5 00:0f:fe:de:81:87
pc6 00:0f:fe:de:80:e5
pc7 00:0f:fe:de:81:f7
pc8 00:0f:fe:de:84:17  
pc9 00:0f:fe:de:82:9b
pc10 00:0f:fe:de:84:f9  
pc11 00:0f:fe:de:84:51
pc12 00:0f:fe:de:7c:9b
pc13 00:0f:fe:de:8f:89  
pc14 00:0f:fe:de:90:4b 
pc15 00:0f:fe:de:84:67
pc16 00:0f:fe:de:7b:65
NOTA:Per al correcte funcionament del generador cal respectar l'ordre de les màquines i no esborrar els comentaris.
 

Al curs 2008/09 tenim les següents MAC:

https://www.iescopernic.com/mediawiki/index.php/2008-2009/Aula1
https://www.iescopernic.com/mediawiki/index.php/2008-2009/Aula2
https://www.iescopernic.com/mediawiki/index.php/2008-2009/Aula3
https://www.iescopernic.com/mediawiki/index.php/2008-2009/Aula4
https://www.iescopernic.com/mediawiki/index.php/2008-2009/Aula5
https://www.iescopernic.com/mediawiki/index.php/2008-2009/Aula6
https://www.iescopernic.com/mediawiki/index.php/2008-2009/Aula7
https://www.iescopernic.com/mediawiki/index.php/2008-2009/Aula8

Podeu obtenir més informació sobre DHCP a:

http://acacha.dyndns.org/mediawiki/index.php/DHCP

Script per generar automàticament el fitxer dhcpd.conf

#!/bin/bash

PREFIX_FITXER_MACS="macs_aula";
EXTENSIO_FITXER_MACS="txt";
PREFIX_FITXERS_CONF="dhcpd-aula";
SUFIX_FITXERS_CONF=".conf";

#Exemple Fitxer CONF: dhcpd-aula6.conf
#Exemple Fitxer MACS: macs_aula

MASK="255.255.255.0";
BROADCAST_SUFIX=".255"
SUBNET_PREFIX="192.168.";
SUBNET_SUFIX=".0";
MIN_IP=".25";
MAX_IP=".50";
ROUTER_IP=".1";

DNS1="192.168.0.7";
DNS2="195.235.113.3";
DNS3="195.235.96.90";
 

PREFIX_PC="pc";
PREFIX_PROFE="profe";
PREFIX_PUNT_ACCES="pa";
PREFIX_SWITCH_CISCO="cisco";

SUFIX_IP_PROFE=".2";

TERMINACIO_IP_PROFE="2"
TERMINACIO_IP_SWITCH_CISCO="20";
TERMINACIO_IP_PUNT_ACCES="21";

i=1;  

ls ${PREFIX_FITXER_MACS}*.$EXTENSIO_FITXER_MACS | while read FILENAME;
 do
  #ITERACIo FITXER A FITXER
  
  echo "";
  echo "";
  echo "#*************** CONFIGURACIÓ DHCP AULA $i *****************************"
  #Configuració general
  echo "subnet ${SUBNET_PREFIX}${i}${SUBNET_SUFIX} netmask $MASK {";
  echo "     range ${SUBNET_PREFIX}${i}$MIN_IP ${SUBNET_PREFIX}${i}$MAX_IP;";
  echo "     option subnet-mask ${MASK};";
  echo "     option broadcast-address ${SUBNET_PREFIX}${i}${BROADCAST_SUFIX};";
  echo "     option routers ${SUBNET_PREFIX}${i}$ROUTER_IP;";
  echo "     option domain-name-servers $DNS1, $DNS2, $DNS3;";
  echo "}";
  echo "";
                                  	
  #MAC PROFESSOR
  echo "host pcprofe_aula$i {";
  echo "         hardware ethernet `cat $FILENAME | grep -v '^#' | grep $PREFIX_PROFE | awk '{print $2}'`;";
  echo "         fixed-address 192.168.${i}.$TERMINACIO_IP_PROFE;";
  echo "}";
  echo "";
                         

 #MAC SWITCH CISCO
 echo "host switch_cisco_aula$i  {";
 echo "         hardware ethernet `cat $FILENAME | grep -v '^#' | grep $PREFIX_SWITCH_CISCO | awk '{print $2}'`;";
 echo "         fixed-address 192.168.${i}.$TERMINACIO_IP_SWITCH_CISCO;";
 echo "}";
 echo ""; 
 
 #MAC PUNT ACCES
 echo "host punt_acces_aula$i  {";
 echo "         hardware ethernet `cat $FILENAME | grep -v '^#' | grep $PREFIX_PUNT_ACCES | awk '{print $2}'`;";
 echo "         fixed-address 192.168.${i}.$TERMINACIO_IP_PUNT_ACCES;";
 echo "}";  
 echo "";
 
 j=1;     
 k=3;    

 cat $FILENAME | grep -v '^#' | grep pc | awk '{print $2}' | while read MAC;
  do
   echo "host pc${j}_aula$i  {"; 
   echo "         hardware ethernet $MAC;";
   echo "         fixed-address 192.168.${i}.${k};";
   echo "}";
   echo "";
    j=$(($j + 1))
   k=$(($k + 1))
  done
  
 i=$(($i + 1))

done

Client DHCP alternatiu

La següent comanda instal·la Pump i desinstal·la dhclient:

$ sudo apt-get install pump dhclient-

No disposo més informació sobre pump.


Cisco i DHCP Relay

If your routers are cisco like just add "ip helper-address dhcp.server.ip.address" configuration in each vlan interface like: Switch>enable Switch#configure terminal Switch(config)#interface vlan 1968 Switch(config-if)#ip helper-address 192.168.100.200 Switch(config-if)#end

then the router will relay dhcp packets between dhcp server and dhcp client

if you want to use a dhcp server with many virtual interface to do this you need 1. a nic with 802.1q vlan support on your dhcp server 2. define virtual interfaces on your dhcp server machine 3. define a trunk port which directly connect to dhcp server's nic and brings all vlan tag to the nic of dhcp server cisco like config is : Switch>enable Switch#configure terminal Switch(config)#vtp mode transparent Switch(config)#interface gigabitEthernet 0/12 Switch(config-if)#switchport trunk encapsulation dot1q Switch(config-if)#switchport mode trunk Switch(config-if)#end

4. tell dhcp server to listen dhcp packets on virtual interfaces

however, maybe nic won't support more than 64 vlans so, i think you need both router's dhcp relay and dhcp server's virtual interface configuration (mix L2 and L3 traffic on the router or core switch)

finally, you can download cisco's "Multilayer Switch Software Configuration Guide" of their products on cisco's web site, their manual is really easy to understand.

Servidor DHCP en xarxa LAN compartida

Vegeu l'opció:

shared-network

Consulteu:

DHCP#Xarxes_compartides_.28shared_networks.29._M.C3.BAltiples_interf.C3.ADcies_virtuals_en_una_mateixa_targeta_de_xarxa_f.C3.ADsica

Recursos:

dhcp-server-identifier

Alguns commutadors com Cisco poden enrutar paquets DHCPamb el que s'anomena ip-helper

Resolució d'errors. TroubleShooting

DHCP i Spanning Tree Protocol (STP). Switches Cisco

"fast-spanning-tree" or "spanning-tree-fast" for the user-ports.

Documentació de Cisco:

Consulteu també l'apartat Spanning tree protocol i problemes amb PXE/Etherboot de l'article sobre drbl.

Recursos

No IP at boot/startup o inici amb DHCP

Podeu tornar a demanar una IP amb la comanda:

$ sudo dhclient eth0

Sembla que hi ha un error relacionat amb el rellotge del sistema:

$ cat /etc/default/rcS
#
# /etc/default/rcS
#
# Default settings for the scripts in /etc/rcS.d/
#
# For information about these variables see the rcS(5) manual page.
#
# This file belongs to the "initscripts" package.

TMPTIME=60
SULOGIN=no
DELAYLOGIN=no
UTC=no
VERBOSE=no
FSCKFIX=no

Cal canviar el rellotge UTC a si:

UTC=yes

Afegir a /etc/network/interfaces:

 pre-up sleep 10

Problemes amb els temps de préstec i els els renews d'aquest préstecs. Al client es poden consultar els préstecs al fitxer :

$ /var/lib/dhcp3/dhclient.eth1.leases
Ok, I think I found the problem:

The networking initscript in /etc/rcS.d is started *before* hwclock.sh (which sets the correct local time). dhcp3 picks up the wrong time and, depending  
on the UTC-Offset, thinks its lease is still valid and never sends a DHCP-Request.


I fixed it by starting hwclock.sh earlier (for example renaming it to S09hwclock.sh) and deleting /var/lib/dhcp3/dhclient.eth0.leases .

Fer que Windows utilitzi UTC

Create a file named WindowsTimeFixUTC.reg with the following contents and then double click on it to merge the contents with the registry:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation]
    "RealTimeIsUniversal"=dword:00000001


Recursos:

Arrancada de xarxa i DHCP

Consulteu:

Pxelinux#Configuraci.C3.B3_de_DHCP

i TFTP.

Script de creació automàtica de fitxers

Scripts 1

#/bin/bash

FILENAME=hosts_ordenat.csv

cat $FILENAME | while read line;
 do
     NAME=`echo $line | awk '{print $1}'`
     IP=`echo $line | awk '{print $2}'`
     MAC=`echo $line | awk '{print $3}'`
     echo "host ${NAME}  {";
     echo "         hardware ethernet ${MAC};";
     echo "         fixed-address ${IP};";
     echo "}";
     echo "";
 done

o amb heredoc:

#/bin/bash

FILENAME=hosts_ordenat.csv

cat $FILENAME | while read line;
 do
     NAME=`echo $line | awk '{print $1}'`
     IP=`echo $line | awk '{print $2}'`
     MAC=`echo $line | awk '{print $3}'`
     cat <<EOF
     host ${NAME}  {
              hardware ethernet ${MAC};
              fixed-address ${IP};
     }
EOF
 done

NOTA: El EOF de tancament ha d'estar a l'inici de la fila

On el fitxers és:

$ cat hosts_ordenat.csv 
A20-2PC01 192.168.7.101 00:10:5c:f2:be:38
A20-2PC02 192.168.7.102 00:1a:92:9d:db:ab
A20-2PC03 192.168.7.103 00:1b:fc:65:cc:1b
A20-2PC04 192.168.7.104 00:10:5C:F2:BE:39
A20-2PC05 192.168.7.105 00:10:5C:F2:ED:D2
A20-2PC06 192.168.7.106 00:1b:fc:08:75:42
...

Script 2

#!/bin/bash

PREFIX_FITXER_MACS="macs_aula";
EXTENSIO_FITXER_MACS="txt";
PREFIX_FITXERS_CONF="dhcpd-aula";
SUFIX_FITXERS_CONF=".conf";

#Exemple Fitxer CONF: dhcpd-aula6.conf
#Exemple Fitxer MACS: macs_aula

MASK="255.255.255.0";
BROADCAST_SUFIX=".255"
SUBNET_PREFIX="192.168.";
SUBNET_SUFIX=".0";
MIN_IP=".25";
MAX_IP=".50";
ROUTER_IP=".1";

DNS1="192.168.0.7";
DNS2="195.235.113.3";
DNS3="195.235.96.90";
 

PREFIX_PC="pc";
PREFIX_PROFE="profe";
PREFIX_PUNT_ACCES="pa";
PREFIX_SWITCH_CISCO="cisco";

SUFIX_IP_PROFE=".2";

TERMINACIO_IP_PROFE="2"
TERMINACIO_IP_SWITCH_CISCO="20";
TERMINACIO_IP_PUNT_ACCES="21";

i=1;  

ls ${PREFIX_FITXER_MACS}*.$EXTENSIO_FITXER_MACS | while read FILENAME;
 do
  #ITERACIo FITXER A FITXER
  
  echo "";
  echo "";
  echo "#*************** CONFIGURACIÓ DHCP AULA $i *****************************"
  #Configuració general
  echo "subnet ${SUBNET_PREFIX}${i}${SUBNET_SUFIX} netmask $MASK {";
  echo "     range ${SUBNET_PREFIX}${i}$MIN_IP ${SUBNET_PREFIX}${i}$MAX_IP;";
  echo "     option subnet-mask ${MASK};";
  echo "     option broadcast-address ${SUBNET_PREFIX}${i}${BROADCAST_SUFIX};";
  echo "     option routers ${SUBNET_PREFIX}${i}$ROUTER_IP;";
  echo "     option domain-name-servers $DNS1, $DNS2, $DNS3;";
  echo "}";
  echo "";
                                  	
  #MAC PROFESSOR
  echo "host pcprofe_aula$i {";
  echo "         hardware ethernet `cat $FILENAME | grep -v '^#' | grep $PREFIX_PROFE | awk '{print $2}'`;";
  echo "         fixed-address 192.168.${i}.$TERMINACIO_IP_PROFE;";
  echo "}";
  echo "";
                         

 #MAC SWITCH CISCO
 echo "host switch_cisco_aula$i  {";
 echo "         hardware ethernet `cat $FILENAME | grep -v '^#' | grep $PREFIX_SWITCH_CISCO | awk '{print $2}'`;";
 echo "         fixed-address 192.168.${i}.$TERMINACIO_IP_SWITCH_CISCO;";
 echo "}";
 echo ""; 
 
 #MAC PUNT ACCES
 echo "host punt_acces_aula$i  {";
 echo "         hardware ethernet `cat $FILENAME | grep -v '^#' | grep $PREFIX_PUNT_ACCES | awk '{print $2}'`;";
 echo "         fixed-address 192.168.${i}.$TERMINACIO_IP_PUNT_ACCES;";
 echo "}";  
 echo "";
 
 j=1;     
 k=3;    

 cat $FILENAME | grep -v '^#' | grep pc | awk '{print $2}' | while read MAC;
  do
   echo "host pc${j}_aula$i  {"; 
   echo "         hardware ethernet $MAC;";
   echo "         fixed-address 192.168.${i}.${k};";
   echo "}";
   echo "";
    j=$(($j + 1))
   k=$(($k + 1))
  done
  
 i=$(($i + 1))

done

Exemple de fitxers de MACS:

$ cat macs_aula1.txt 
# **** Macs de l'Aula 1 ****
# No toqueu els comentaris d'aquest fitxer. Només modifiqueu el valors de
# les MAC allà on correspongui.
# Primer va la MAC del professor. Després del punt d'accés i el commutador 
# Cisco. Despreś ve una llista ordenada de les MAC de la classe
# Cada aula disposa d'un mapa que està a l'armari de comunicacions de l'aula
# També podeu consultar els mapes a la wiki del departament:
# https://www.iescopernic.com/departament/index.php/Configuraci%C3%B3_de_la_xarxa._Aules
#
#Punt accés
pa 00:80:5A:4B:89:C8
#Switch cisco
cisco 00:15:FA:9B:A7:C0
#Profe
profe 00:30:05:eb:38:55
#Alumnes
pc1 00:13:8f:bc:70:9d
pc2 00:13:8f:db:85:79
pc3 00:13:8f:bc:6f:94
pc4 00:13:8f:bc:70:79
pc5 00:13:8f:bc:70:85
pc6 00:13:8f:bc:6e:e0
pc7 00:13:8f:bb:77:28
pc8 00:13:ff:ff:70:a9
pc9 00:13:8f:bc:70:84
pc10 00:13:8f:bc:70:82
pc11 00:13:8f:bc:70:90
pc12 00:13:8f:bc:70:80
pc13 00:19:66:b6:c7:94
pc14 00:13:8f:bc:70:a5
pc15 00:13:8f:bc:70:7c
pc16 00:13:8f:bc:6b:29

FOG

He creat un Servei per a FOG que crea les assignacions fixes automàticament. Consulteu FOg

Exemples. Com fer?

Múltiples servidors DHCP en un mateix segment de xarxa

Com impedir que s'assigni IP a les màquines que no estigui definides

Podeu utilitzar boot-unknown-clients, segons el manual:

$ man dhcpd.conf
   The boot-unknown-clients statement
 
  boot-unknown-clients flag;

  If  the  boot-unknown-clients  statement  is  present  and has a value of false or off, then 
clients for which there is no host declaration will not be allowed to
obtain IP addresses.   If this statement is not present or has a value of true or on, then 
clients  without  host  declarations  will  be  allowed  to  obtain  IP
addresses, as long as those addresses are not restricted by allow and deny statements within 
their pool declarations.

Allow i deny a les declaracions pool

TODO

Assignar una IP només si està lliure. ping

Es pot utilitzar els paràmetres ping-check i ping-timeout. Cal tenir en compte que introdueix un retard en la resposta que pot ser important segons el tipus de client.

The ping-check statement

         ping-check flag;

         When the DHCP server is considering dynamically allocating an IP address to a client, it first sends an ICMP Echo request (a ping) to the address being  assigned.
         It  waits for a second, and if no ICMP Echo response has been heard, it assigns the address.   If a response is heard, the lease is abandoned, and the server does
         not respond to the client.

         This ping check introduces a default one-second delay in responding to DHCPDISCOVER messages, which can be a problem for some clients.   The default delay of  one
         second  may  be  configured  using the ping-timeout parameter.  The ping-check configuration parameter can be used to control checking - if its value is false, no
         ping check is done.

      The ping-timeout statement

         ping-timeout seconds;

         If the DHCP server determined it should send an ICMP echo request (a ping) because the ping-check statement is true, ping-timeout allows you to configure how many
         seconds  the  DHCP server should wait for an ICMP Echo response to be heard, if no ICMP Echo response has been received before the timeout expires, it assigns the
         address.  If a response is heard, the lease is abandoned, and the server does not respond to the client.  If no value is set, ping-timeout defaults to 1 second.

IP Aliasing i DHCP

TODO

 The server-identifier statement

         server-identifier hostname;

         The server-identifier statement can be used to define the value that is sent in the DHCP Server Identifier option for a given scope.   The value specified must be
         an IP address for the DHCP server, and must be reachable by all clients served by a particular scope.

         The use of the server-identifier statement is not recommended - the only reason to use it is to force a value other than the default value to be sent on occasions
         where  the  default  value  would  be  incorrect.    The default value is the first IP address associated with the physical network interface on which the request
         arrived.

         The usual case where the server-identifier statement needs to be sent is when a physical interface has more than one IP address, and the one being sent by default
         isn’t  appropriate  for  some  or all clients served by that interface.  Another common case is when an alias is defined for the purpose of having a consistent IP
         address for the DHCP server, and it is desired that the clients use this IP address when contacting the server.

         Supplying a value for the dhcp-server-identifier option is equivalent to using the server-identifier statement.

Noms de màquina

 use-host-decl-names flag;

         If the use-host-decl-names parameter is true in a given scope, then for every host declaration within that scope, the name provided for the host declaration  will
         be supplied to the client as its hostname.   So, for example,

             group {
               use-host-decl-names on;

               host joe {
                 hardware ethernet 08:00:2b:4c:29:32;
                 fixed-address joe.fugue.com;
               }
             }

         is equivalent to

               host joe {
                 hardware ethernet 08:00:2b:4c:29:32;
                 fixed-address joe.fugue.com;
                 option host-name "joe";
               }

DHCP i dinàmic DNS

Rogue DHCP servers

DHCP secundari

subnet 192.168.200.0 netmask 255.255.255.0 {
       option subnet-mask 255.255.255.0;
       option broadcast-address 192.168.200.255;
       option routers 192.168.200.1;
       option domain-name-servers 192.168.200.1;
       pool {
       failover peer "dhcp-failover";
       max-lease-time 1800; # 30 minutes
       range 192.168.200.100 192.168.200.254;
              }
      }

The following configuration would be placed on the secondary server:

       authoritative;
      ddns-update-style none;
      failover peer "dhcp-failover" {
      secondary; # declare this to be the secondary server
      address 192.168.200.3;
      port 520;
      peer address 192.168.200.2;
      peer port 520;
      max-response-delay 30;
      max-unacked-updates 10;
      load balance max seconds 3;
         }
      subnet 192.168.200.0 netmask 255.255.255.0 {
      option subnet-mask 255.255.255.0;
      option broadcast-address 192.168.200.255;
      option routers 192.168.200.1;
                                                              Page 13 of 16
option domain-name-servers 192.168.200.1;
pool {
failover peer "dhcp-failover";
max-lease-time 1800; # 30 minutes
range 192.168.200.100 192.168.200.254;
         }

Pràctiques i exercicis

Vegeu també

Enllaços externs

Eines de l'usuari