Thursday, July 5, 2007

beginners CLI guide for static ip address

here are different ways of putting static ip with linux. assuming the system has a properly connected RJ-45 network cable with a working well broadcasting NIC card, using any of the 21 redhat-based linux distro with reference here

if you would like to have an ip address of 192.168.2.221 for eth0 without any default gateway, as root from CLI, simply issue any of the below commands:

# ifconfig eth0 192.168.2.221 up

optionally combined with:

# route add -net 192.168.2.0 netmask 255.255.255.0 eth0

another sets are:

# ifconfig eth0 192.168.2.221 netmask 255.255.255.0

# ifconfig eth0 192.168.2.221 netmask 255.255.255.0 up

by default, it would put the ip address and make the NIC operational without any issues to kernel drivers.

an alternative way is using static eth0 config file. you could launch your fave CLI editor, edit /etc/sysconfig/network/network-scripts/eth0 and paste the ff:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DEVICE=eth0
BOOTPROTO=none
BROADCAST=192.168.2.0
IPADDR=192.168.2.221
NETMASK=255.255.255.0
NETWORK=192.168.2.0
ONBOOT=yes
TYPE=Ethernet
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

and remember to restart the NIC service to take effect like so

#/sbin/service network restart

IP ADDRESS VERIFICATION:

to see your current IP address settings from CLI, just issue any of the below commands from root:

#ifconfig
#ifconfig eth0
#ip address

DEFAULT GATEWAY:

supposed that your default gateway is 192.168.2.1, just issue

#route add default gw 192.168.2.1

or the oldest way around - by editing the static config file /etc/sysconfig/network and making sure it contains following line

GATEWAY=192.168.2.1

then restart your NIC (network) service again

/sbin/service network restart

ROUTE VERIFICATION:

#route -n
#netstat -arn
#ip route show


RECEIVING/SENDING VERIFICATION:

you should be able to see your default route IP address from the very last line of your screen. and by doing so

#ping 192.168.2.1

would confirm that your gateway is reachable at this point.

overall, we could just issue altogether like

#ifconfig eth0 192.168.2.221 netmask 255.255.255.0 default 192.168.2.1 up

and ping the router from the host.

***Note that doing the CLI command is not a permanent state after reboot.


DISABLING ETHERNET/NETWORK FUNCTIONS:

these are your alternatives then

#ifup eth0
#ifdown eth0
#/sbin/service network stop

at any time, you may replace eth0 with eth1 or such. that goes the same with ip address and default gateway.

if you are having problems with your NIC for negotiation problems, link status and/or you might want to change your ethernet card settings, try

# ethtool eth0

If your gateway is supposed to forward your packets to the internet, you could check if your box can reach the internet without any further DNS setup, this can be done by pinging an IP address located from WWW like

#ping 198.6.1.2

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy