网络管理与配置
网络管理可分为通过命令行命令实现和配置文件这两种方式来实现。但是大多数命令行命令配置的网络都是临时的,却是立即有效的,而通过配置文件配置的网络是永久有效的(当然意外事故就拉倒吧),却不能立即有效,这是因为网络管理是功能是由内核提供的,通过命令配置的网络能够直接被内核接受到,而我们知道在Linux中一切皆文件,网络也不外如是,只有写入与网络配置有关的文件中才能永久有效,但是这些配置文件却不能立即被内核识别,所以不会立即生效,那就得让内核重读配置文件才行。
下面先说说命令行命令配置网络。
ifcfg家族:
ifconfig:用来显示和配置网络接口卡
ifconfig或者ifconfig -a是显示所有网络接口信息的,不同的地方在于ifconfig只能显示已经开启的网卡信息,而ifconfig -a能真正显示所有包括开启的和没有开启的网卡信息
ifconfig 网卡名:仅显示指定的网络接口卡的信息
ifconfig 网卡名 ip地址/掩码(这是最简单的方式)
[root@localhost ~]# ifconfig eth2eth2: flags=4163mtu 1500 inet 172.168.0.1 netmask 255.255.0.0 broadcast 172.168.255.255 ether 00:0c:29:f8:b5:f8 txqueuelen 1000 (Ethernet) RX packets 25 bytes 3168 (3.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 10 bytes 1308 (1.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@localhost ~]#
ifconfig 网卡名 down或者up:关闭指定网卡;开启指定网卡
[root@localhost ~]# ifconfig eth2 down[root@localhost ~]# ifconfig eth2eth2: flags=4098mtu 1500 inet 172.168.0.1 netmask 255.255.0.0 broadcast 172.168.255.255 ether 00:0c:29:f8:b5:f8 txqueuelen 1000 (Ethernet) RX packets 28 bytes 3348 (3.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 10 bytes 1308 (1.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@localhost ~]# 虽然还显示有地址,但是已经没了UP这个标志了
ifconfig 网卡名 [-]特性:开启和关闭一些特性,如下开启和关闭混杂模式
[root@localhost ~]# ifconfig eth2 promisc[root@localhost ~]# ifconfig eth2eth2: flags=4354mtu 1500 ether 00:0c:29:f8:b5:f8 txqueuelen 1000 (Ethernet) RX packets 28 bytes 3348 (3.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 10 bytes 1308 (1.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@localhost ~]# ifconfig eth2 -promisc[root@localhost ~]# ifconfig eth2eth2: flags=4098 mtu 1500 ether 00:0c:29:f8:b5:f8 txqueuelen 1000 (Ethernet) RX packets 28 bytes 3348 (3.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 10 bytes 1308 (1.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@localhost ~]#
route:显示和配置路由的
route -n:不以反解的方式显示路由表(建议这样)
[root@localhost ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eno16777728192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777728[root@localhost ~]# 其中Destination为0.0.0.0的表示本机默认路由,而Gateway为0.0.0.0的目的网络就是本机所在的网络,因此不需要任何路由。
route add <-net|-host> <网络好|主机>/掩码 gw 吓一跳ip [dev] [网卡]
[root@localhost ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eno167777280.0.0.0 192.168.1.1 0.0.0.0 UG 101 0 0 eth2192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777728192.168.1.0 0.0.0.0 255.255.255.0 U 101 0 0 eth2[root@localhost ~]# route add -net 172.16.0.0/16 gw 192.168.1.1[root@localhost ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eno167777280.0.0.0 192.168.1.1 0.0.0.0 UG 101 0 0 eth2172.16.0.0 192.168.1.1 255.255.0.0 UG 0 0 0 eno16777728192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777728192.168.1.0 0.0.0.0 255.255.255.0 U 101 0 0 eth2[root@localhost ~]# 需要注意的是下一跳地址(网关)必须在本机任一网卡的网络中,因为如果连网关都不在本机网络中那么何谈路由?那么就可以通过Gateway为0.0.0.0就可以知道本机任一网卡的网络了。
route add default dev 网卡:添加默认路由
[root@localhost ~]# route add default gw 172.16.0.1 dev eth2[root@localhost ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 172.16.0.1 0.0.0.0 UG 0 0 0 eth20.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 eth20.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eno167777280.0.0.0 192.168.1.1 0.0.0.0 UG 101 0 0 eth2172.16.0.0 192.168.1.1 255.255.0.0 UG 0 0 0 eno16777728192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777728192.168.1.0 0.0.0.0 255.255.255.0 U 101 0 0 eth2
route del -net 路由/掩码 dev 网卡
[root@localhost ~]# route del -net 0.0.0.0/0 dev eth2[root@localhost ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eno167777280.0.0.0 192.168.1.1 0.0.0.0 UG 101 0 0 eth2172.16.0.0 192.168.1.1 255.255.0.0 UG 0 0 0 eno16777728192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777728192.168.1.0 0.0.0.0 255.255.255.0 U 101 0 0 eth2[root@localhost ~]#
netstat:查看网络状态信息
常用选项有:
-t:显示tcp传输协议的
-u:显示udp传输协议的
-n:不以反解的方式显示(也就是数字格式)
-l:处于监听状态的
-p:显示相关进程和pid
-e:扩展的格式显示
-a:显示所有状态
[root@localhost ~]# netstat -tunlpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1324/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1425/master tcp6 0 0 :::22 :::* LISTEN 1324/sshd tcp6 0 0 ::1:25 :::* LISTEN 1425/master 省略。。。。其他方式还请自行组合:比如tan,uan,tnl,unl
ifup和ifdown命令是通过读取/etc/sysconfig/network-scripts/ifcfg-网卡名这个文件来开启关闭网卡的,如果没有就会报错
iproute家族:
ip link:显示和管理网卡信息
ip link show(list):显示网卡信息
[root@localhost ~]# ip link show1: lo:mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:002: eno16777728: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:f8:b5:ee brd ff:ff:ff:ff:ff:ff3: eth2: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:f8:b5:f8 brd ff:ff:ff:ff:ff:ff
ip link set:
ip link set [dev] 网卡 up(down):开启(关闭)网卡
[root@localhost ~]# ip link set eth2 down[root@localhost ~]# ip link show1: lo:mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:002: eno16777728: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:f8:b5:ee brd ff:ff:ff:ff:ff:ff3: eth2: mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 1000 link/ether 00:0c:29:f8:b5:f8 brd ff:ff:ff:ff:ff:ff[root@localhost ~]#
ip link set [dev] 网卡 mtu #:设置mtu大小的
ip link set [dev] 网络 name NAME:重命名
[root@localhost ~]# ip link set eth2 down[root@localhost ~]# ip link set eth2 name eth0[root@localhost ~]# ip link show1: lo:mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:002: eno16777728: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:f8:b5:ee brd ff:ff:ff:ff:ff:ff3: eth0: mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 1000 link/ether 00:0c:29:f8:b5:f8 brd ff:ff:ff:ff:ff:ff[root@localhost ~]#
ip link set [dev] 网卡 multicast on(off):开启(关闭)多播
ip netns:管理网络名称空间
ip netns add NAME:添加网络空间
ip netns list:列出网络空间名
ip netns del NAME:删除网络空间
ip netns exec NAME COMMAND:在指定的net空间中执行命令
[root@localhost ~]# ip link set eth2 down[root@localhost ~]# ip link set eth2 name eth0[root@localhost ~]# ip link show1: lo:mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:002: eno16777728: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:f8:b5:ee brd ff:ff:ff:ff:ff:ff3: eth0: mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 1000 link/ether 00:0c:29:f8:b5:f8 brd ff:ff:ff:ff:ff:ff[root@localhost ~]# ip netns add mynet[root@localhost ~]# ip netns list mynet[root@localhost ~]# ip link set eth0 netns mynet[root@localhost ~]# ip link show1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:002: eno16777728: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:f8:b5:ee brd ff:ff:ff:ff:ff:ff[root@localhost ~]# 放到空间中的网卡不能被直接显示[root@localhost ~]# ip netns exec mynet ip link show1: lo: mtu 65536 qdisc noop state DOWN mode DEFAULT link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:003: eth0: mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000 link/ether 00:0c:29:f8:b5:f8 brd ff:ff:ff:ff:ff:ff[root@localhost ~]# 只有这样才能显示出来,但是请看下面[root@localhost ~]# ip netns del mynet[root@localhost ~]# ip netns list[root@localhost ~]# ip link show1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:002: eno16777728: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:f8:b5:ee brd ff:ff:ff:ff:ff:ff3: eno33554968: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:f8:b5:f8 brd ff:ff:ff:ff:ff:ff[root@localhost ~]# 由于为了演示方面我早些改名了第二张网卡名,放到网络空间再出来后就还原了
ip addr;显示和设置网络地址
ip addr add ip/掩码 dev 网卡 这里的dev不能省
ip addr del ip/掩码 dev 网卡
[root@localhost ~]# ip addr add 10.0.0.1/8 dev eth2[root@localhost ~]# ifconfig eth2eth2: flags=4163mtu 1500 inet 10.0.0.1 netmask 255.0.0.0 broadcast 0.0.0.0 ether 00:0c:29:f8:b5:f8 txqueuelen 1000 (Ethernet) RX packets 197 bytes 18250 (17.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 48 bytes 5004 (4.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@localhost ~]#
ip addr可以在一个网卡上添加多个地址,但是只有在同一个网段上的才有主次之分。
[root@localhost ~]# ip addr add 172.16.0.2/16 dev eth2[root@localhost ~]# ip addr show 3: eth2:mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:f8:b5:f8 brd ff:ff:ff:ff:ff:ff inet 10.0.0.1/8 scope global eth2 valid_lft forever preferred_lft forever inet 172.16.0.2/16 scope global eth2 valid_lft forever preferred_lft forever inet 10.0.0.2/8 scope global secondary eth2 valid_lft forever preferred_lft forever[root@localhost ~]# 而且ifconfig只会显示源地址[root@localhost ~]# ifconfig eth2eth2: flags=4163 mtu 1500 inet 10.0.0.1 netmask 255.0.0.0 broadcast 0.0.0.0 ether 00:0c:29:f8:b5:f8 txqueuelen 1000 (Ethernet) RX packets 211 bytes 19090 (18.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 48 bytes 5004 (4.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
要想ifconfig能显示只能加别名
[root@localhost ~]# ip addr add 192.19.0.1/24 dev eth2 lable eth2:0Error: either "local" is duplicate, or "lable" is a garbage.[root@localhost ~]# ip addr add 172.16.0.3/16 dev eth2 lable eth2:0Error: either "local" is duplicate, or "lable" is a garbage.[root@localhost ~]# ip addr add 172.16.0.3/16 dev eth2 label eth2:0[root@localhost ~]# ifconfig eth2:0;ip addr show eth2eth2:0: flags=4163mtu 1500 inet 172.16.0.3 netmask 255.255.0.0 broadcast 0.0.0.0 ether 00:0c:29:f8:b5:f8 txqueuelen 1000 (Ethernet)3: eth2: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:f8:b5:f8 brd ff:ff:ff:ff:ff:ff inet 10.0.0.1/8 scope global eth2 valid_lft forever preferred_lft forever inet 172.16.0.2/16 scope global eth2 valid_lft forever preferred_lft forever inet 10.0.0.2/8 scope global secondary eth2 valid_lft forever preferred_lft forever inet 172.16.0.3/16 scope global secondary eth2:0 valid_lft forever preferred_lft forever[root@localhost ~]# 就是在添加地址的时候加上label 网卡:数字(0-9)
ip addr flush dev 网卡:清空所有指定网卡的ip地址
[root@localhost ~]# ip addr flush dev eth2[root@localhost ~]# ip addr show eth23: eth2:mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:f8:b5:f8 brd ff:ff:ff:ff:ff:ff[root@localhost ~]#
ip route:管理路由
ip route show(list):显示路由信息
ip route add 网络/掩码 via 下一跳地址 [dev] 网卡 [src 源ip地址]
[root@localhost ~]# ip addr add 172.16.0.1/16 dev eth2[root@localhost ~]# ip addr add 172.16.0.2/16 dev eth2[root@localhost ~]# ip addr list eth23: eth2:mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:f8:b5:f8 brd ff:ff:ff:ff:ff:ff inet 172.16.0.1/16 scope global eth2 valid_lft forever preferred_lft forever inet 172.16.0.2/16 scope global secondary eth2 valid_lft forever preferred_lft forever[root@localhost ~]# ip route showdefault via 192.168.1.1 dev eno16777728 proto static metric 100 172.16.0.0/16 via 192.168.1.1 dev eno16777728 172.16.0.0/16 dev eth2 proto kernel scope link src 172.16.0.1 192.168.1.0/24 dev eno16777728 proto kernel scope link src 192.168.1.107 metric 100 [root@localhost ~]# ip route add 10.0.0.0/8 via 172.16.10.1 dev eth2 src 172.16.0.1[root@localhost ~]# ip route showdefault via 192.168.1.1 dev eno16777728 proto static metric 100 10.0.0.0/8 via 172.16.10.1 dev eth2 src 172.16.0.1 172.16.0.0/16 via 192.168.1.1 dev eno16777728 172.16.0.0/16 dev eth2 proto kernel scope link src 172.16.0.1 192.168.1.0/24 dev eno16777728 proto kernel scope link src 192.168.1.107 metric 100 [root@localhost ~]# 当网卡地址较多时可指定源地址
ip route delete 网络/掩码:删除指定路由
[root@localhost ~]# ip route del 10.0.0.0/8[root@localhost ~]# ip route showdefault via 192.168.1.1 dev eno16777728 proto static metric 100 172.16.0.0/16 via 192.168.1.1 dev eno16777728 172.16.0.0/16 dev eth2 proto kernel scope link src 172.16.0.1 192.168.1.0/24 dev eno16777728 proto kernel scope link src 192.168.1.107 metric 100 [root@localhost ~]#
ip route flush 网络/掩码:清除指定的网络所有路由
ss命令:与netstat很相似,但是更加强大
除了-t,-u,-a,-l,-p,-n,-e,还有
-m:显示内存用量
-o:显示计时器信息
还能显示执行的连接状态
state:
LISTEN
ESTABLISHMENT
FIN_WAIT1:
FIN_WAIT2
SYN_SENT
SYN_RECV
CLOSED
port:
dport
sport
配置文件配置网络:
/etc/sysconfig/network-scripts/ifcfg-网卡名:这个文件可以配置大量的网络接口信息。
DEVICE=指定设备要与网卡名一样
ONBOOT:[yes|no];是否开机启动
IPV6INIT:[yes|no]:是否初始化IPV6
BOOTPROTO:四种(dhcp,bootp,static,none)
UUID:此设备的UUID号
TYPE:网络类型(比如Ehernet,Bridge)
IPADDR:ip地址
DNS1:第一dns
DNS2:备用dns
NETMASK(PREFIX);掩码
GATEWAY:默认网关
USERCTL:是否允许普通用户控制此设备
PEERDNS:当BOOTPROTO为dhcp时候是否允许dhcp服务器分配的dns覆盖手动指定的dns默认是允许
NM_CONTROLLED:是否开启NetworkManager来管理网络
HWADDR:MAC地址
如果要指定除了默认路由的其他路由在/etc/sysconfig/network-scripts/route-网卡名这个文件中配置
有两种方式,这里我只写最简单的方式
网络/掩码长度 via ip
一行一个路由
DNS配置文件在:/etc/resolv.conf:
nameserver:DNS
一行一个
在/etc/hosts设置优先DNS映射
ip 域名 别名1 别名2.。。
主机名设定:
hostname:显示主机名
hostname HOSTNAME:设定主机名,但是重启就会失效
hostnamectl set-hostname HOSTNAME:永久有效
/etc/sysconfig/network配置文件:
hostname=HOSTNAME
注意:配置文件配置的必须重读配置文件才能有效
还有一些工具:setup,nmtui图形界面设置网络,也是永久有效的,打开就会!!
还有nmcli和ifconfig很相似。这里就不一一演示了