Linux系统网络管理
- 什么是网络?
- 网卡配置文件
- nmcli
网络
- IP地址: 用于标识机器的地址,类似于身份证号码。 公网全球唯一 ipv4 ipv6
- DNS: 域名服务,将网站域名解析到网站的主机。 域名–> ip地址
- Gateway: 网关, 我们的路由器就是网关。
ifconfig : 查看所有的网卡信息
ifconfig ens33: 查看固定网卡的信息
ip addr: 查看网卡信息
网卡配置文件
/etc/sysconfig/network-scripts
ens33 == ifcfg-ens33
ens35 == ifcfg-ens35
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none" ##none static静态 dhcp 动态
DEFROUTE="yes" ## 默认路由
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33" ## 网卡名称
UUID="e20ba92f-11c2-49b4-8542-36b5a3879173" ## 网卡UUID 唯一
DEVICE="ens33"
ONBOOT="yes" ## yes开机自动打开网卡
IPADDR=192.168.1.199 ## IP地址
NETMASK=255.255.255.0 ## 子网掩码
centos7 : systemctl restasrt network
nmcli
nmcli c reload
nmcli c up xxx
实验
添加一块实验网卡
-
修改IP地址为
192.168.1.188 255.255.255.0 192.168.1.1
-
cp ifcfg-ens33 ifcfg-ens37
-
[root@myserver network-scripts]# cat ifcfg-ens37 TYPE="Ethernet" PROXY_METHOD="none" BROWSER_ONLY="no" BOOTPROTO="none" DEFROUTE="yes" IPV4_FAILURE_FATAL="no" IPV6INIT="yes" IPV6_AUTOCONF="yes" IPV6_DEFROUTE="yes" IPV6_FAILURE_FATAL="no" IPV6_ADDR_GEN_MODE="stable-privacy" NAME="ens37" DEVICE="ens37" ONBOOT="yes" IPADDR=192.168.1.188 NETMASK=255.255.255.0 GATEWAY=192.168.1.1
nmcli c reload 重新加载
ifdown ens37 关闭ens37网卡
ifup ens37 开启ens37网卡
[root@myserver network-scripts]# ifdown ens37
Connection 'ens37' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/9)
[root@myserver network-scripts]# ifconfig ens37
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:94:17:3a txqueuelen 1000 (Ethernet)
RX packets 182 bytes 19010 (18.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 286 bytes 31958 (31.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@myserver network-scripts]# ifup ens37
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/10)
[root@myserver network-scripts]# ifconfig ens37
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.188 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::8568:3e1a:817f:59c0 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:94:17:3a txqueuelen 1000 (Ethernet)
RX packets 193 bytes 19790 (19.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 306 bytes 34192 (33.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
nmcli
查看nmcli状态
# nmcli n
启动nmcli
# nmcli n on
激活网卡
# nmcli d c xxx
启动网卡xxx
# nmcli c up xxx
关闭网卡
# nmcli c down xxx
查看网卡xxx信息
# nmcli d show xxx
关闭无线
# nmcli r all off
查看网卡信息
# nmcli connection
# nmcli con xxx
显示具体的网络接口信息
# nmcli connection show xxx
显示所有活动连接
# nmcli connection show --active
删除一个网卡连接
# nmcli connection delete ens37
给ens37添加一个IP(IPADDR)
# nmcli connection modify ens37 ipv4.addresses 192.168.1.189
给xxx添加一个子网掩码(NETMASK)
# nmcli connection modify ens37 ipv4.addresses 192.168.1.189/24
IP获取方式设置成手动(BOOTPROTO=static/none)
# nmcli connection modify ens37 ipv4.method manual
添加一个ipv4
# nmcli connection modify ens37 +ipv4.addresses 192.168.1.189/24
删除一个ipv4
# nmcli connection modify ens37 -ipv4.addresses 192.168.1.189/24
添加一个网关(GATEWAY)
# nmcli connection modify ens37 ipv4.gateway 192.168.0.2
可一块写入:
# nmcli connection modify ens37 ipv4.dns 114.114.114.114 ipv4.gateway 192.168.0.1
添加DNS
# nmcli connection modify ens37 ipv4.dns 114.114.114.114
删除DNS
# nmcli connection modify ens37 -ipv4.dns 114.114.114.114
使用nmcli重新回载网络配置
# nmcli c reload
如果之前没有xxx的connection,则上一步reload后就已经自动生效了
# nmcli c up ens37
[root@myserver ~]# cd /etc/sysconfig/network-scripts/
[root@myserver network-scripts]# ls
ifcfg-ens33 ifcfg-ens37 ifcfg-ens37-1
[root@myserver network-scripts]# mv ifcfg-ens37 ifcfg-ens37~~~~
[root@myserver network-scripts]# ls
ifcfg-ens33 ifcfg-ens37~~~~ ifcfg-ens37-1
[root@myserver network-scripts]# cd
[root@myserver ~]# nmcli d c ens37 激活网卡
[root@myserver ~]# nmcli connection modify ens37 ipv4.addresses 192.168.1.189
[root@myserver ~]# ifconfig ens37
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.188 netmask 255.255.255.255 broadcast 0.0.0.0
inet6 fe80::20c:29ff:fe94:173a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:94:17:3a txqueuelen 1000 (Ethernet)
RX packets 449 bytes 47699 (46.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 530 bytes 59159 (57.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@myserver ~]# nmcli connection reload
[root@myserver ~]# ifconfig ens37
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.188 netmask 255.255.255.255 broadcast 0.0.0.0
inet6 fe80::20c:29ff:fe94:173a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:94:17:3a txqueuelen 1000 (Ethernet)
RX packets 458 bytes 48335 (47.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 530 bytes 59159 (57.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@myserver ~]# nmcli connection up ens37
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/13)
[root@myserver ~]# ifconfig ens37
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.189 netmask 255.255.255.255 broadcast 0.0.0.0
inet6 fe80::20c:29ff:fe94:173a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:94:17:3a txqueuelen 1000 (Ethernet)
RX packets 500 bytes 52058 (50.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 582 bytes 64767 (63.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0