Changing IP addresses on Linux systems

June 11, 2008, 01:25 PM —  ITworld.com — 

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

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Resources
White Paper

Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.

Webcast

Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.

White Paper

Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.

Free stuff

VMware ESX Server in the Enterprise
By Edward L. Haletky
Published Dec 29, 2007 by Prentice Hall.
Enter now! | Official rules | Sample chapter

Green IT
By Toby Velte, Anthony Velte, Robert C. Elsenpeter
To be published Oct. 10, 2008 by McGraw Hill Professional
Enter now! | Official rules | About the book

Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

More Resources