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







