Qual è il modo migliore per aggiungere un percorso permanente?


11

Devo aggiungere un percorso che non verrà eliminato dopo il riavvio. Ho letto questi due modi per farlo:

Aggiungi up route add -net 172.X.X.0/24 gw 172.X.X.X dev ethXal file /etc/network/interfaces

o

Crea il file /etc/network/if-up.d/route con:

#!/bin/sh
route add -net 172.X.X.0/24 gw 172.X.X.X dev ethX

e renderlo eseguibile:

chmod +x /etc/network/if-up.d/route

Quindi sono confuso. Qual è il modo migliore per farlo?


1
sembra che tu sia concentrato su un sistema Linux? (abbiamo AIX, HPUX, Irix, Solaris, ....) sul sito.
— Jeff Schaller

Ti dispiace, non ho specificato che era su un sistema Debian per Linux. Grazie per avermelo ricordato.
— Pozinux l'

Risposte:


14

Hai menzionato /etc/network/interfaces, quindi è un sistema Debian ...

Creare una tabella di routing denominata. Ad esempio, ho usato il nome "mgmt" di seguito.

echo '200 mgmt' >> /etc/iproute2/rt_tables

Sopra, il kernel supporta molte tabelle di routing e fa riferimento a queste con numeri interi univoci numerati 0-255. Un nome, mgmt, è anche definito per la tabella.

Di seguito, uno sguardo a un valore predefinito /etc/iproute2/rt_tablessegue, che mostra che alcuni numeri sono riservati. La scelta in questa risposta di 200 è arbitraria; si potrebbe usare qualsiasi numero che non sia già in uso, 1-252.

#
# reserved values
#
255     local
254     main
253     default
0       unspec
#
# local
#

Di seguito, un file di interfaccia Debian 7/8 definisce eth0e eth1. eth1è la rete 172. eth0potrebbe usare anche DHCP. 172.16.100.10è l'indirizzo IP a cui assegnare eth1. 172.16.100.1è l'indirizzo IP del router.

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The production network interface
auto eth0
allow-hotplug eth0
# iface eth0 inet dhcp 
# Remove the stanzas below if using DHCP.
iface eth0 inet static
  address 10.10.10.140
  netmask 255.255.255.0
  gateway 10.10.10.1

# The management network interface
auto eth1
allow-hotplug eth1
iface eth1 inet static
  address 172.16.100.10
  netmask 255.255.255.0
  post-up ip route add 172.16.100.0/24 dev eth1 src 172.16.100.10 table mgmt
  post-up ip route add default via 172.16.100.1 dev eth1 table mgmt
  post-up ip rule add from 172.16.100.10/32 table mgmt
  post-up ip rule add to 172.16.100.10/32 table mgmt

Riavvia o riavvia la rete.

Aggiornamento - Esposizione su EL

Ho notato in un commento che eri "chiedendo anche a RHEL". In Enterprise Linux ("EL" - RHEL / CentOS / et al), creare una tabella di routing denominata come indicato sopra.

Il /etc/sysconfig/networkfile EL :

NETWORKING=yes
HOSTNAME=host.sld.tld
GATEWAY=10.10.10.1

/etc/sysconfig/network-scripts/ifcfg-eth0Segue il file EL , utilizzando una configurazione statica (senza NetworkManager e non specificando "HWADDR" e "UUID" nell'esempio seguente).

DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTOCOL=none
IPADDR=10.10.10.140
NETMASK=255.255.255.0
NETWORK=10.10.10.0
BROADCAST=10.10.10.255

/etc/sysconfig/network-scripts/ifcfg-eth1Segue il file EL (senza NetworkManager e non specificando "HWADDR" e "UUID" nell'esempio seguente).

DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTOCOL=none
IPADDR=172.16.100.10
NETMASK=255.255.255.0
NETWORK=172.16.100.0
BROADCAST=172.16.100.255

Il /etc/sysconfig/network-scripts/route-eth1file EL :

172.16.100.0/24 dev eth1 table mgmt
default via 172.16.100.1 dev eth1 table mgmt

Il /etc/sysconfig/network-scripts/rule-eth1file EL :

from 172.16.100.0/24 lookup mgmt

0

Sulla distribuzione basata su Debian è possibile aggiungere una route statica in modo permanente come segue:

 echo "up route add -net 172.X.X.X/24 gw 172.X.X.X dev ethX" | sudo tee --append /etc/network/interfaces

Sulla distribuzione basata su RHEL:

echo "172.X.X.X/24 via 172.X.X.X" | sudo tee --append /etc/sysconfig/network-scripts/route-ethX

3
Non ha sudosenso in entrambi i tuoi due comandi. O sei già root, quindi >>funziona o no, nel qual caso >>viene applicato come utente originale e solo echoviene eseguito come root. Inoltre, ciò non riesce in modo negativo se ci sono più interfacce definite in /etc/network/interfaces.
— roaima,

1
echo "sth" | sudo tee nomefile
— JSBach

questo non funziona per le istanze debian in gcp
— Parv Sharma,

In effetti, per quanto riguarda la versione di Debian, il comando dato dipende fortemente dall'interfaccia configurata per ultima nel file / etc / network / interfaces
— Gohu
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.