linux下的网络配置
学习目标
- 了解 IPV4 网络的定义
- IPADDRESS 的设定
- GATEWAY 网关的设定
- dns 的设定
#1.什么是IP address
interner protocol address ##网络协议地址
ipv4 internet protocol version 4 ##IPV4协议
2*32位
ip是由32个01组成
11111110.11111110.11111110.11111110 = 254.254.254.254
#2.子网掩码netmask
用来划分网络区域
子网掩码非0的位对应的ip上的数字表示这个ip的网络位
子网掩码0位对应的数字是ip的主机位
网络位表示网络区域
主机位表示网络区域内的某台主机
#3.ip通信判定
网络位一致,主机位不一致的2个ip可以直接通信
172.25.254.1/24 24=255.255.255.0(前三位 8*1*3 八个1)
172.25.254.2/24
172.25.0.1/16(大区域可以找到小区域172.25.254.2/24 可能会混乱)
#4.网络设定工具
ping ##检测网络是否通畅
ifconfig ##查看或设定网络接口
ifconfig device ip/24 ##设定ip(device是设备名称)
ifconfig device down ##关闭接口
ifconfig device up ##开启接口
ip addr show #查看ip(推荐)
#5.图形方式设定ip
nm-connection-editor
或者
nmtui
#6.命令方式
device 物理设备 connection 逻辑设计连接
nmcli ##NetworkManager 必须开启
nmcli device show eth0 ##查看网卡信息(device后面一定是设备名称)
nmcli device status eth0 ##查看网卡服务接口信息
nmcli device connect eth0 ##启用eth0网卡
nmcli device disconnect eth0 ##断开eth0网卡
nmcli connection show ##显示连接网络的信息
nmcli connection down westos ##关闭westos网络连接
nmcli connection up westos ##开启westos网络连接
nmcli connection delete westos ##删除westos网络连接
nmcli connection add type ethernet con-name westos ifname eth0 ip4 172.25.254.100/24 ##设置网络连接:ethernet 名字:westos 网卡:eth0 IP:172.25.254.100/24
nmcli connection modify westos ipv4.method auto ##修改连接方式自动
nmcli connection modify westos ipv4.method manual #修改模式手动
nmcli connection modify westos ipv4.addresses 172.25.254.150/24 #修改ip
##7.管理网络配置文件##
DECICE 物理网卡
PREFIX24与NETWORK255.255.255.0一致
网络配置目录
/etc/sysconfig/network-scripts/
网络配置文件命令规则
vim ifcfg-xxx
DEVICE=XXX ##设备名称
ONBOOT=yes ##网络服务开启时自动激活网卡
BOOTPROTO=dhcp|static|none ##设备工作方式
IPADDR= ##ip地址
PREFIX=24 ##子网掩码
NETMASK=255.255.255.0 ##子网掩码
NAME= ##接口名称(可有可无)
(示例:)
静态网络配置文件:
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.100
PREFIX=24
NAME=westos
systemctl restart network
一块网卡上配置多个IP:
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR0=172.25.254.100
PREFIX=24
NAME=westos
IPADR1=172.25.0.100
NATMASK1=255.255.255.0
systemctl restart network
ip addr show eth0
##8.lo回环接口##
##9.网关##
1.把真实主机变成路由器
firewall-cmd --list-all
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
firewall-cmd --list-all
masquerade:yes <---表示地址伪装功能开启,物理机变成路由器。
2.设定虚拟机网关
vim /etc/sysconfig/network ##全局网关,针对所有没有设定网关的网卡生效
NETWORKING=yes
NOZEROCONF=yes
vim /etc/sysconfig/network-scrispts/ifcfg-eth0
GATEWAY0=172.254.65 ##当网卡中设定的IP有多个时,指定对于哪个IP生效
GATRWAY=172.25.254.65##当网卡中只设定了一个IP时(物理机IP)
route -n
firewall-cmd --list-all
[root@server network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.25.254.65 0.0.0.0 UG 1024 0 0 eth0
172.25.3.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
172.25.254.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
##10.设定dns##
dns = domain name system
vim /etc/hosts ##本地解析文件
ip 域名
61.135.169.125 www.baidu.com
vim /etc/resolv.conf ##dns指向文件
nameserver 114.114.114.114 ##当需要某个域名的IP地址时去问114.114.114.114
vim /etc/sysconfig/network-scripts/ifcfg-xxx
DNS1=114.114.114.114
注意:
当网络工作模式为dhcp时,系统会自动获得ip网关dns,/etc/resolv.conf 会被获得到的信息修改,如果不需要获得dns信息,在网卡配置文件中加入PEERDNS=no,如果需要则在后面加上DNS1=114.114.114.114
#这个实验需要yum install dhcp,首先需要先配置yum源,可以在系统安装镜像。注意删除干净之前 /etc/yum.repos.d/ 里面有关配置yum的内容。此处不讲解如何配置yum 见下一篇博客#
在/etc/resolv.conf文件中指定的DNS是临时的,将网卡的设备文件中的BOOTPROTO改为dhcp后,经过systemctl restart network后会将/etc/resolv.conf文件文件中的内容全部注释;
将ip获取方式改为dhcp后 ;我们可以看到此时desktop获取到的ip为1.1.1.151:
此时,查看/etc/resolv.conf文件,我们会发现此文件中会自动添加一些项目,并且原文件中的内容也会被覆盖,这是因为在dhcp方式下,网卡设备文件中的参数PEERDNS默认为yes
在上面我们就可以看到,将IP的获取方式改为DHCP后,/etc/resolv.conf文件中DNS的指向就会自动被修改,因此若想自动获取DNS,则将ip获取方式改为dhcp即可:
vim /etc/sysconfig/network-scripts/ifcfg-et0(网卡的配置文件)
BOOTPROTO=dhcp
如何让自己设定DNS不受dhcp的控制:
vim /etc/sysconfig/network-scripts/ifcfg-eth0
PEERDNS=no ##是否指定DNS当主机设置获取IP为DHCP方式时,此项参数默认为yes,它会自动修改/etc/resolv.conf文件
##当参数设置为no,在dhcp方式下,系统不会修改/etc/resolv.conf文件
重启网络,此时我们可以看到/etc/resolv.conf文件内容被重置:
再vim /etc/sysconfig/network-scripts/ifcfg-eth0 添加DNS1:
此时再重启网络,我们可以看到,在dhcp服务仍开启的情况下文件内容仍不会被更改:
##11.设定解析的优先级##
系统默认:
/etc/hosts > /etc/resolv.conf
vim /etc/nssswithch.conf
39 host: file dns ##/etc/hosts优先
vim /etc/nssswithch.conf
39 host: dns file ##/etc/resolv.conf dns优先
本人将baidu的IP hosts文件设为:119.75.213.61
ping www.baidu.com可以看见百度的IP为119.75.213.61,当我们把host文件中baidu IP删除可以发现ping www.baidu.com时 IP变化此时DNS获取的百度IP,这就是优先级的作用。
##12.dhcp服务配置##
在服务端:
yum install dhcp
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 172.25.254.250;
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.
# This is a very basic subnet declaration.
subnet 172.25.254.0 netmask 255.255.255.0 {
range 172.25.254.150 172.25.254.200;
option routers 172.25.254.250;
}
systemctl start dhcpd
可以在服务端和客户端看到ip分配的记录:
服务端:/var/lib/dhcpd/dhcpd.leases
客户端:/var/log/messages
测试:
在网络工作模式是dhcp的主机中重启网络,可以看到ip 网关dns 全部获取成功。
配置文件总共改三处:
1.改7-8行
2.30-33行 写一个分配ip的范围
3.记得将34行后,以及30行前没用的代码删除