ip addressing and routing
Level: - Beginner
Platform: - RED hat Linux 9.0
Keywords: - Assigning IP address, adding static routes, adding routes to startup
Author: - Dinesh Aggarwal
Assigning IP address to a network card is very simple. If you want to assign a IP address 172.31.117.1 with mask 255.255.255.0 to interface eth1 give the following command
ifconfig eth1 172.31.117.1 netmask 255.255.255.0
Now if you reboot the PC, this ip address will be gone.
NETWORK CONFIGURATION SHOULD BE SAVED IN THIS FILE
===============================================
vi /etc/sysconfig/network-scripts/ifcfg-eth0
In order to make IP address permanent, issue the following commands.
cd /etc/sysconfig/network-scripts
vi ifcfg-eth1
Now enter the following inside the file ifcfg-eth1 (Press I to start writing inside the file
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
NETMASK=255.255.255.0
BROADCAST=172.31.117.1
IPADDR=172.31.117.1
NETWORK=172.31.117.0
(Press ESC and then :w! )
Then file is saved now
Note:-
To save a vi file press esc then
:wq
To forcefully write a read only file
:wq!
To check the current IP address
root@localhost root]# ifconfig –a
eth0 Link encap:Ethernet HWaddr 00:10:B5:CC:83:DB
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:2058 errors:0 dropped:0 overruns:0 frame:0
TX packets:71 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:144622 (141.2 Kb) TX bytes:7759 (7.5 Kb)
Interrupt:9 Base address:0x6f00
eth1 Link encap:Ethernet HWaddr 00:D0:B7:BB:E6:2E
inet addr:172.31.117.1 Bcast:172.31.117.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:26071 errors:0 dropped:0 overruns:0 frame:0
TX packets:561 errors:6 dropped:0 overruns:0 carrier:6
collisions:2 txqueuelen:100
RX bytes:1499796 (1.4 Mb) TX bytes:96543 (94.2 Kb)
Interrupt:5 Base address:0x74c0 Memory:feaff000-feaff038
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:36914 errors:0 dropped:0 overruns:0 frame:0
TX packets:36914 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2520187 (2.4 Mb) TX bytes:2520187 (2.4 Mb)
Adding a static route:-
route [-v] [-A family] add [-net|-host] target [netmask Nm] [gw Gw]
[metric N] [mss M] [window W] [irtt I] [reject] [mod] [dyn]
[reinstate] [[dev] If]
route add -net 192.56.76.0 netmask 255.255.255.0 dev eth1
(adds a route to the network 192.56.76.x via "eth0". The Class C
netmask modifier is not really necessary here because 192.* is a
Class C IP address. The word "dev" can be omitted here.)
Route add –net 192.168.1.0/24 gw 192.168.1.1
(Adds a route 192.168.1.0/255.255.255.255 and the gateway will be 192.168.1.1)
Route add –host 192.168.1.1/32 gw 192.156.1.1
( adds a route for one host/IP 192.168.1.1 and the packet destined for this host should go to 102.156.1.1)
Use the following command to check the present routes.
[root@localhost network-scripts]# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
172.31.117.192 0.0.0.0 255.255.255.224 U 0 0 0 eth1
172.31.117.224 0.0.0.0 255.255.255.224 U 0 0 0 eth1
192.56.76.0 0.0.0.0 255.255.255.224 U 0 0 0 eth1
172.31.117.160 0.0.0.0 255.255.255.224 U 0 0 0 eth1
172.31.117.64 0.0.0.0 255.255.255.224 U 0 0 0 eth1
172.31.117.96 0.0.0.0 255.255.255.224 U 0 0 0 eth1
172.31.117.0 0.0.0.0 255.255.255.224 U 0 0 0 eth1
172.31.117.32 0.0.0.0 255.255.255.224 U 0 0 0 eth1
172.16.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
0.0.0.0 172.16.100.1 0.0.0.0 UG 0 0 0 eth0
[root@localhost network-scripts]#
Adding routes to the startup:-
All these added routes will be lost whenever we restart the pc, in order to make these route entries permanent ot to make sure that these comes back automatically whenever a pc is restarted, issue the following commands.
Vi /etc/rc.local
And insert the following command, save the file and exit.
route add -net 192.56.76.0 netmask 255.255.255.0 dev eth1
Route add –net 192.168.1.0/24 gw 192.168.1.1
Route add –host 192.168.1.1/32 gw 192.156.1.1
Rc.local is run every time your pc is restarted.
If you have any suggestions or want to add more to this article do write us an email articles@knowurtech.com
What Next?
If you liked this article, you can share it with others using the following link:
Related Content :