80系统单网口网络配置

问题描述

单个网络接口设备静态、dhcp以及子网等网络配置方法

适用范围

linx-6.0.80

解决方案

1. 打开终端,使用cd命令切换到网络配置目录

~# cd /etc/network/

~# ls

if-down.d if-pre-up.d interfaces routes if-post-down.d if-up.d run

interfaces:网络配置文件

if-down.d if-post-down.d if-pre-up.d if-up.d:是网络关闭前、网络关闭后、网络建立前、网络建立后都会到/etc/network/里运行相应目录下的脚本

2. 编辑interfaces网络配置文件,如下以设置eth0静态IP为例

~# vim /etc/network/interfaces

# This file describes the network interfaces available on your system

# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface

auto lo

iface lo inet loopback

auto eth0# eth0接口统启动时被自动配置

allow-hotplug eth0# 将网络接口设置为热插拔模式

iface eth0 inet static# eth0接口具有一个静态的(static)IP配置

address 192.168.1.100# ip地址

netmask 255.255.255.0# 掩码

network 192.168.1.0# 网段

gateway 192.168.1.254# 网段

pre-up ip addr flush dev $IFACE# pre-up激活网卡之前需要需要执行的命令

3. 重新启动网络服务,使ip配置生效

~# systemctl restart networking.service

4. 检查ip网络配置是否生效

~# ifconfig

Link encap:Ethernet HWaddr 00:0c:29:7f:98:f6eth0

inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fe7f:98f6/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:17805 errors:0 dropped:0 overruns:0 frame:0

TX packets:904 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:1708959 (1.6 MiB) TX bytes:125068 (122.1 KiB)

Link encap:Local Loopbacklo

inet addr:127.0.0.1 Mask:255.0.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING MTU:65536 Metric:1

RX packets:8 errors:0 dropped:0 overruns:0 frame:0

TX packets:8 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:512 (512.0 B) TX bytes:512 (512.0 B)

~# ping -c 2 192.168.1.1

PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.

64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.057 ms

64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.115 ms

--- 192.168.1.1 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 1027ms

rtt min/avg/max/mdev = 0.054/0.082/0.132/0.021 ms

5. 创建子网配置如下

~# vim /etc/network/interfaces

# This file describes the network interfaces available on your system

# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface

auto lo

iface lo inet loopback

auto eth0

allow-hotplug eth0

iface eth0 inet static

address 192.168.1.100

netmask 255.255.255.0

network 192.168.1.0

gateway 192.168.1.1

pre-up ip addr flush dev $IFACE

auto eth0:0

allow-hotplug eth0:0

iface eth0:0 inet static

address 192.168.2.100

netmask 255.255.255.0

network 192.168.2.0

6. 多个ip网络设置如下

先确定网口数量以及对应名称

~# ifconfig -a

Link encap:Ethernet HWaddr 00:0c:29:7f:98:f6eth0

inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fe7f:98f6/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

TX packets:1232 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:12393 (12.1 KiB) TX bytes:711382 (694.7 KiB)

Link encap:Ethernet HWaddr 00:0c:29:7f:98:00eth1

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:116 errors:0 dropped:0 overruns:0 frame:0

TX packets:1232 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:12393 (12.1 KiB) TX bytes:711382 (694.7 KiB)

Link encap:Ethernet HWaddr 00:0c:29:7f:98:f6eth2

UPUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:13642 errors:0 dropped:0 overruns:0 frame:0

TX packets:12 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:2696472 (2.5 MiB) TX bytes:13176 (12.8 KiB)

Link encap:Local Loopbacklo

inet addr:127.0.0.1 Mask:255.0.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING MTU:65536 Metric:1

RX packets:12 errors:0 dropped:0 overruns:0 frame:0

TX packets:12 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:712 (712.0 B) TX bytes:712 (712.0 B)

增加配置eth1和eth2,其中配置文件interfaces只能存在一个默认网关gateway参数,其他网口以添加路由的方式设置

~# vim /etc/network/interfaces

# This file describes the network interfaces available on your system

# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface

auto lo

iface lo inet loopback

auto eth0

allow-hotplug eth0

iface eth0 inet static

address 192.168.1.100

netmask 255.255.255.0

network 192.168.1.0

gateway 192.168.1.1

pre-up ip addr flush dev $IFACE

auto eth1

allow-hotplug eth1

iface eth1 inet static

address 192.168.2.100

netmask 255.255.255.0

network 192.168.2.0

pre-up ip addr flush dev $IFACE

auto eth2

allow-hotplug eth2

iface eth2 inet static

address 192.168.3.100

netmask 255.255.255.0

network 192.168.3.0

pre-up ip addr flush dev $IFACE

7. DHCP自动获取IP地配置如下

~# vim /etc/network/interfaces

# This file describes the network interfaces available on your system

# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface

auto lo

iface lo inet loopback

auto eth0

allow-hotplug eth0

iface eth0 inet dhcp

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐