Liunx下的网络配置

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通信判断

网络位一致,主机位不一致的两个IP可以直接通讯
172.25.254.1/24 24=255.255.255.0
172.25.254.2/24
172.25.0.1/16

4.网络设定工具

ping ##检测网络是否通畅
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 flush eth0 ##更改ip步骤

“注意:device的名字是一个物理事实,看到什么用什么”
Liunx下的网络配置

Liunx下的网络配置
Liunx下的网络配置

5.图形方式设定ip

nm-connection-editor
systemctl stop NetworkManager
systemctl restart network
systemctl start NetworkManager

6.命令方式设定网络

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 eth0
nmcli connection up eth0
nmcli connection delete eth0
nmcli connection add type ethernet con-name eth0 ifname ip4 172.25.254.138/24
nmcli connection modify eth0 ipv4.method auto
nmcli connection modify eth0 ipv4.method manual
nmcli connection modify eth0 ipv4.addresses 172.25.254.138/24

添加自定义网络

Liunx下的网络配置
将静态ip改为动态获取ip
Liunx下的网络配置
将动态获取ip改为静态ip

Liunx下的网络配置

7.管理网络配置文件

网络配置目录
/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
Liunx下的网络配置
:wq
systemctl restart network

动态网络设定文件
vim /etc/sysconfig/network-scripts/ifcfg-eth0

Liunx下的网络配置
:wq
systemctl restart network

8.gateway 网关

1.路由器
主要功能是用来作nat的
dnat 目的地地址转换
snat 源地址转换

2.网关
路由器上和自己处在同一个网段的那个ip

3.设定网关
systemctl stop NetwrokManager
vim /etc/sysconfig/network ##全局网关
GATEWAY=网关ip
vim /etc/sysconfig/network-scripts/ifcfg-网卡配置文件 ##网卡接口网关
GATEWAY=网关ip
route -n ##查询网关

4.如何在客户端上访问目标主机
客户端ip:1.1.1.3 目标主机ip:172.25.254.250
将一台主机设定为路由器
在virt-manager上将路由器上添加为两个网卡
将一个网卡设定ip设定和客户端网段一直,另一个和目标主机一致
Liunx下的网络配置
Liunx下的网络配置
Liunx下的网络配置
sysctl -a | grep ip_forward ##查看内网是否畅通
Liunx下的网络配置
systemctl start firewalld ##开启防火墙
firewall-cmd --add-masquerade ##将防火墙伪装
Liunx下的网络配置
在客户端添加网关
Liunx下的网络配置
测试
Liunx下的网络配置

9.dns

1.dns
dns是一台服务器
这太服务器提供了回答客户主机名和ip对应关系的功能

2.设定dns
vim /etc/resolv.conf
nameserver dns服务器ip
Liunx下的网络配置

vim /etc/sysconfig/network-scripts/ifcfg-网卡配置文件
DNS1=dns服务器ip
Liunx下的网络配置
两种方法一样
3.本地解析文件
vim /etc/hosts
ip 主机名称

4.本地解析文件和dns读取的优先级调整
/etc/nsswitch.conf
38 #hosts: db files nisplus nis dns
39 hosts: files dns ##files代表本地解析文件,dns代表dns服务器,那个在前面那个优先

5.dns的锁定
vim /etc/sysconfig/network-scripts/ifcfg-eth0
PEERDNS=no ##网络从起是dns不受网卡默认配置
Liunx下的网络配置
Liunx下的网络配置

END