第一步、
进入/etc/init.d目录,创建一个名字叫route文件(也可以copy该目录下任意一个文件作为我们的模板,修改一下即可),先编辑如下内容:
#!/bin/sh
# route This is a route ----单纯的一个纯注释
# chkconfig: - 99 10
# description: route
chkconfig: 设置的三个值是必须的。这个跟我们的启动服务顺序有关。这行告诉chkconfig缺省启动的运行级以及启动和停止的优先级。如果某服务缺省不在任何运行级启动,那么使用-
代替运行级。数字越低优先级越高。本例中,开机时由于路由设置与其他服务的依赖关系不大,放在后面启动(设置为99)。相应的,开机最后启动的服务应在关机的时候最先关闭(避免依赖关系导致的服务无法关机)(设置为10)。
description这一行也是必不可少的。虽然这行是注释行,但其他文件会截取这行内容进行判断等处理。
第二步、编辑route文件
#!/bin/sh
# route This is a route
#
# chkconfig: - 99 10
# description: route
. /etc/rc.d/init.d/functions //方便我们调用action函数,实现绿色的OK,红色的false
#开启服务
start(){
route add -net 1.1.1.0/24 gw 192.168.32.167 #添加路由表
action "Route set OK!" true #显示红色OK
}
#关闭服务
stop(){
route del -net 1.1.1.0/24 #删除路由
action "route already delete!" true
}
#stop:关闭路由服务 start:开启路由服务 restart:重启路由服务 status:列出路由表
case $1 in
stop)stop;;
start)
if route -n |grep 1.1.1.0 &> /dev/null ;then
action "route already exit!" false
exit 1;
else
start
fi
;;
restart)stop;start;;
status)route -n;;
*)echo "error";;
esac
第三步、增加服务
1、在增加服务前,我们先查看一下/etc/rc?.d
ls /etc/rc?.d/*route*
啥也没有就对了!因为我们还没有增加服务呢!
2、然后我们添加服务
chkconfig --add route
ls /etc/rc?.d/*route* //再查看一下/etc/rc?.d/有没包含route的文件
/etc/rc0.d/K10route
/etc/rc1.d/K10route
/etc/rc2.d/K99route
/etc/rc3.d/K99route
/etc/rc4.d/K99route
/etc/rc5.d/K99route
/etc/rc6.d/K10route
哈哈有文件了!
3、
chkconfig route on ---在chkconfig工具服务列表中增加此服务,此时服务会被在/etc/rc.d/rcN.d
中赋予K/S入口了;
chkconfig route on
ls /etc/rc?.d/*route* -1
/etc/rc0.d/K10route
/etc/rc1.d/K10route
/etc/rc2.d/S99route
/etc/rc3.d/S99route
/etc/rc4.d/S99route
/etc/rc5.d/S99route
/etc/rc6.d/K10route
对比一下上一步,文件名的K、S发生了变化。离成功就差最后一步了!
最后一步、重启
reboot
重启完成后,查看一下路由表吧 ^_^
[root@localhost init.d]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.32.0 0.0.0.0 255.255.255.0 U 1 0 0 eth1
1.1.1.0 192.168.32.167 255.255.255.0 UG 0 0 0 eth1
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 ethmage
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 ethmage
0.0.0.0 172.18.0.1 0.0.0.0 UG 0 0 0 ethmage
1.1.1.0/24路由已经添加上去啦。
注:您也可以尝试着用service命令开启或者关闭服务
[root@localhost init.d]# service route start
route already exit! [FAILED]
[root@localhost init.d]# service route stop
route already delete! [ OK ]
有没有受到启发^_^试着做做其他的开机自启服务吧。
如有疑问,联系QQ:1930818140 大家可以一起多多交流