0

I need to route the outgoing traffic via a virtual ip address. I can do this using the 'ip' tool from the iproute-package.

Using ip route I can see which address is used as the source and I can change it using ip route change, but this is temporary and I loose this change after restarting the network service or the machine.

How can I do this using config files so that it's persistent? For example I was trying several options in /etc/sysconfig/network-scripts/route-eth0:0 but no luck.

Thanks

Ryan Babchishin
  • 6,160
  • 2
  • 16
  • 36
user3849013
  • 1
  • 1
  • 1

2 Answers2

1

In general you can easily add anything you want to run at boot time to /etc/rc.d/rc.local. So just add whatever ip route commands you want there. Simple and no messing around.

https://www.centos.org/docs/5/html/Installation_Guide-en-US/s1-boot-init-shutdown-run-boot.html

The /etc/rc.d/rc.local script is executed by the init command at boot time or when changing runlevels. Adding commands to the bottom of this script is an easy way to perform necessary tasks like starting special services or initialize devices without writing complex initialization scripts in the /etc/rc.d/init.d/ directory and creating symbolic links.

Or

You can go the more complex way and create a systemd service. https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Unit_Files.html

That way you can set it up to restart with the networking services.

Or

Perhaps you can make use of the custom network scripts in CentOS

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Networking_Guide/sec-NetworkManager_and_the_Network_Scripts.html

Custom Commands and the Network Scripts

Custom commands in the scripts /sbin/ifup-local, ifdown-pre-local, and ifdown-local are only executed when those devices are controlled by the /etc/init.d/network service. If you modified the initscripts themselves (for example, /etc/sysconfig/network-scripts/ifup-eth) then those changes would be overwritten by an initscripts package update. Therefore it is recommend that you avoid modifying the initscripts directly and make use of the /sbin/iflocal scripts, so that your custom changes will survive package updates. The initscripts just check for the presence of the relevant /sbin/iflocal and run them if they exist. The initscripts do not place anything in the /sbin/if*local scripts, nor does the initscripts RPM (or any package) own or modify those files.

Ryan Babchishin
  • 6,160
  • 2
  • 16
  • 36
1

You had to edit the /etc/sysconfig/network-scripts/route-eth0:0 file, adding an entry with the following syntax: <SUBNET>/<PREFIX_LENGTH> via <GATEWAY> dev <INTERFACE>

For example: 10.10.10.0/24 via 192.168.1.1 dev eth0:0

shodanshok
  • 44,038
  • 6
  • 98
  • 162
  • Assuming init.d and networking service, you don't edit the scripts, you edit the config files that the scripts load. e.g., if you call `ifup etho` or `ifdown eth0`, it refers to the contents of `/etc/sysconfig/network-scripts/ifcfg-eth0`. If you're using systemd and/or NetworkManager service, best bet is to use NetworkManager to set it up. Better to use the network-scripts config files, as this example demonstrates, the routes will be part of the network config and will automatically reload. – Jeter-work Aug 22 '16 at 21:59