LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   First init.d script? (https://www.linuxquestions.org/questions/linux-newbie-8/first-init-d-script-4175465593/)

kaempfer0080 06-11-2013 12:51 PM

First init.d script?
 
Hello everyone. At work we have a linux server that is on both the local network and a VPN network. In order for it to function correctly I need to route local traffic through the local network and the rest of the traffic through the VPN router. I did this by putting the following in terminal
Code:

route add default gw 10.226.67.1 eth1
route del default gw 192.168.16.1 eth0

to route the traffic through a specific interface.

And that fixed the problem. However, whenever the server restarts or the network goes down or whatever, the route config switches back to the default which doesn't work. I want to write a script and place it in init.d that just executes the 2 lines written above. I've done a lot of googling but the examples I find are complex with starts, stops, conditionals, etc. I just want to run the two lines. How can I do this?

thedaver 06-11-2013 01:06 PM

do some research into an init script 'rc.local' which is present for Debian/Ubuntu systems... Redhat/Fedora probably use something similar.

rc.local allows you to insert some commands that should be part of the boot sequence.


You've clearly appreciated that the 'route' command you've been issuing are only valid until a restart of networking or a reboot.

kaempfer0080 06-11-2013 01:35 PM

Oh, thanks for reminding me. The server runs Fedora 14 so fedora/red hat structure. I'll try a few more things in the meantime.

theNbomr 06-12-2013 08:37 AM

rc.local should be present in the /etc tree on most Linux distros.

kaempfer0080 06-12-2013 09:59 AM

Ah I found rc.local. Can I just put the commands in there as is without any of the binning and bashing?

thedaver 06-12-2013 10:34 AM

It should work that way. If you have the flexibility to reboot that server, you can insert this into the rc.local and check the outcome after reboot

Code:

echo "hello world" > /var/log/my_rc.local_script_output.log
IF that works, then you should be able to insert real commands into rc.local.

Note, this approach will NOT implement the commands on restart of networking, which likely exists under another set of init.d scripts.

You could write your commands into a script and store that script in /sbin or /usr/bin or whatever. Then in rc.local AND in the networking scripts you could reference that script. That way you solve for both cases.

kaempfer0080 06-12-2013 11:07 AM

Alright I'll try the echo thing to see if it works. I discovered another issue where on reboot/network restart it sets both connections to eth1 for some reason. Having trouble figuring out why because the ifcfg-device files have device=eth0 and device=eth1 respectively.

e: I got the log output so I assume it's working, but I can't know til I get this other problem sorted out.

Firerat 06-12-2013 02:16 PM

Quote:

Originally Posted by kaempfer0080 (Post 4969719)
..
And that fixed the problem. However, whenever the server restarts or the network goes down or whatever, the route config switches back to the default which doesn't work. I want to write a script and place it in init.d that just executes the 2 lines written above. I've done a lot of googling but the examples I find are complex with starts, stops, conditionals, etc. I just want to run the two lines. How can I do this?

just wondering if it would be better to set it up in /etc/network/interfaces

Code:

man interfaces
an init.d script might* work for server restarts, but the network coming back up.. it won't

* might work !! but probably won't as the network will likely 'come up' after your script ran, clobbering it's work.

thedaver 06-12-2013 02:49 PM

Hey, I said that! :-)

Firerat 06-12-2013 03:25 PM

Quote:

Originally Posted by thedaver (Post 4970503)
Hey, I said that! :-)

:) well yes and no
  • Not enough weight given to correctly configuring the network
  • The rc.local would be redundant if also handled by the networking scripts, so no need for AND.
  • Pointing to a script that 'fudges' something which can be handled within the standard configuration is poor practice.
None of which really matter all that much on a personal system, but the workplace is a different matter.
Someone may inherit responsibility for that server/system and not necessarily with the benefit of a personalized handover.
It is, in my opinion, only polite to do things in a standard way.

Please for give me for being so pedantic ;)

chrism01 06-12-2013 07:05 PM

Re eth1 vs eth0; have you set the MAC addresses in the ifcfg-ethX files? Check if they're the same.
See also /etc/udev/rules.d/70-persistent-net.rules


All times are GMT -5. The time now is 11:24 PM.