Jan 26, 2014

How to configure static network IP address in Linux

Sometimes in Linux we need to have a static IP address, for example to install a Hadoop cluster, this is a recommended step. Though in windows it can be done graphically, in Linux it has an interesting way to do through a few commands. Let's take a look.

Part One: configure a static IP address in a physical machine
Note: here a physical machine I mean a machine that installs Linux OS directly, rather than a virtual machine.
Also almost all operations here need a sudo access, so we can sudo to an qualified account or add sudo ahead to each of the command.
1. view or edit 70-persistent-net-rules file
open a terminal, type
cat /etc/udev/rules.d/70-persistent-net-rules



By default, the name will be eth0, which means it will use a file name "ifcfg-eth0" to control the network settings,  but you can also let it use a new control file, say for example eth1. This can be done by in terminal, type
vim /etc/udev/rules.d/70-persistent-net-rules

and modify the name value to eth1.

2. Modify a configuration file
cd /etc/sysconfig/network-scripts
ls


You will see a file named ifcfg-eth0, which is the network configuration file as we mentioned earlier.
If you step1 you configured it to use a new file for example eth1, then you need to create a new configuration file:


vim ifcfg-eth1

Please configure it like below.

Note:
1). NM_CONTROLLED must set to no, so that it's not controlled by a service called NetworkManager
2) BOOTPROTO value is static, because it's a static IP. If it's controlled by NetworkManager, the value is dhcp
3) Add the entry for GATEWAY, IPADDR, and DNS here. If you have multiple DNS, you can put DNS1=...; DNS2=...; if you have only one DNS, you just put DNS1=....
4) People might have question that how I can know what values I can input in step 3. Here are two commands that we can use: ifconfig and route

3. Add DNS IP information in /etc/resolv.conf
vim /etc/resolv.conf


4. Persistent the configuration
1) We want to stop NetworkManager, so that the IP is set as we input and will not be changed by the service.
service NetworkManager stop

2) Make service NetworkManager by default not started when reboot system
chkconfig NetworkManager off

3) If you are using a new control file, for example in step 1 you want it to use eth1, and the original one is eth0. We need to bring down eth0 and bring up eth1
ifdown eth0

ifup eth1

4) restart network service
service network restart

5) if in step 3 or 4 you met some errors for example saying eth1 cannot be initialized. please reboot the system
reboot


Now you will have a static IP successfully configured.

Part Two:  configure a static IP address in a VMware virtual machine
configure a static IP in virtual machine is similar as doing it in physical machine, but there are a few differences.
Step 1. Same as step 1 in part one
Difference: In case you run vim /etc/udev/rules.d/70-persistent-net.rules and get the result as below. You may not know which is the right one to change.

Here you need to check in VMware your virtual machine settings by
right click your virtual machine name --> Network Adapter --> Advanced
It will show the MAC address, comparing it with the result above, now you will now it's the second one to modify.

Step 2: the same as Step 2 in part one
Difference: VMware NAT settings by default use 192.168.136.2 as the DNS and GATEWAY


so your configuration setting can be like below

Step 3: the same as Step 3 in part one
Step 4: the same

OK. All set!

If you find this blog is useful, please kindly click the ads on this page to help. Thank you very much.

No comments: