0## Linux上网络管理
一.网络基础知识
1.一台主机如果可以连接公网,比如访问www.baidu.com
那么这台主机必然会有
ipaddress (internet protocol ADDRESS) ##网路进程地址
ipv4 (internet protocol version 4)
ip是由32个0/1组成的
11111110.11111110.11111110.11111110=254.254.254
GATEWAY
dns
2.子网掩码
用来划分网络区域
子网掩码非0的位对应ip上的数字表示这个ip的网络位
网络位表示网络区域
主机位表示网络区域里某台主机
3.ip通信判定
网络位一致,主机位不一致的两个ip可以直接通讯
4.网络的设定工具
ifconfig ###查看或者临时设定主机ip的工具
ifconfig 网络接口 ##查看这个接口上的 ip
ifconfig 网络接口 ip netmask 子网掩码 ####临时设定ip
ifconfig eth0 172.25.254.100 netmask 255.255.255.0
ping ##检测网络是否通畅
ifconfig device ip/24 ##设定ip
ifconfig device down ##关闭
ifconfig device up ##开机
ip addr ##检测这项网络配置
ip addr show 查看设定的网络接口
ip addr add ip/24 dev device ##对端口设置ip
ip addr flush eth0 ##更改ip
ip addr add ip/24 dev device
systemctl restart network ###重新启动网络基本服务
图形配置ip
在shell中输入nm-connection-editor
双击自己设置的网络
点击ipv4 并设定自己的配置
命令设定ip的方式
nmcli
nmcli device
nmcli device connect eth0 ##启用eth0网卡
nmcli device disconnect eth0 ##关闭eth0网卡
nmcli device show eth0 ##查看网卡信息
nmcli device status eth0 ##查看网卡服务接口服务
网络配置文件
/etc/sysconfig/network-scripts/ ###网络配置目录
动态网络配置
vim ifcfg-xxx ###网络配置文件格式
DEVICE=eth0 ##指定文件管理的设备名称
BOOTPROTO=dhcp ##dhcp动态获取,none和static都表示静态网络
ONBOOT=yes ##开机自动激活设备
NAME=westos ##指定网络链接的名字为westos
静态网络配置
vim ifcfg-xxx ###网络配置文件格式
DEVICE=eth0 ##指定文件管理的设备名称
BOOTPROTO=static|none ##dhcp动态获取,none和static都表示静态网络
ONBOOT=yes ##开机自动激活设备
NAME=hello ##指定链接名字
IPADDR=172.25.254.100 ##设定ip为172.25.254.100
NETMASK=255.255.255.0 ##子网掩码为255.255.255.0
PREFIX=24 ##子网掩码为255.255.255.0
网关gateway
网关是一个网络连接到另一个网络的关口,就是路由器上和我们的主机处在同一个网段上的ip
vim /etc/sysconfig/network-scripts/ifcfg-xxx
GATEWAY=网关地址
网关的实验
让虚拟机连接主机上网
将主机变为路由器
sysctl -a | grep ip_forward查看ipv4状态是否为1
如果不为1可以通过vim /etc/sysctl.conf修改文件使其等于1
systemctl status firewalld 查看防火墙的状态,在路由情况下防火墙必须是开启的
shell中输入firewall-cmd --list-all查看masquerad服务是否开启,如果没有开启(显示no)
输入firewall-cmd --permanent --add-masquerade使其开启
如果手动开启需要自己重启服务并查看
firewall-cmd --reload ##重新加载配置
firewall-cmd --list-all ##查看服务是否开启
虚拟机作为客户端的设置
cd /etc/sysconfig/network-scripts/ ##进入网卡配置目录
vim /etc/sysconfig/network ##配置网关
DNS设置
vim /etc/hosts ##本地域名解析配置文件(添加一个才能访问一个)
vim /etc/resolv.conf ##dns域名解析配置文件(临时配置)
vim /etc/sysconfig/network-scripts/ifcfg-xxx ##网卡配置信息中配置(可长久生效)
systemctl restart network ##重启服务
DNS的优先级配置
vim /etc/nsswitch.conf ##解析优先级配置
以本地解析优先
以dns优先