Linux操作系统下为网卡配置ip
by:授客
1. Linux单一网卡设置多IP的配置方法
1. Linux single-net card configuration to set multi-IPs
在Linux下网卡接口逻辑名被称为eth0,eth1,eth2,.....,ethN,所有网卡接口的配置文件都存储在/etc/sysconfig/network-scripts/下,文件名是以ifcfg-eth0,ifcfg-eth1,ifcfg-eth2,....,ifcfg-ethN形式来命名的。一个网卡接口对应一个配置文件。如果我们要给某个网卡接口配置两个IP地址,我们该怎么办呢?
The logical name for the net-card interface at Linux is eth0, eth1, eth2,..., ethN, the configuration files for all the web-card interfaces are stored at /etc/sysconfig/work-scripts/, in the form of ifcfg-eth0, ifcfg-eth1, ifcfg-eth2,..., ifcfg-ethN. A web-card interface corresponds to a configuration file. What do we do if we have to configure two IPs for a web-card interface?
例:为网卡接口eth0配置两个ip
Example: Configure two ips for webcard interface eth0
步骤1.复制网卡接口eth0配置文件
Step 1. Copy webcard interface eth0 configuration file
# cp ifcfg-eth0 ifcfg-eth0:0
说明:eth*:n 虚拟网卡接口,建立于eth0网卡接口边上,*表示网卡(接口)号,n表示虚拟网卡接口号,范围0-255
Note: Eth*:n Virtual net card interface, built on the Eth0 net card interface,* means the net card (interface) number,n means the virtual net card interface number, range 0-255
步骤2.编辑修改ifcfg-eth0:0中相应的字段
Step 2. Edit the corresponding field in ifffg-eth0:0
假设ifcfg-eth0本身已经有ip了,配置如下:
Assuming that ifcfg-eth0 itself already has ip, it is configured as follows:
DEVICE=eth0 //这里是网卡设备名,改为eth0:0
DEVICE =eth0 // This is the name of the network card device, changed to eth0:0
BOOTPROTO=none //设置为none,设置为静态IP:static;设置为动态IP:dhcp
BOOTOTROTO=none / / set to nonone, set to static IP:static; set to dynamic IP:dhcp
BROADCAST=10.4.255.255 //广播地址
ROADAST = 10.4.255.255 // broadcast address
HWADDR=08:00:27:5C:56:2A //MAC地址,不需要修改
HWADR=08:00:27:5C:56:2A//MAC address, no change required
IPADDR=10.4.8.191 //ip地址,改为你要设置的ip地址
IPADDR = 10.4.8.191 //ip to the ip address you want to set
NETMASK=255.255.0.0 //子网掩码,改为你要设置的掩码
NETMASK = 255.255.0.0 // Subnet mask changed to the mask you want to set
NETWORK=10.4.0.0 //网络地址
NETWORK = 10.4.0.0 / web address
DNS1=8.8.8.8 //DNS地址,ping命令出现unkonwn host时,一般是未配置DNS所致//查看/etc/resolv.conf文件可看到配置是否起作用,注意该文件随ifcfg-ethN配置而动态变化的,所以最好在ifcfg-ethN配置文件中该添加DNS配置信息
DNS1 = 8.8.8.8 //DNS address, when the ping command appears unkonwn host, usually the DNS-induced/view/etc/resolv.conf file can see whether the configuration works, noting that it changes with ifcfg-ethN configuration, so it is better to add DNS profile information to the ifcfg-ethN configuration file
ONBOOT=yes //ONBOOT指明系统启动时是否激活网卡,只有在激活状态才能连接网络,不激活:no;激活:yes
ONBOOT=yes//ONBOOT specifies whether to activate the netcard at the start of the system, to connect the network only in active state, not: no; to activate: yes
USERCTL=yes //该选项指定是否允许非root用户控制该设备,允许:yes;不允许:no
USERCTL=yes//This option specifies whether non-root users are allowed to control the device, allowing: yes; not allowed:no
TYPE=Ethernet //网络类型,表明是以太网:Ethernet
TYPE = Ethernet / Network type, indicating in toonet: Ethernet
GATEWAY=10.4.0.1 //默认网关地址
GATEWAY = 10.4.0.1 // Default gateway address
编辑后的ifcfg-eth0配置如下
Editd ifcfg-eth0 configured as follows:
DEVICE=eth0:0
BOOTPROTO=none
BROADCAST=10.4.255.255
HWADDR=08:00:27:5C:56:2A
IPADDR=10.4.125.192
NETMASK=255.255.255.0
NETWORK=10.4.0.0
ONBOOT=yes
USERCTL=yes
TYPE=Ethernet
GATEWAY=10.4.0.1
同一块网卡虚拟的两个IP地址,可以不用添加新的路由,系统会自动的装载路由表。
The two IP addresses of the same net card can be loaded automatically without adding a new route.
步骤3.ifconfig命令为网卡绑定ip
为0号网卡的虚拟网络接口eth0:2配置ip 10.4.125.192
Configure ip 10.4.1225.192 for virtual network interface eth0:2 for web card 0
# ifconfig eth0:0 10.4.125.192 netmask 255.255.255.0
注意:如果单独做第3步,不做第2步,重启机器后,配置就失效了
Note: If you do step 3 alone, if you don't step 2, when the machine is restarted, the configuration will lapse.
2. Linux为多个网卡设置单一ip的配置方法
2. Linux sets a single p configuration for multiple net cards
步骤1.编辑网卡接口配置文件
1.网卡接口ifcfg-eth0配置文件
1. Web-card interface config file ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
BROADCAST=10.4.255.255
HWADDR=08:00:27:5C:56:2A
IPADDR=10.4.8.191
NETMASK=255.255.0.0
NETWORK=10.4.0.0
ONBOOT=yes
USERCTL=yes
PEERDNS=no
TYPE=Ethernet
GATEWAY=10.4.0.1
2.网卡接口ifcfg-eth1配置文件
2. Webcard interface config file ifcfg-eth1
DEVICE=eth1
BOOTPROTO=none
BROADCAST=10.4.255.255
HWADDR=08:00:27:5C:56:2A
IPADDR=10.4.125.192
NETMASK=255.255.255.0
NETWORK=10.4.0.0
ONBOOT=yes
USERCTL=yes
PEERDNS=no
TYPE=Ethernet
GATEWAY=10.4.0.1
步骤2.添加静态路由[临时性]
方法一:
Method I:
# route add -net 10.4.0.0 netmask 255.255.0.0 gw 10.4.0.1 up
# route add -net 10.4.0.0 netmask 255.255.255.0 gw 10.4.0.1 up
查看路由表
View route sheets
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
10.4.0.0 10.4.0.1 255.255.0.0 U 0 0 0 eth0
10.4.0.0 10.4.0.1 255.255.255.0 U 0 0 0 eth1
方法二:
Method II:
# route add -net 10.4.0.0 netmask 255.255.0.0 up
# route add -net 10.4.0.0 netmask 255.255.255.0 up
再为系统增加一条缺省路由,因为缺省的路由是把所有的数据包都发往它的上一级网关(假设地址是10.4.0.1,这个地址依赖于使用的网络而定,
Add a missing route to the system because the missing route is to send all data packages to its upper gateway (assuming the address is 10.4.0.1), depending on the network used,
# route add default gw 10.4.0.1
但重新启动后,上述配置就失效了,因此可在/etc/sysconfig/network-scripts/目录下建立文件。因为我们是做内网的路由,并且内网网卡接口为eth0,因此我们建立route-eth0文件如下:
However, the configuration failed after it was restarted, so the files can be created under the /etc/sysconfig/network-scripts/ directory. Because we are the route to the inner network and the inner network card interface is eth0, we have created the following root-eth0 document:
# cd network-scripts
# vi route-eth0
10.4.0.0/16 via 10.4.0.1 dev eth0
10.4.0.0/24 via 10.4.0.1 dev eth1
# /etc/rc.d/init.d/network restart启动生效
# /etc/rc.d/init.d/network start
步骤3.启动IP转发功能使得linux能够转发数据包。
Step 3. Launching the IP forwarding function enables Linux to forward the data package.
3.1将/proc/sys/net/ipv4/ip_forward 文件内容置1[临时生效]
3.1 Place the content of the/proc/sys/net/ipv4/ip_forward document 1 [provisional entry into force]
echo “1” > /proc/sys/net/ipv4/ip_forward
IP Forward 将立即生效,但重启动失效。
IP Forward will be effective immediately, but restart will not be effective.
3.2
# vi /etc/sysctl.conf[永久生效]
#vi /etc/syscl.conf [permanent entry into force]
net.ipv4.ip_forward=1
注:默认情况下
Note: By default
同一网卡,不可以设置同一网段的多个ip,会冲突
The same web card, you can't set multiple ips in the same section. It conflicts.
不同网卡,不可以设置同一网段的不同ip,会冲突-->自动修改原来的路由表可能导致网络不同
Different webcards, you can't set different ips in the same section, you can conflict -- > automatic modification of the original route list may lead to a different network
关于这个结论可能并不是100%正确,具体以实际环境为准(出现问题时,关键看路由表)。
may not be 100% correct with regard to this conclusion, depending on the actual circumstances (the key line of reference in the event of a problem).
---------------------------------------------------------------------------
测试实验
Test Experiment
为不同网卡,设置同一网段的不同ip
Set different ips for the same section for different web cards
例子:测试机ip 10.5.8.183,网关10.5.0.1,通过网卡接口eth2同外面的10.4.0.0网段进行网络通信,修改eth1接口ip之前,路由表如下,网络可通
Example: Test machine ip 10.5.8.183, Gateway 10.5.0.1, network communication via webcard interface eth2 with outside web segment 10.4.0.0, prior to modification of eth1 interface ip, route table below, network accessible
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.5.0.0 * 255.255.0.0 U 0 0 0 eth2
1.0.0.0 * 255.0.0.0 U 0 0 0 eth3
default 10.5.0.1 0.0.0.0 UG 0 0 0 eth2
接着,系统界面上修改eth1接口ip为10.5.8.187,网关10.5.0.1,生效,发现10.4.0.0网段无法访问ip 10.5.8.183了
The system interface then changed the eth1 interface to 10.5.8.187 and the gateway to 10.5.0.1. Effective, it was found that access to ip 10.5.8.183 was not possible in the 10.4.0.0 section.
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.5.0.0 * 255.255.0.0 U 0 0 0 eth1
10.5.0.0 * 255.255.0.0 U 0 0 0 eth2
1.0.0.0 * 255.0.0.0 U 0 0 0 eth3
default 10.5.0.1 0.0.0.0 UG 0 0 0 eth1
注:此时,ping网关10.5.0.1, ping不通,从route命令的输出来看,我们可以发现路由表已经被修改了,要通往默认网关,必须经过eth1,但是eth1并没有接网线,所以不通。把eth1的网线接到交换机,再ping网关可ping通
NOTE: At this point, ping gateway 10.5.0.1, ping is disabled. From the output of the roote command, we can see that the route sheet has been modified to go to the default gateway through Eth1, but Eth1 has no connection, so it doesn't work.
断开eth1的网线,手动修改路由表,修改后如下
Disconnect Eth1's network and manually modify the router's table, as follows:
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.5.0.0 * 255.255.0.0 U 0 0 0 eth2
10.5.0.0 * 255.255.0.0 U 0 0 0 eth1
1.0.0.0 * 255.0.0.0 U 0 0 0 eth3
default 10.5.0.1 0.0.0.0 UG 0 0 0 eth2
发现也可以ping通网关了,,
I’m not sure what I'm going to do, but I don’t know what I'm going to do, but I don’t know what I'm going to do.
说明:linux是这么匹配的,按路由表条目顺序,从上到下进行匹配,添加的默认网关总是放到表中最后,也就是最下面。同时也可以看出,要能相互通信,路由表至少有一条本地路由和一条默认路由
Note: Linux matches this, matching top to bottom by route list entries, adding the default gateway always at the end of the table, which is the bottom. It also shows that, to be able to communicate with each other, the route must be at least one local route and one default route.
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论