文章目录
前言
这一节我将会学习Linux下的网络配置,首先我们会学到有关网络配置的一些概念,比如IP、DNS、子网掩码和dhcp服务等等;接下来会学到使用图形和代码的方式来设定IP、DNS;以及设定解析优先级
一、概念
1.什么是IP ADDRESS
internet protocol ADDRESS ##网络进程地址
ipv4 internet protocol version 4
1.2x32
ip是由32个01组成
11111110.11111110.11111110.11111110 = 254.254.254.254
2.子网掩码
用来划分网络区域
子网掩码非0的位对应的ip上的数字表示这个ip的网络位
子网掩码0位对应的数字是ip的主机位
网络位表示网络区域
主机位表示网络区域里某台主机
3.ip通信判定
网络位一致,主机位不一致的2个IP可以直接通讯
172.25.254.1/24 24=255.255.255.0
172.25.254.2/24
172.25.0.1/16
4.网络设定工具
ping ##检测网络是否通畅
ping -c 1 ##ping 1 次
ping -w 1 ##等待1秒
ping -c1 -w1 ip ##ping1次等待1秒
ifconfig ##查看或设定网络接口
ifconfig ##查看
ifconfig device ip/24 ##设定
ifconfig device down ##关闭
ifconfig device up ##开启
ip addr ##检测或这顶网络接口
ip addr show ##检测
ip addr add ip/24 dev device ##设定
ip addr del dev ens160 ip/24 ##删除
“注意: device的名字一个物理事实,看到什么命字只能用什么命字”
1 ping 192.168.0.28 检查网络是否通畅
2 ping www.baidu.com 检查是否可以上网,比如连接百度
3 ping -c 1 192.168.0.28 连接一次
4 ping -w 1 192.168.0.28 等待一秒
5 ping -w 1 -c 1 192.168.0.28
6 ifconfig 查看或设定网络接口(如下)
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:ee:89:31 txqueuelen 1000 (Ethernet)
RX packets 955 bytes 121637 (118.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 118 bytes 8684 (8.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens192: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.28 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::cf2a:694d:6a2e:999 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:ee:89:3b txqueuelen 1000 (Ethernet)
RX packets 870 bytes 102870 (100.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 169 bytes 16074 (15.6 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 1000 (Local Loopback)
RX packets 66 bytes 6200 (6.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 66 bytes 6200 (6.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:d4:4f:19 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
12 ip addr del dev ens192 192.168.0.28/24 删除这块网卡上设定的IP
13 ifconfig 查看是否成功删除
14 nm-connection-editor
15 ifconfig
16 ping 192.168.0.28 再次检测网络,发现网络不通说明删除成功
17 ip addr add 192.168.0.28/24 dev ens192 添加网卡上IP的设定
18 ifconfig
19 ping www.baidu.com
20 ping 192.168.0.28
21 ip addr
22 ping www.baidu.com
23 nm-connection-editor
24 nmcli connection down ens192
25 nmcli connection down Ethernet\ connection\ 1 关闭连接
26 nmcli connection up Ethernet\ connection\ 1 开启连接
27 ping www.baidu.com 网络可以成功连接
28 histyory
29 history
1 ifconfig ens192 192.168.0.28/24 也可以采用ifconfig来设定网卡的IP
2 ifconfig ens192 down
3 ifconfig ens192 up
4 ping 192.168.0.28
5 ping www.baidu.com
6 nmcli connection down E
7 nmcli connection down Ethernet\ connection\ 1 设定完后,需要关闭里连接
8 nmcli connection up Ethernet\ connection\ 1 开启连接来刷新
9 ping www.baidu.com
10 history
5.图形方式设定ip
设定:
1.nm-connection-editor
更改:
不建议用服务控制网络
systemctl restart NetworkManager
建议使用:
nmcli connection show
nmcli connection down westos
nmcli connection up westos
二、nmtui
1.命令方式设定网络
nmcli ##NetworkManager必须开启
nmcli device connect ens160 ##启用ens160网卡
nmcli device disconnect ens160 ##关闭ens160网卡
nmcli device show ens160 ##查看网卡信息
nmcli device status ens160 ##查看网卡服务接口信息
nmcli connection show ##查看连接
nmcli connection down westos ##关闭连接
nmcli connection up westos ##开启连接
nmcli connection delete westos ##删除连接
#添加连接
nmcli connection add type ethernet con-name westos ifname ens160 ip4 172.25.254.100/24
#更改
nmcli connection modify westos ipv4.addresses 172.25.254.200/24
1 nmcli
2 nmcli device disconnect ens192 关闭192网卡
3 nmcli device connect ens192 开启192网卡
4 ping www.baidu.com 检查连接是否成功
5 nmcli device show ens192 查看网卡信息
6 nmcli device status ens192
7 nmcli connection show 查看连接
8 history
10 nmcli connection add type ethernet con-name mou2 ifname ens160 ip4 192.168.0.29/24
11 ping 192.168.0.29 添加连接
12 nm--c
13 nm-connection-editor
14 nmcli connection modify mou2 ipv4.addresses 192.168.0.30/24 更改连接
15 ping 192.168.0.29
16 ping 192.168.0.30
17 nmcli connection down mou2 关闭、开启ens160网卡
18 nmcli connection up mou2 (刷新更改)
19 ping 192.168.0.29
20 ping 192.168.0.30
21 history
2.管理网络配置文件
网络配置目录
/etc/sysconfig/network-scripts/
网络配置文件的名命规则
ifcfg-xxxx
DEVICE=xxx ##设备名称
BOOTPROTO=dhcp|static|none ##设备工作方式
ONBOOT=yes ##网络服务开启时自动激活网卡
IPADDR= ##IP地址
PREFIX=24 ##子网掩码
NETMASK=255.255.255.0 ##子网掩码
NAME= ##接口名称
dhcp网络设定
vim /etc/sysconfig/network-scripts/ifcfg-ens160
DEVICE=ens160
ONBOOT=yes
BOOTPROTO=dhcp
systemctl restart network
示例:
静态网络设定文件
vim /etc/sysconfig/network-scripts/ifcfg-ens160
DEVICE=ens160
ONBOOT=yes
IPADDR=172.25.254.100
NETMASK=255.255.255.0
BOOTPROTO=none
NAME=westos
systemctl restart<C-F9> network
一块网卡上配置多个IP
vim /etc/sysconfig/network-scripts/ifcfg-ens160
DEVICE=ens160
ONBOOT=yes
IPADDR0=172.25.254.100
NETMASK0=255.255.255.0
BOOTPROTO=none
NAME=westos
IPADDR1=172.25.0.100
PREFIX1=24
systemctl restart network
ip addr show ens160
1 vim /etc/sysconfig/network-scripts/ifcfg-ens160
2 vim /etc/sysconfig/network-scripts/ifcfg-mou2 编写网络设定文件
3 nmcli connection reload
4 nmcli connection up mou2
5 ping 192.168.0.28
6 ping 192.168.0.30
7 ping 192.168.0.29
8 vim /etc/sysconfig/network-scripts/ifcfg-mou2
9 nmcli connection reload
10 ping 192.168.0.29
11 ping 192.168.0.30
12 nmcli connection up mou2
13 ping 192.168.0.30
14 history
这是ens160中的网络设定文件
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=192.168.0.29
PREFIX=24
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=mou2
UUID=29086f78-248e-4a2b-a23a-22824d166e3b
DEVICE=ens160
ONBOOT=yes
IPADDR1=192.168.0.30
NETMASK1=255.255.255.0
在这块网卡中,我设定了两个IP,我们可以在文件里边进行相关设定的修改、增加以及删除
3.回环接口
回环接口------人的神经----127.0.0.1-----localhost
4.网关
vim /etc/sysconfig/network ##全局网关,针对所有没有设定网关的网卡生效
GATEWAY=172.25.254.250
vim /etc/sysconfig/network-scripts/ifcfg-ens160
GATEWAY0=172.25.254.20 ##档网卡中设定的IP有多个时,指定对于那个IP生效
GATEWAY=172.25.254.20 ##档网卡中设定的IP只有一个时
route -n ##查看网关
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 "172.25.254.250" 0.0.0.0 UG 1024 0 0 ens160
172.25.0.0 0.0.0.0 255.255.255.0 U 0 0 0 ens160
172.25.254.0 0.0.0.0 255.255.255.0 U 0 0 0 ens160
查看网关
[root@westos_student50 Desktop]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.193 0.0.0.0 UG 100 0 0 ens192
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens192
192.168.0.0 0.0.0.0 255.255.255.0 U 101 0 0 ens160
192.168.0.0 0.0.0.0 255.255.255.0 U 101 0 0 ens160
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
5.设定DNS
#地址解析
#系统操作者对字符敏感
#系统网络通信需要通过ip地址这个数字
#当操作着输入网址www.baidu.com,这个网址不是一个可以通信的IP地址
#于是必须要在系统中把www.baidu.com变成百度服务器的IP地址
#这样的过程叫做地址解析
##
domain name server == 域名解析服务 ##解析就是把域名变成IP
vim /etc/hosts ##本地解析文件,此文件中直接提供地址解析
ip 域名
220.181.111.188 www.baidu.com
vim /etc/resolv.conf ##dns的指向文件 不需要重新启动网络立即生效
nameserver 114.114.114.114 ##当需要某个域名的IP地址时去问114.114.114.114
vim /etc/sysconfig/network-scripts/ifcfg-xxxx
DNS1=114.114.114.114 ##需要重启网络,当网络重新启动
##此参数会修改/etc/resolv.conf
注意:
档网络工作模式为dhcp时
系统会自动获得ip 网关 dns
那么/etc/resolv.conf会被获得到的信息修改
如果不需要获得dns信息
在网卡配置文件中加入
PEERDNS=no
6.设定解析的优先级
系统默认: /etc/hosts > /etc/resolv.conf
vim /etc/nsswitch.conf
39 hosts: files dns ##/etc/hosts优先
vim /etc/nsswitch.conf
39 hosts: dns files ##/etc/resolv.conf dns指向优先
7.dncp服务配置
在主机中使用景象找到dhcp-server的安装包
rpm -ivh dhcp-server
cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf ##用模板生成配置文件
vim /etc/dhcp/dhcpd.conf
#dhcpd.conf
##
#Sample configuration file for ISC dhcpd
#
#option definitions common to all supported networks...
option domain-name "westos.com"; ##域名
option domain-name-servers 114.114.114.114; ##dns
default-lease-time 600; ##默认租约
max-lease-time 7200; ##最长租约
#Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;
#If this DHCP server is the official DHCP server for the local
#network, the authoritative directive should be uncommented.
#authoritative;
#Use this to send dhcp log messages to a different log file (you also
#have to hack syslog.conf to complete the redirection).
log-facility local7;
#No service will be given on this subnet, but declaring it helps the
#DHCP server to understand the network topology.
27 删除
28 删除
#This is a very basic subnet declaration.
subnet 172.25.254.0 netmask 255.255.255.0 { ##子网设定
range 172.25.254.160 172.25.254.200; ##IP地址池
option routers 172.25.254.250; ##网关
}
35行有面全删除
systemctl restart dhcpd
systemctl stop firewalld
测试:
在网络工作模式是dhcp的主机中重启网络
可以看到
ip
GW
dns
全部获取成功
总结
这一节的内容比较多,主要涉及到了Linux下的网络配置,包括其中的一些概念,比如网关、IP、dns。dhcp服务;以及配置网络的两种方法:图形方式和命令方式;还有设定解析的优先级等等