Changing IP addresses on Linux systems

June 11, 2008, 12: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

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

I like it!
Close

On Twitter now

Linux

Powered by Twitter
You are logged in | Sign out
Sign in and post to Twitter

What are you thinking?

Cancel Tweet sent

On Twitter now

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.
peer-to-peer

Esther Schindler
If the comments are ugly, the code is ugly

claird
SVG a graphics format for 21st century

pasmith
Take Chrome OS for a test spin

Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?

sjvn
64-bits of protection?

jfruh
Android fragments vs. the iPhone monolith

mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive

 

Where Google Chrome security fails: the password
I heard mention that the Chrome OS will have some sort of encryption available a la bitlocker. If it's possible to encrypt personal data using another password or key, then it may have potential for very secure data.... And Ubuntu has an 'encrypt home directory' option, perhaps google should follow suit.
- Dann

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

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.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
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.

Marketplace