route 相关设置

时间:2023-03-10 07:13:29
route 相关设置

Debian系统

查看路由表:

root@debian:~# ip route
default via 192.168.6.1 dev enp4s0
10.0.0.0/24 dev br0 proto kernel scope link src 10.0.0.1 linkdown
192.168.6.0/24 dev enp4s0 proto kernel scope link src 192.168.6.20

  

Debian / Ubuntu Linux static routing for two interfaces:

Here is a config file named /etc/network/interfaces:

auto lo
iface lo inet loopback auto eth0
iface eth0 inet static
address 10.9.38.76
netmask 255.255.255.240
network 10.9.38.64
broadcast 10.9.38.79
### static routing for eth0 that connects to the VLAN ###
post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.9.38.65
pre-down route del -net 10.0.0.0 netmask 255.0.0.0 gw 10.9.38.65 auto eth1
iface eth1 inet static
address 204.186.149.140
netmask 255.255.255.240
network 204.186.149.128
broadcast 204.186.149.143
## default gateway for eth1 and the server ##
gateway 204.186.149.129
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 10.0.80.11 10.0.80.12
dns-search nixcraft.in

  

ip command to set a default router to 192.168.1.254

# ip route add default via 192.168.1.254

  

route command to set a default router to 192.168.1.254

 route add default gw 192.168.1.254

  

Save routing information to a configuration file /etc/network/interfaces

Open /etc/network/interfaces file

# vi /etc/network/interfaces

Find eth0 or desired network interface and add following option

gateway 192.168.1.254 

Save and close the file. Restart networking:

# /etc/init.d/networking restart

 

A note about ip command and persistence static routing on a Debian/Ubuntu

Edit your /etc/network/interfaces file for say eth0:

# vi /etc/network/interfaces

Update it as follows:

auto eth0
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.1.254
## static ip config START ##
up /sbin/ip route add 172.10.1.0/24 via 10.8.0.1 dev eth0
down /sbin/ip route delete 172.10.1.0/24 via 10.8.0.1 dev eth0
## static ip config END ##

  

Restart networking service when using a Debian or Ubuntu Linux

# systemctl restart networking