什么是IP ADDRESS
internet protocol ADDRESS ##网络进程地址
ipv4:internet protocol version 4
ip是由32个0,1组成
11111110.11111110.11111110.11111110=254.254.254.254
子网掩码
用来划分网络区域
子网掩码非0的位对应的Ip上的数字表示这个ip的网络位
子网掩码0位对应的数字是ip的主机位
网络位表示网络区域
主机位表示网络区域的某台主机
ip通信判定
网络位一致,主机位不一致的2个IP可以直接通讯
172.25.254.1/24 24=255.255.255.0
172.25.254.2/24
172.25.0.1/16
网络设定工具
1`inconfig命令
ping ##检测网络是否通畅
ifconfig ##查看或设定网络接口
ifconfig device ip/24 ##临时设定,device为网卡名
ifconfig device down ##关闭网卡,device为网卡名
ifconfig device up ##开启网卡,device为网卡名
2`ip addr命令
ip addr ##检测或设定网络接口
ip addr show ##检测
ip addr add ip/24 dev device ##设定,device为网卡名
ip addr del ip/24 dev device ##删除,device为网卡名
##直接更改ip步骤
ip addr flush eth0
ip addr add ip/24 dev device
注意:这里用ifconfig和ip addr命令的都是临时更改
命令方式设定网络
nmcli
##NetworkManager必须开启(systemctl status NetworkManager)
nmcli device connect eth0 ##启动eth0网卡
nmcli device disconnect eth0 ##关闭eth0网卡
nmcli device show eth0 ##查看网卡信息
nmcli device status eth0 ##查看网卡服务接口信息
nmcli connectiong down eth0 关闭eth0网卡
nmcli connectiong up eth0 开启eth0网卡
nmcli connectiong delete eth0 删除eth0网卡
管理网络配置文件
在/etc/sysconfig/network-scripts中更改ifcfg开头的文件
##ifcfg-lo不可更改
新建文件时,文件名必须以ifcfg开头
设置动态ip:
TYPE=Ethernet ##选择网络名称,可省略
NAME=westos ##设置名称,可省略
DEVICE=eth0 ##选择网卡
BOOTPROTO=dhcp|none|static ##设置ip获取方式,动态dhcp,静态none/static
ONBOOT=yes ##自启动
##自己设置ip需要加上以下内容
IPADDR0=172.25.254.101 ##静态时,自己设置ip
NETMASK0=255.255.255.0
(PREFIX1=24) ##设置子网掩码
这里所设置的是静态ip 自己设置的ip
文件编辑成狗后必须重新启动
systemctl restart network
预配置
先保证客户端有一个网卡,服务端有两个网卡
客户端配置网卡ip为1.1.1.101/24
服务端配置网卡一个为ip为1.1.1.201/24 ,另一个为ip为172.25.254.201/24
关闭防火墙systemctl stop firewalld
##路由端##
sysctl -a | grep ip_forward ##查看内核路由沟通功能是否开启,1是开,0是关
vim /etc/sysctl.conf ##更改路由沟通功能状态
在文件最后加net.ipv4.ip_forward = 1
sysctl -p ##使其生效
firewall -cmd --add-masquerade ##开启防火墙伪装功能
##客户端##
vim /etc/sysconfig/network-scripts/ifcfg-eth0 ##在eth0配置文件中添加GATEWAY0=1.1.1.201
systemctl restart network
route -n ##查看网关
ping 172.25.254.2 ##查看正常是否连接
让虚拟机登上百度
更改虚拟机ip为172.25.254.101/24,让其和主机ip有线网段一致
vim /etc/sysconfig/network-scripts/ifcfg-eth0
##在eth0配置文件中添加GATEWAY0=172.25.254.1
systemctl restart network重启
#主机操作,把主机配置成路由器
sysctl -a | grep ip_forward ##查看内核路由沟通功能是否开启,1是开,0是关
vim /etc/sysctl.conf ##更改路由沟通功能状态
在文件最后加net.ipv4.ip_forward = 1
sysctl -p ##使其生效
firewall-cmd --add-masquerade ##开启防火墙伪装功能,真机中该命令和虚拟机不同,firewall和-cmd中没有空格
地址解析:就是让域名指向ip地址
vim /etc/hosts ##本地域名解析文件,优先级默认比resolv.conf高
vim /etc/resolv.conf ##网络解析
vim /etc/nsswitch.conf ##更改优先级,39行,更改files的顺序dns
vim /etc/sysconfig/network-scripts/ifcfg-eth0 ##在网卡配置文件中添加DNS1=114.114.114.114也可以,重启网络服务后,实际上就是更改了vim /etc/resolv.conf
##固定网络解析文件/etc/resolv.conf
适用于动态获取的ip
在vim /etc/sysconfig/network-scripts/ifcfg-eth0中加上参数PEERDNS=no,
再重启网络即可systemctl restart network
ping ip
-c 5 ##次数
-w 5 ##等待秒数