How To Setup Static IP Address on a Headless Raspberry Pi

In this how to, I am going to share how to add persistent or static routes on CentOS 5.9 and 6.4 versions. Assuming the network information below configured on the server itself with one network interface card, eth0: –

Interface: eth0
IP Address: 172.16.0.1
Netmask: 255.255.255.0
Gateway: 172.16.0.254

We need to add two static routes below and these routes don’t route to the default route of eth0 interface, 172.16.0.254: -

Interface: eth0
Address: 192.168.1.0
Netmask: 255.255.255.0
Gateway: 172.16.0.11

Interface: eth0
Address: 10.20.30.0
Netmask: 255.255.255.0
Gateway: 172.16.0.12

Here is how we can do it by creating this /etc/sysconfig/network-scripts/route-eth0 file. You have two ways to define the content. I had tested on CentOS 5.9 and 6.4 version and it works.

Option 1, include the below in /etc/sysconfig/network-scripts/route-eth0 file: -

GATEWAY0=172.16.0.11
NETMASK0=255.255.255.0
ADDRESS0=192.168.1.0
GATEWAY1=172.16.0.12
NETMASK1=255.255.255.0
ADDRESS1=10.20.30.0

Option 2, include the below in /etc/sysconfig/network-scripts/route-eth0 file: -

192.168.1.0/24 via 172.16.0.11 dev eth0
10.20.30.0/24 via 172.16.0.12 dev eth0

You can restart the network service to have the changes to take effect using this command: -

# service network restart

If there is no error, you can verify the routes using this command: -

# route -n

or

# netstat -rn

Your routes will be added automatically after every reboot. Please note that if you need to add routes to eth1 interface then you need to create /etc/sysconfig/network-scripts/route-eth1 file. Hope this helps you. Thank you.

About wingloon

I am Linux engineer attempting to decode my knowledge through blogging in the World Wide Web. I will share my knowledge as much as possible about Linux (what I know best) and technology in general to all my readers.