Linux how to automatically obtain an IP address and configure a static IP address to access the Internet

  


Step 1: Activate the network card
After the system is installed, the default network card is eth0, use the following command to activate this network card. # ifconfig eth0 up
Step 2: Set the network card to start when entering the system
You want to automatically obtain the IP address to access the Internet every time you turn it on. You must set the network service to start when the system starts. Linux is different from Windows. Many services are stopped by default, and you start the service when you use the service one time, but you don't set it to be the default. The next time you enter the system, the service is still stopped. The following is how to set up the network service to start when the system starts.
Use the chkconfig command to make the network service start by default when the system startup level is 2345.
#chkconfig --level 2345 network on
Step 3: Modify the NIC file ifcfg-eth0
Modify the ifcfg-eth0 file and set the ONBOOT value to yes to let the network service use the network card when it starts. Set the BOOTPROTO value to dhcp to let the NIC automatically obtain an IP address from the DHCP server.
# vi /etc /sysconfig /network-scripts /ifcfg-eth0
ONBOOT = yesBOOTPROTO = dhcp

summary:
usual case, the first three steps are the most important, because most The Linux system default network service is also started when the system starts, and the network card is also enabled. Just set the third step, then use the following command to start the network service.
# service network start
Configure the static IP address to go online with the dynamic IP address, modify ifcfg-eth0, and then restart the network service with the service network retart command.
DEVICE = eth0IPADDR = 192.168.1.100NETMASK = 255.255.255.0GATEWAY = 192.168.1.1DNS = 8.8.8.8BOOTPROTO = staticONBOOT = yes


after performing the above three steps, you can get the ip. But later it was not possible to get ip, and repeating the above did not work.


#dhclient eth0

Remarks:

Linux Force DHCP client (dhclient) to renew ip addressLinux renew ip command$ sudo dhclient -r //release ip Release IP$ Sudo dhclient //Get IP////////////////Original start //////////////////////////////Now Acquire fresh IP:$ sudo dhclient There is no need to restart network service. Above command should work with any Linux distro such as RHEL, Fedora, CentOS, Ubuntu and others. On a related note you can also try out the following commands:# Ifdown eth0# ifup eth0# /etc/init.d/network restart OR# /etc/init.d/networking restart



Copyright © Windows knowledge All Rights Reserved