June 11, 2008, 1:25 PM — 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.