Configure static IP addresses in Linux for Internet access and automatic access to IP addresses

Source: Internet
Author: User
Tags bind get ip iptables nameserver

Automatically get IP address

First step: Activate the network card

Linux system installed after the default network card is eth0, with the following command to activate this network card.

The code is as follows Copy Code

# ifconfig eth0 up

Step two: Set the network card to start when entering the system
If you want to automatically get the IP address to the Internet every time you turn on, you should set up the network service when the system starts. Linux is a bit different from Windows is a lot of services by default is stopped, and you use the service when you start the service, but did not set it as the default startup, the next time to enter the system this service is still stopped. The following is a way to set up network services to start when the system starts.


Use the Chkconfig command to enable network services to start by default when the system boot level is 0123456.

The code is as follows Copy Code

# Chkconfig–level 0123456 Network on

Step three: Modify the NIC file Ifcfg-eth0


Modify the Ifcfg-eth0 file and set the value of Onboot to Yes so that the network service uses the NIC when it starts. Set the value of Bootproto to DHCP, allowing the network card to automatically obtain an IP address from the DHCP server.

The code is as follows Copy Code

# Vi/etc/sysconfig/network-scripts/ifcfg-eth0

Onboot=yes
Bootproto=dhcp

static IP address configuration

Configure static IP address Internet and dynamic IP address expatiating, modify Ifcfg-eth0, and then use command Service network retart Restart network services.
Cases:

The code is as follows Copy Code
Device=eth0
ipaddr=192.168.1.100
netmask=255.255.255.0
gateway=192.168.1.1
dns=8.8.8.8
Bootproto=static
Onboot=yes

The first: Use the command to modify (direct immediate effect, restart failure)

The code is as follows Copy Code
#ifconfig eth0 192.168.0.1 netmask 255.255.255.0 up

Description
Eth0 is the first NIC, and the other is eth1,eth*
192.168.0.1 is the IP address configured for the first NIC configured for the NIC
Netmask 255.255.255.0 is configured with a subnet mask
Up is to indicate immediate activation
More parameter descriptions for Ifconfig view related help

The code is as follows Copy Code
Gateway
# route Add default GW 192.168.30.1 eth0
Dns:
# VI Etc/resolv.conf
NameServer 202.131.80.1
NameServer 202.131.80.5

If you want to get back into effect, write these in the start script rc.local

Second: Modify the configuration document (need to restart network configuration, always in effect)


First, modify the IP address
manually change/etc/sysconfig/network-scripts/ifcfg-eth0 file #第二块网卡: vi/etc/sysconfig/network-scripts/ifcfg-eth1

The code is as follows Copy Code
Device=eth0 #物理设备名
ipaddr=192.168.1.10 #IP地址
netmask=255.255.255.0 #掩码值
network=192.168.1.0 #网络地址 (but not)
broadcast=192.168.1.255 #广播地址 (but not)
gateway=192.168.1.1 #网关地址
Onboot=yes # [Yes|no] (whether the device is activated at boot time)
Userctl=no #[yes|no] (whether a non-root user can control the device)
Bootproto=static #[none|static|bootp|dhcp] (Do not use protocol at boot | static Allocation | BOOTP protocol | DHCP protocol)

Note: Single NIC binding two IP
The Linux device configuration file is stored in the/etc/sysconfig/network-scripts,
For the first device on Ethernet, the profile name is typically Ifcfg-eth0
If you need to bind more than one IP address for the first device,
You only need to create a file named ifcfg-eth0:0 in the/etc/sysconfig/network-scripts directory.
Examples of the content are:

The code is as follows Copy Code
Device= "eth0:0"
Ipaddr= "211.100.10.119"
netmask= "255.255.255.0"

If you need to bind more than one IP address, simply add the file name and the eth0:x in the device in the file. Linux can support up to 255 IP aliases.

The device is the name of the device, ipaddr the IP address of this device, netmask is the subnet mask, and Onboot indicates that it starts automatically at startup.
Second, modify the gateway

The code is as follows Copy Code
Vi/etc/sysconfig/network
Networking=yes
Hostname=aaron
gateway=192.168.30.1

Third, modify DNS
The configuration file is below/etc/resolv.conf.

The code is as follows Copy Code
Vi/etc/resolv.conf
NameServer 202.109.14.5 #主DNS
NameServer 219.141.136.10 #次DNS

Search Localdomain
Four, restart network configuration
There are several methods:

The code is as follows Copy Code
1:root Account # Service Network restart
2:#/etc/init.d/network Restart
3:# Ifdown eth0
# ifup Eth0
4:# ifconfig eth0 Down
# ifconfig eth0 up

Five, start and shutdown the firewall
Immediate effect:

The code is as follows Copy Code
1: #service iptables start
#service iptables Stop

Reboot system in effect:

The code is as follows Copy Code
2: #service iptables on
#service iptables off

Six, modify the host name

Vi/etc/sysconfig/network, modify hostname row to hostname= host name, reboot to take effect

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.