Linux下的网络配置

Linux下的网络配置

1.什么是IP ADDRESS
internet protocol ADDRESS 网络进程地址
ipv4 internet protocol version 4
ip是由32个01组成
11111110.11111110.1111110.11111110=254.254.254.254

2.子网掩码
用来划分网络区域
子网掩码非0的位对应的ip上的数字表示这个ip的网络位
子网掩码0位对应的数字是ip的主机位
网络位表示网络区域
主机位表示网络区域里某台主机
3.ip通信判定
网络位一致,主机位不一致的两个ip可以i直接通讯
172.25.254.1/24 24=255.255.255.0
172.25.254.2/24
4.网络设定工具

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 del  dev eth0 ip/24 删除(此删除只是删除一个ip)
更改ip步骤
ip addr flush eth0  先删除所有ip
ip addr add ip/24 dev device 再设定

Linux下的网络配置
Linux下的网络配置
Linux下的网络配置

5.图形方式设定ip

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

6.命令方式设定网络
nmcil NetworkManager必须开启

nmcil device connect eth0 启用eth0网卡
Linux下的网络配置

nmcil device disconnect eth0  关闭eth0网卡
nmcil device show eth0  查看网卡信息
nmcil device status eth0 查看网卡服务接口信息

Linux下的网络配置

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.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.200/24

7.管理网络配置文件

网络配置目录
/etc/sysfig/network-scripts/
网络配置文件的命名规则
ifcfg-xxxx 
DEVICE=XXX     设备名称
ONBOOT=yes     网络服务开启时自动**网卡
BOOTPROTO=dhcp|static|none 设备工作方式
IPADDR=        IP地址
PREFIX=24      子网掩码
NETMASK=255.255.255.0  	子网掩码
NAME=          接口名称


dhcp网络设定(自动生成ip)
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp

示例:
静态网络设定文件
vim /etc/sysconfig/network-scripts/ifcfg-eth0
Linux下的网络配置

DEVICE=eth0
ONBOOT=yes
IPADDR=172.25.254.100
NETMASK=255.255.255.0
BOOTPROTO=none
NAME=westos

Linux下的网络配置

systemctl restart network
Linux下的网络配置

实现路由器功能(双网卡主机)

sysctl -a | grep ip_forward  (查看net _ipv4.ip_forward)
vim /etc/sysctl.conf (在里边将net _ipv4.ip_forward的值改为1)
sysctl p (使上述操作生效)
systemctl start firewalld (开启火墙)
firewall-cmd --list-all (查看火墙列表)
firewall-cmd --permanent --add-masquerade (开启火墙伪装功能--地址转换)
firewall-cmd --reload (重新加载,使生效)
firewall-cmd --list-all (查看火墙列表,当masquerade=1时,即成功)