ubuntu interfaces配置文件

https://www.cyberciti.biz/faq/ip-route-add-network-command-for-linux-explained/

ip route add network command examples
The syntax is pretty simple:
ip route add {NETWORK/MASK} via {GATEWAYIP}
ip route add {NETWORK/MASK} dev {DEVICE}
ip route add default {NETWORK/MASK} dev {DEVICE}
ip route add default {NETWORK/MASK} via {GATEWAYIP}

Add a static route on Linux
You must login as root user with the help of su command or sudo command:
$ su -

OR
$ sudo -i

Once become a root user, setup a temporary route using the ip command:

ip route add 172.10.1.0/24 via 10.0.0.100 dev eth0

Verify it:

ip r

Here is another example where I am setting up route for my VPN gateway:

ip link set dev tun0 up mtu 1500

ip addr add dev tun0 10.8.0.2/24 broadcast 10.8.0.255

ip route add 139.59.2.125/32 via 192.168.2.254

ip route add 0.0.0.0/1 via 10.8.0.1

ip route add 128.0.0.0/1 via 10.8.0.1

Again view route with the ip command:

ip r

Linux ip route add network command examples

Warning: Do not stop networking service over ssh session.

How to add a permanent static route using ip command on Linux
Edit config file such as /etc/sysconfig/network-scripts/route-eth0 on a CentOS/RHEL/Fedora Linux for interface eth0 using a text editor such as nano command or vim command:

vim /etc/sysconfig/network-scripts/route-eth0

Append the following text:
172.10.1.0/24 via 10.0.0.100 dev eth0

Save and exit (close) the file in a vim text editor. Finally, restart your network service on a CentOS/RHEL/Fedora Linux so they take effect:

systemctl restart network.service

Restarting network service on a CentOS/RHEL/Fedora Linux
Restarting network service on a CentOS/RHEL/Fedora Linux

Test new routing with the help of ping command or ip command:

ping 172.10.1.10

ip r

For more info see:
How to configure a static IP address on CentOS 7 / RHEL 7
Red Hat Enterprise Linux Static Routing Configuration For v 4.x/5.x/6.x/7.x
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

Conclusion
This page showed you how to configure static routes using the ip command line options. For more info see ip command man page.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值