inux下的网络配置
什么是IP ADDRESS
internet protocol ADDRESS ##网络进程地质
ipv4 internet protocol version 4
1.2x32
ip是由32个01组成
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
网络设定工具
ping ##检测网络是否通畅
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 flush eth0 ##清空ip
图形方式设定ip
nm-connection-editor
命令方式设定网络
nmcli ##NetworkManager必须开启
nmcli device connect eth0 ##启用eth0网卡
nmcli device disconnect eth0 ##关闭eth0网卡
nmcli device show eth0 ##查看网卡信息
nmcli device status eth0 ##查看网卡服务接口信息
nmcli connection show
nmcli connection down westos
nmcli connection up westos
nmcli connection delete westos
nmcli connection add type ethernet con-name westos ifname eth0 ip4 172.25.254.160/24
nmcli connection modify westos ipv4.method auto
nmcli connection modify westos ipv4.addresses 172.25.254.161/24
nmcli connection modify westos ipv4.method manual
管理网络配置文件
网络配置目录
/etc/sysconfig/network-scripts/
网络配置文件的命名规则
ifcfg-xxxx
DEVICE=xxx ##设备名称
BOOTPROTO=dhcp|static|none ##设备工作方式
ONBOOT=yes ##网络服务开启时自动激活网卡
IPADDR= ##IP地址
PREFIX=24 ##子网掩码
NETMASK=255.255.255.0 ##子网掩码
NAME= ##接口名称
示例:
静态网络设定文件
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=ethernet
ONBOOT=yes
IPADDR0=172.25.254.160
PREFIX0=24
BOOTPROTO=none
GATEWAY0=172.25.254.60
systemctl restart network
lo回环接口
回环接口----人的神经----127.0.0.1----localhost
将server主机设置成路由
目的:使desktop:1.1.1.160 连通 真实主机:172.25.254.60
server主机:
设置server双网卡
eth0:172.25.254.80
eth1:1.1.1.80
设置后重启网络服务
sysctl -a | grep ip_forward ##查看server主机内双网卡能否互联 0为不能 1为能
若不能:
vim /etc/sysctl.conf
在文件中添加
net.ipv4.ip_forward=1
添加后重启网络服务
systemctl start firewalld ##开启防火墙
firewall-cmd --add-masquerade ##设置防火墙地址伪装功能
client主机:
设置网关:
vim /etc/sysconfig/network ##设置全局网关
在文件中添加
GATEWAY=1.1.1.80
添加后重启网络服务
ping 172.25.254.60 ##连接主机IP 连通则设置路由成功
设置网关![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7ae98c2e85ceaa14a4380f25eea410d3.png)
vim /etc/sysconfig/network ##设置全局网关
在文件中加入
GATEWAY=172.25.254.60
vim /etc/sysconfig/network-scripts/ifcfg-xxx ##设置所选网卡
在文件中加入
GATEWAY0=172.25.254.60
route -n ##查看网关
设置DNS
vim /etc/hosts ##本地解析域名
ip 域名
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 ##DNS后必须有数字
注意:
当网络工作模式位dhcp时
系统会自动获得ip 网关 dns
那么/etc/resolv.conf会被获得到的信息修改
如果不需要获得dns信息
在网卡配置文件中加入
PEERDNS=no
设置解析的优先级
系统默认:
/etc/hosts > /etc/resolv.conf
修改优先级:
vim /etc/nsswitch.conf
39 hosts: files dns ## files表示hosts dns 表示resolv.conf 哪个在前面哪个优先