Changing IP addresses on Linux systems
Changing the IP address on a Linux system involves both changing the IP address using the ifconfig command and modifying the files that will make your change permanent. The process is very similar to the process you would follow on a Solaris system, except that a different set of files must be modified. The proper steps to take also depend on the particular Linux distribution you are using. Debian systems, for example, use different files to store network configuration than do RedHat systems.
For starters, we use the ifconfig command to modify the active IP address. A command like this makes the change:
# ifconfig eth0 inet 10.2.7.11
The ifconfig -a command will list the current settings as well as confirm that your network device is (or is not) eth0.
If the system needs to have its IP address changed, it may be joining a different subnet. If so, it will need to have its default route switched as well. Be careful when changing default routes not to break the connection that you are using to make the changes. Either make this change via a console connection or otherwise ensure that your connection to the system is not broken before you have completed your work.
# route add default gw 10.2.7.1 # route delete default gw 10.1.7.1
The files you need to modify to make the IP address change permanent include the /etc/hosts file and the file in the /etc/sysconfig/network-scripts directory that sets up the parameters for the particular network interface. Typically, it is the /etc/sysconfig/network-scripts/ifcfg-eth0 file that needs to be modified. This file contains information that describes the network interface, including the IP address, netmask and MAC address. This file also indicates whether the IP address is static or assigned by DHCP. Here's an example of the file when a static IP address is used:
DEVICE=eth0 BOOTPROTO=static IPADDR=10.2.7.11 NETMASK=255.255.255.0 HWADDR=00:02:B1:CC:11:32 ONBOOT=yes
If you do not have an /etc/sysconfig directory, your network configuration parameters might be stored instead in a file named /etc/network/interfaces -- as it is on Debian, Ubuntu and related distributions. That file will have a similar look to what is shown in the example below.
iface eth0 inet static address 10.2.7.11 netmask 255.255.255.0 network 10.2.7.0 broadcast 10.2.7.255 gateway 10.2.7.1
The script below could be used to both detect the files to be modified and then make the required changes. Notice that it expects the old and new IP addresses along with an optional new default route. The script does no checking of the arguments, so they must be added in the correct order.
#!/bin/bash
#=============================================
# Get IP info from command line
#=============================================
if [ $# -lt 2 ]; then
echo -n "Usage: $0 oldIP newIP [defaultRouter]"
exit 1
fi
oldIP=$1
newIP=$2
gw=$3
#=============================================
# Switch IP address for network interface
#=============================================
ifconfig eth0 inet $newIP
perl -p -i -e "s/^oldIP/$newIP/" /etc/hosts
if [ -f /etc/sysconfig/network-scripts/ifcfg-eth0 ]; then
perl -p -i -e "s/^IPADDR=$oldIP/IPADDR=$newIP/" \
/etc/sysconfig/network-scripts/ifcfg-eth0
fi
if [ -f /etc/network/interfaces ]; then
perl -p -i -e "s/address $oldIP/address $newIP/" \
/etc/network/interfaces
fi
#=============================================
# Re-add default route if provided
#=============================================
if [ $3 ]; then
route add default gw $gw
fi
This script will not move you to a static address if you are currently obtaining your IP address through DHCP.
ITworld.com
Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world
On Twitter now
Linux
Powered by Twitter
jfruh
Apple syncing patent can't come soon enough
pasmith
New Twitter features borrow from 3rd party clients
Esther Schindler
Open Source Changes the Software Acquisition Process
mikelgan
How to set up continuous podcast play on the new iTunes
David Strom
Five important Windows 7 mobility features
sjvn
Guard your Wi-Fi for your own sake
Sandra Henry-Stocker
Grepping on Whole Words
Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325
Join the conversation here
Quick, practical advice for IT pros. Made fresh daily.
Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.












