Jump to content
aelius

Configurare adrese ip multiple pe linux.

Recommended Posts

Stiu ca toti sunteti tentati sa faceti alias-uri, insa in cazul in care aveti sa spunem 200 de adrese ip de configurat pe un server, nu cred ca veti sta sa scrieti cele 200 de linii intr-un script de init. Mai mult, la comanda 'ifconfig -a' or sa fie afisate ca alias-uri (adica, mai pe romaneste, o sa vomite prima interfata de retea + 200 de alias-uri ale acesteia in CLI).

Exemplu standard:


root@rst:~# ifconfig eth0:1 10.0.0.2 netmask 255.255.255.0
root@rst:~# ifconfig eth0:2 10.0.0.3 netmask 255.255.255.0
root@rst:~# ifconfig eth0:3 10.0.0.4 netmask 255.255.255.0
root@rst:~# ifconfig eth0:4 10.0.0.5 netmask 255.255.255.0
root@rst:~# ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:02:c0:f1
inet addr:192.168.0.77 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe02:c0f1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2014 errors:0 dropped:0 overruns:0 frame:0
TX packets:1105 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:181623 (177.3 KiB) TX bytes:157231 (153.5 KiB)

eth0:1 Link encap:Ethernet HWaddr 08:00:27:02:c0:f1
inet addr:10.0.0.2 Bcast:10.0.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

eth0:2 Link encap:Ethernet HWaddr 08:00:27:02:c0:f1
inet addr:10.0.0.3 Bcast:10.0.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

eth0:3 Link encap:Ethernet HWaddr 08:00:27:02:c0:f1
inet addr:10.0.0.4 Bcast:10.0.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

eth0:4 Link encap:Ethernet HWaddr 08:00:27:02:c0:f1
inet addr:10.0.0.5 Bcast:10.0.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 TX bytes:0 (0.0

root@rst:~#

Idee:


root@rst:~# for i in {2..30} ; do ip addr add 10.0.0.$i broadcast 10.0.0.255 dev eth0 ; done
root@rst:~# ifconfig -a
eth0 Link encap:Ethernet HWaddr 08:00:27:02:c0:f1
inet addr:192.168.0.77 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe02:c0f1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2522 errors:0 dropped:0 overruns:0 frame:0
TX packets:1405 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:225333 (220.0 KiB) TX bytes:204095 (199.3 KiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 TX bytes:0 (0.0

root@rst:~# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:02:c0:f1 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.77/24 brd 192.168.0.255 scope global eth0
inet 10.0.0.2/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.3/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.4/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.5/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.6/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.7/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.8/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.9/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.10/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.11/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.12/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.13/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.14/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.15/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.16/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.17/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.18/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.19/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.20/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.21/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.22/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.23/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.24/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.25/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.26/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.27/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.28/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.29/32 brd 10.0.0.255 scope global eth0
inet 10.0.0.30/32 brd 10.0.0.255 scope global eth0
inet6 fe80::a00:27ff:fe02:c0f1/64 scope link
valid_lft forever preferred_lft forever
root@rst:~#

Din cate vedeti, la ifconfig imi este afisata o singura adresa ip. No alias, no shits.

Cum ar arata fisierul de configuratie al retelei in cazul in care utilizati alias-uri pe eth0 si aveti o gramada de adrese ip ?


auto eth0
allow-hotplug eth0
iface eth0 inet static
address 192.168.0.42
netmask 255.255.255.0
gateway 192.168.1.1

auto eth0:0
allow-hotplug eth0:0
iface eth0:0 inet static
address 192.168.0.43
netmask 255.255.255.0

auto eth0:1
allow-hotplug eth0:1
iface eth0:1 inet static
address 192.168.0.44
netmask 255.255.255.0
.......................
.......................

Pentru a face permanente modificarile (sa se configureze adresele ip la restart):


root@rst:~# cat >> /etc/ipaddr << EOF
> #!/bin/bash
> for i in {2..30} ; do ip addr add 10.0.0.\$i broadcast 10.0.0.255 dev eth0 ; done
> EOF
root@rst:~# chmod 711 /etc/ipaddr

In fisierul '/etc/network/interfaces' adaugati:


post-up /etc/ipaddr

Exemplu:


# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet static
address 192.168.0.77
netmask 255.255.255.0
gateway 192.168.0.1
post-up /etc/ipaddr

Note:

- Testele au fost facute pe Debian. Functioneaza pe Debian si orice distributie derivata din aceasta.

- Comanda 'ip addr' functioneaza pe orice distributie linux. Face parte din pachetul iproute2

- Tema, daca aveti fetishuri cu cititul: http://www.policyrouting.org/iproute2.doc.html

Edited by aelius
Link to comment
Share on other sites

@Nicotin: pe centos ai range (iti autoconfigureaza el alias-uri)

Pentru ip range pe Centos:


cd /etc/sysconfig/network-scripts
cat >> ifcfg-eth0-range0 << EOF
TYPE="Ethernet"
IPADDR_START=10.0.0.2
IPADDR_END=10.0.0.200
IPV6INIT=no
EOF
chmod 755 ifcfg-eth0-range0

Note:

- Asta o sa-ti faca alias-uri cu eth0:0, eth0:1 ..... pentru toate adresele ip de la 10.0.0.2 la 10.0.0.200

- Nu adaugati in rc.local; Se initializeaza ultimul (dupa ce au pornit toate serviciile), caz in care serviciile ce se listeaza pe adresele ip configurate in rc.local, nu o sa porneasca.

Pentru a implementa scriptul 'ipaddr' prezentat mai sus, poti arunca o privire aici:

How to run a startup script automatically after a network interface is up on CentOS - Linux FAQ

Edited by aelius
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...