大网数据广告活动
服务器运维问答

Add IP Routes and Rules at Startup

· 52CE 我爱测

I’m currently routing some marked packets via eth0 . However, I have to apply the routing rules every time the system reboots. The two commands I always have to re enter are ip rule add fwmark 3 table 3 ip route add default via 1…

问题原文

原帖链接 · Zaden · CC BY-SA 3.0

I’m currently routing some marked packets via eth0. However, I have to apply the routing rules every time the system reboots. The two commands I always have to re enter are

ip rule add fwmark 3 table 3
ip route add default via 192.168.0.1 table 3

The gateway for eth0 being 192.168.0.1 I’ve tried placing those commands in both /etc/rc.local and /etc/network/interfaces, and in both cases I have still had to run them manually. Does anyone have a suggestion as to where/how to run these commands on every startup?

社区高分解答

原帖链接 · user9517 · CC BY-SA 3.0

Put your commands in a shell script e.g. /usr/local/sbin/myrouting and make it executable.

You could use cron and an @reboot target in /etc/crontab or the root crontab e.g. /etc/crontab

@reboot root /usr/local/sbin/myrouting

or root crontab

@reboot /usr/local/sbin/myrouting 

You could also do it using systemd.

Create a systemd unit file /etc/systemd/system/myrouting.service

[Unit]
after=network

[Service]
ExecStart=/usr/local/sbin/myrouting

[Install]
WantedBy=default.target

Then enable it

systemctl enable myrouying.service
Created symlink /etc/systemd/system/default.target.wants/myrouting.service → /etc/systemd/system/myrouting.service.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-12-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。