- How to Set Static IP Address and Configure Network in Linux
- Configure Static IP Address in RHEL/CentOS/Fedora:
- Set Static IP Address in Debian / Ubuntu
- Conclusion:
- NetworkConfigurationCommandLine
- Find Network Interface Card
- Configuring Static IP Address For Your Network Card
- Setting up Second IP Address or Virtual IP Address
- Setting Your Hostname
- Setting up DNS
- Configuring DHCP Address for Your Network Card
- Howto Set MTU for a DHCP Connection
How to Set Static IP Address and Configure Network in Linux
If you are a Linux system administrator, time will come when you will need to configure networking on your system. Unlike desktop machines where you can use dynamic IP addresses, on a server infrastructure, you will need to setup a static IP address (at least in most cases).
This article is meant to show you how to configure static IP address on most frequently used Linux distributions.
For the purpose of this tutorial, we will use the following Internet Protocol version 4 (IPv4) details:
IP address: 192.168.0.100 Netmask: 255.255.255.0 Hostname: node01.tecmint.com Domain name: tecmint.com Gateway: 192.168.0.1 DNS Server 1: 8.8.8.8 DNS Server 2: 4.4.4.4
Configure Static IP Address in RHEL/CentOS/Fedora:
To configure static IP address in RHEL / CentOS / Fedora, you will need to edit:
/etc/sysconfig/network /etc/sysconfig/network-scripts/ifcfg-eth0
Where in the above «ifcfg-eth0″ answers to your network interface eth0 . If your interface is named “ eth1» then the file that you will need to edit is «ifcfg-eth1» .
Let’s start with the first file:
NETWORKING=yes HOSTNAME=node01.tecmint.com GATEWAY=192.168.0.1 NETWORKING_IPV6=no IPV6INIT=no
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
Note: Make sure to open the file corresponding to your network interface. You can find your network interface name with ifconfig -a command.
In that file make the following changes:
DEVICE="eth0" BOOTPROTO="static" DNS1="8.8.8.8" DNS2="4.4.4.4" GATEWAY="192.168.0.1" HOSTNAME="node01.tecmint.com" HWADDR="00:19:99:A4:46:AB" IPADDR="192.68.0.100" NETMASK="255.255.255.0" NM_CONTROLLED="yes" ONBOOT="yes" TYPE="Ethernet" UUID="8105c095-799b-4f5a-a445-c6d7c3681f07"
You will only need to edit the settings for:
Other settings should have already been predefined.
Next edit resolve.conf file by opening it with a text editor such as nano or vi:
nameserver 8.8.8.8 # Replace with your nameserver ip nameserver 4.4.4.4 # Replace with your nameserver ip
Once you have made your changes restart the networking with:
# /etc/init.d/network restart [On SysVinit] # systemctl restart network [On SystemD]
Set Static IP Address in Debian / Ubuntu
To setup static IP address in Debian/ Ubuntu, open the following file:
# nano /etc/network/interfaces
You may see a line looking like this:
auto eth0 iface eth0 inet dhcp
Change it so it looks like this:
auto eth0 iface eth0 inet static address 192.168.0.100 netmask 255.255.255.0 gateway 192.168.0.1 dns-nameservers 4.4.4.4 dns-nameservers 8.8.8.8
Save the file and then edit /etc/resolv.conf like this:
nameserver 8.8.8.8 # Replace with your nameserver ip nameserver 4.4.4.4 # Replace with your nameserver ip
Restart the networking on your system with:
# /etc/init.d/network restart [On SysVinit] # systemctl restart network [On SystemD]
Your static IP address has been configured.
Conclusion:
You now know how to configure a static IP address on a Linux distro. If you have any questions or comments, please do not hesitate to submit them in the comment section below.
NetworkConfigurationCommandLine
You can configure a network interface from the command line. You configure your network client hosts with the command line by using commands to change your current settings or by editing a number of system files.
Find Network Interface Card
When setting up your network you will need to know the network interface cards on your computer. The interface name of cards for different vendors may be different, which is why this step is needed.
This will list the interface names for all NICs on your computer. It will probably include eth0 (hardwired NIC), lo (loopback interface for the localhost), and something for your wireless card (like wifi0, or wlan0).
Configuring Static IP Address For Your Network Card
Configure a Static IP address by editing /etc/network/interfaces. Replace eth0 with your network interface card (see Find Network Interface Card).
sudo nano /etc/network/interfaces
# The primary network interface auto eth0 iface eth0 inet static address 192.168.2.33 gateway 192.168.2.1 netmask 255.255.255.0 network 192.168.2.0 broadcast 192.168.2.255
For these settings to take effect you need to restart your networking services.
sudo /etc/init.d/networking restart
Setting up Second IP Address or Virtual IP Address
If you need to set up a second ip address you need to edit the /etc/network/interfaces.
sudo nano /etc/network/interfaces
auto eth0:1 iface eth0:1 inet static address 192.168.2.33 netmask 255.255.255.0 network x.x.x.x broadcast x.x.x.x gateway x.x.x.x
You need to enter all the details like address,netmask,network,broadcast and gateways values.
For these new settings to take effect you need to restart networking services using the following command
sudo /etc/init.d/networking restart
Setting Your Hostname
The hostname command allows you to directly query, or set, the hostname from the command line.
You can see your current hostname with
To set the hostname directly you can become root and run
When your system boots it will automatically read the hostname from the file /etc/hostname
Setting up DNS
You can add hostname and IP addresses to the file /etc/hosts for static lookups.
To cause your machine to consult with a particular server for name lookups you simply add their addresses to /etc/resolv.conf.
For example a machine which should perform lookups from the DNS server at IP address 192.168.3.2 would have a resolv.conf file looking like this
enter the following details
search myaddress.com nameserver 192.168.3.2
Configuring DHCP Address for Your Network Card
The DHCP address can be configured by editing the following file /etc/network/interfaces. Replace eth0 with your interface card (see Find Network Interface Card).
sudo nano /etc/network/interfaces
# The primary network interface – use DHCP to find our address auto eth0 iface eth0 inet dhcp
Howto Set MTU for a DHCP Connection
Although this is not documented in the manual for interfaces, MTU for a DHCP connected device can be set in the /etc/network/interfaces file. To do so you need to append the ‘pre-up’ command to the ‘iface’ section of the relevent interface.
iface eth0 inet dhcp pre-up /sbin/ip link set $IFACE mtu 1492
The above example sets the MTU for device eth0 to 1492, the usual MTU for a PPPoE ISP connection. This however is only needed if connections seem to hang otherwise (with the default of 1500).
NetworkConfigurationCommandLine (последним исправлял пользователь ip72-213-131-215 2009-08-14 04:14:29)
The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details