linux下配置网络一般有三种方式: 1.命令配置(#ifconfig,#toute , #netstat,#ip) 2.基于图形或文本图形界面 3.编辑配置文件 命令配置: ifconfig主要是可以手动启动、观察与修改网络接口的相关参数,可以修改的参数很多,包括IP参数以及MTU等都可以修改 ifconfig命令用法及示例: ifconfig [-v] [-a] [-s] [interface] 例如: 查看ip信息默认显示激活状态的地址信息 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| [iyunv@localhost~]# ifconfig
eno33554984:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.249.102 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::20c:29ff:fee5:2d74 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e5:2d:74 txqueuelen 1000 (Ethernet)
RX packets 5542 bytes 636289 (621.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 278 bytes 28607 (27.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:flags=73<UP,LOOPBACK,RUNNING> mtu65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 7 bytes 748 (748.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7 bytes 748 (748.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
查看所有ip信息包括未激活的 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| [iyunv@localhost~]# ifconfig -a
eno16777736:flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 00:0c:29:e5:2d:6a txqueuelen 1000 (Ethernet)
RX packets 126 bytes 44856 (43.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 333 bytes 62382 (60.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eno33554984:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.249.102 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::20c:29ff:fee5:2d74 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e5:2d:74 txqueuelen 1000 (Ethernet)
RX packets 5594 bytes 640373 (625.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 287 bytes 30429 (29.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 7 bytes 748 (748.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7 bytes 748 (748.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
ifconfig网卡名称 :查看指定网卡的信息 1
2
3
4
5
6
7
8
9
| [iyunv@localhost~]# ifconfig lo
lo:flags=73<UP,LOOPBACK,RUNNING> mtu65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 7 bytes 748 (748.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7 bytes 748 (748.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
ifconfig还可以给出指定的ip地址并激活
第一种方式:#ifconfig IFACEIP/MASK [up]
例如:
1
2
3
4
5
6
7
8
9
10
| [iyunv@localhost~]# ifconfig eno16777736 192.168.10.100/24 up
[iyunv@localhost~]# ifconfig
eno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.100 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fee5:2d6a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e5:2d:6a txqueuelen 1000 (Ethernet)
RX packets 126 bytes 44856 (43.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 341 bytes 63010 (61.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
第二种方式:#ifconfig IFACEIP netmask NETMASK
1
2
3
4
5
6
7
8
9
10
| [iyunv@localhost~]# ifconfig eno16777736 192.168.10.101 netmask 255.255.255.0
[iyunv@localhost~]# ifconfig
eno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.101 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fee5:2d6a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e5:2d:6a txqueuelen 1000 (Ethernet)
RX packets 126 bytes 44856 (43.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 343 bytes 63150 (61.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
可以看到eno16777736的地址已经被更改;但是要注意,ifconfig命令修改的配置重启后会失效;
route命令:用来查看路由的信息
route命令的用法及示例:
查看路由信息:
route -n :表示以数字格式显示路由信息;
1
2
3
4
5
6
| [iyunv@localhost~]# route -n
Kernel IProuting table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.16.0.1 0.0.0.0 UG 100 0 0 eno33554984
172.16.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eno33554984
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eno16777736
|
添加路由条目:
route add [-net | -host] target [netmask Nm] [gw GW] [[dev] If]
例如:添加目标地址为10.0.0.0 ,下一跳网关192.168.10.1
1
2
3
4
5
6
7
8
| [iyunv@localhost~]# route add -net 10.0.0.0/8 gw 192.168.10.1
[iyunv@localhost~]# route -n
Kernel IProuting table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.16.0.1 0.0.0.0 UG 100 0 0 eno33554984
10.0.0.0 192.168.10.1 255.0.0.0 UG 0 0 0 eno16777736
172.16.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eno33554984
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eno16777736
|
删除路由条目:
route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]
例如:把新添加的路由条目删除
1
2
3
4
5
6
7
| [iyunv@localhost~]# route del -net 10.0.0.0/8
[iyunv@localhost~]# route -n
Kernel IProuting table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.16.0.1 0.0.0.0 UG 100 0 0 eno33554984
172.16.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eno33554984
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eno16777736
|
删除默认网关:
#route del default
netstat命令:用于查看网络状态
netstat命令用法及示例:
显示路由表:
1
2
3
4
5
6
| [iyunv@localhost~]# netstat -rn
Kernel IProuting table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 172.16.0.1 0.0.0.0 UG 0 0 0 eno33554984
172.16.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eno33554984
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eno16777736
|
-r:显示内核路由表
-n:以数字格式显示
显示路由连接状态:
选项:
-t:TCP协议的相关连接,连接均有其状态;FSM (Finate StateMachine);
-u:UDP相关的连接;但没有监听状态一说
-w:rawsocket相关的连接
-l:处于监听状态的连接
-a:所有状态
-n:以数字格式显示IP和port
-e:扩展格式
-p:显示相关的进程及PID
以数字格式显示TCP相关的连接:
1
2
3
4
| [iyunv@localhost~]# netstat -tn
ActiveInternet connections (w/o servers)
Proto Recv-QSend-Q Local Address ForeignAddress State
tcp 0 52 172.16.249.102:22 172.16.19.1:51004 ESTABLISHED
|
查看处于监听状态的UDP连接:
1
2
3
4
5
6
7
8
9
10
| [iyunv@localhost~]# netstat -unl
ActiveInternet connections (only servers)
Proto Recv-QSend-Q Local Address ForeignAddress State
udp 0 0 0.0.0.0:68 0.0.0.0:*
udp 0 0 0.0.0.0:123 0.0.0.0:*
udp 0 0 0.0.0.0:29309 0.0.0.0:*
udp 0 0 127.0.0.1:323 0.0.0.0:*
udp6 0 0 :::123 :::*
udp6 0 0 ::1:323 :::*
udp6 0 0 :::23960 :::*
|
以数字格式显示TCP的所有连接:
1
2
3
4
5
6
7
8
| [iyunv@localhost~]# netstat -tan
ActiveInternet connections (servers and established)
Proto Recv-QSend-Q Local Address ForeignAddress State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 52 172.16.249.102:22 172.16.19.1:51004 ESTABLISHED
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
|
显示口的统计数据:
1
2
3
4
5
6
| [iyunv@localhost ~]# netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eno16777 1500 285 0 0 0 343 0 0 0 BMRU
eno33554 1500 8847 0 0 0 749 0 0 0 BMRU
lo 65536 7 0 0 0 7 0 0 0 LRU
|
ifup、ifdown命令:启动或关闭接口
例如:
1
2
| [iyunv@localhost~]# ifdown eno16777736
Device'eno16777736' successfully disconnected.
|
ifdown用法相同。
注意:ifup与ifdown仅就 /etc/sysconfig/network- scripts内的ifcfg-x(x为接口)进行启动或关闭的操作,并不能直接修改网络参数,除非手动调整ifcfg-x文件才行。
IP命令:ip是iproute2软件包里面的一个强大的网络配置工具,它能够替代一些传统的网络管理工具,例如ifconfig、route等,使用权限为超级用户。
ip命令用法及示例:
ip link set设置连接状况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| [iyunv@localhost~]# ip link set eno16777736 down
[iyunv@localhost~]# ifconfig
eno33554984:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.249.184 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::20c:29ff:fe1d:aef1 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1d:ae:f1 txqueuelen 1000 (Ethernet)
RX packets 4601 bytes 366509 (357.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 457 bytes 75326 (73.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:flags=73<UP,LOOPBACK,RUNNING> mtu65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 14 bytes 1496 (1.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 14 bytes 1496 (1.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
ip link show 显示当前所有连接
1
2
3
4
5
6
7
| [iyunv@localhost~]# ip link show
1: lo:<LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00
2: eno16777736:<BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULTqlen 1000
link/ether 00:0c:29:1d:ae:e7 brdff:ff:ff:ff:ff:ff
3:eno33554984: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_faststate UP mode DEFAULT qlen 1000
link/ether 00:0c:29:1d:ae:f1 brd ff:ff:ff:ff:ff:ff
|
ip addr add 添加新地址
例如:添加一个新地址 192.168.10.1
1
2
3
4
5
6
7
8
9
10
11
12
| [iyunv@localhost~]# ip addr add 192.168.10.1/24 dev eno16777736
[iyunv@localhost~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP>mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2:eno16777736: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWNqlen 1000
link/ether 00:0c:29:1d:ae:e7 brdff:ff:ff:ff:ff:ff
inet 192.168.10.1/24 scope global eno16777736
valid_lft forever preferred_lft forever
|
ip addr del 删除地址
例如:删除 192.168.10.1 这个地址
1
2
3
4
5
6
7
8
9
10
| [iyunv@localhost~]# ip addr del 192.168.10.1/24 dev eno16777736
[iyunv@localhost~]# ip addr show
1: lo:<LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2:eno16777736: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWNqlen 1000
link/ether 00:0c:29:1d:ae:e7 brdff:ff:ff:ff:ff:ff
|
ip addr flush 清空设备上的所有地址
ip route add 添加路由
例如:添加目标地址10.10.0.1/8 经过网关为 172.16.0.2 的路由
1
2
3
4
5
6
| [iyunv@localhost~]# ip route add 10.10.0.1 via 172.16.0.2 dev eno16777736
[iyunv@localhost~]# ip route list
default via172.16.0.1 dev eno16777736 protostatic metric 100
default via172.16.0.1 dev eno33554984 protostatic metric 101
10.0.0.0/8dev eno16777736 proto kernel scope link src 10.10.0.1
10.10.0.1via 172.16.0.2 dev eno16777736
|
ip route del 删除路由
例如:上面添加的路由
1
2
3
4
5
6
| [iyunv@localhost~]# ip route delete 10.0.0.0/8
[iyunv@localhost~]# ip route list
default via172.16.0.1 dev eno16777736 protostatic metric 100
default via172.16.0.1 dev eno33554984 protostatic metric 101
172.16.0.0/16dev eno33554984 proto kernel scope link src 172.16.249.184
172.16.0.0/16dev eno16777736 proto kernel scope link src 172.16.249.199 metric 100
|
ip route show:显示所有的路由信息;
ip routeflush:清空所有的路由信息;
ip route get获取单条路由信息
1
2
3
| [iyunv@localhost~]# ip route get 172.16.249.199/16
local172.16.249.199 dev lo src 172.16.249.199
cache <local>
|
ss 命令:与netstat 命令相似
ss [options] [ FILTER ]
选项
-t:TCP协议的相关连接
-u:UDP相关的连接
-w:raw socket相关的连接
-l:监听状态的连接
-a:所有状态的连接
-n:数字格式
-p:相关的程序及其PID
-e:扩展格式信息
-m:内存用量
-o:计时器信息
FILTER := [state TCP-STATE ] [ EXPRESSION ]
例如:显示过滤原端口与目标端口都是22的tcp协议的信息;
1
2
3
4
5
| [iyunv@localhost~]# ss -tan '( dport = :22 or sport = :22 )'
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
ESTAB 0 52 172.16.249.184:22 172.16.19.1:50286
LISTEN 0 128 :::22 :::*
|
通过图形界面修改网络属性
Centos 7上的 nmtui 工具
使用 nmtui 命令
Centos 6 使用setup命令,也可以使用专属命令 system-config-network 来配置。
修改配置文件
通过修改ifcfg-IFACE配置文件参数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| [iyunv@localhost~]# cd /etc/sysconfig/network-scripts/
[iyunv@localhostnetwork-scripts]# ls
ifcfg-eno16777736
root@localhostnetwork-scripts]# vim ifcfg-eno16777736
#Generated by dracut initrd
NAME="eno16777736"
DEVICE="eno16777736"
ONBOOT=yes
NETBOOT=yes
UUID="c5f9072b-3998-4ebd-9287-fbccc011e25a"
IPV6INIT=yes
BOOTPROTO=dhcp
TYPE=Ethernet
|
以上只是我的配置文件中的内容,下面给出完整内容并说明各字段的意义;
DEVICE:此配置文件对应的设备的名称;
ONBOOT:在系统引导过程中,是否激活此接口;
UUID:此设备的惟一标识;
IPv6INIT:是否初始化IPv6;
BOOTPROTO:激活此接口时使用什么协议来配置接口属性,常用的有DHCP、BOOTP、static、none
TYPE:接口类型,常见的有Ethernet, Bridge;
DNS1:第一DNS服务器指向;
DNS2:备用DNS服务器指向
DOMAIN:DNS搜索域;
GATWAY:默认网关
IPADDR:本机的IP地址
NETMASK:子网掩码;centos 7 支持使用PREFIX以长度方式指明子网掩码;
USERCTL:是否允许普通用户控制此设备;
PEERDNS:如果BOOTPROTO的值为“dhcp”,是否允许dhcp server分配的dns服务器指向覆盖本地手动指
定 的DNS服务器指向;默认为允许;
NM_CONTROLLED:是否使用NetworkManager服务来控制接口;
查看服务状态:chkconfig --list
管理网络服务:
1
2
3
4
5
| [iyunv@localhost network-scripts]# service network status
已配置设备:
lo eno16777736
当前活跃设备:
lo eno16777736 eno33554984
|
重启网络服务:
1
2
| [iyunv@localhost network-scripts]# service network restart
Restarting network (via systemctl):
|
修改网络服务:
1、复制想要修改的配置文件;
2、修改配置文件内容保存退出;
3、重启服务使配置生效;
Centos 6 :#servicenetwork restart
Centos 7: #systemctl restart network.service
配置路由
配置文件为/etc/sysconfig/network-scripts/route-IFACE ,配置路由支持两种方式,但不能混用;
第一种方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| [iyunv@localhost network-scripts]# vim
route-eno16777736
10.0.0.0/8 via 172.16.0.1
[iyunv@localhost network-scripts]# systemctl
restart network
[iyunv@localhost network-scripts]# ip route
list
default via 172.16.0.1 dev eno33554984 proto static
metric 100
default via 172.16.0.1 dev eno16777736 proto static
metric 101
10.0.0.0/8 via 172.16.0.1 dev
eno16777736 proto static metric 100
172.16.0.0/16 dev eno33554984 proto kernel
scope link src 172.16.249.184
172.16.0.0/16 dev eno16777736 proto kernel
scope link src 172.16.249.199
|
第二种方式:修改一下内容即可
ADDRESS#=TARGET
NETMASK#=MASK
GATEWAY#-NEXTHOP
给接口配置多个地址
1、使用命令配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| [iyunv@localhostnetwork-scripts]# ifconfig eno16777736:0 192.168.0.1/24
[iyunv@localhostnetwork-scripts]# ifconfig
eno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.249.199 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::20c:29ff:fe1d:aee7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1d:ae:e7 txqueuelen 1000 (Ethernet)
RX packets 34116 bytes 2864726 (2.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 126 bytes 10908 (10.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eno16777736:0:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.1 netmask 255.255.255.0 broadcast 192.168.0.255
ether 00:0c:29:1d:ae:e7 txqueuelen 1000 (Ethernet)
|
2、修改配置文件:
1、复制想要修改的配置文件;
2、修改配置文件内容保存退出;
3、重启服务使配置生效;
|