########1、Linux下的网络配置######
###1、什么是IP ADDRESS
internet protocol ADDRESS ##网络进程地址
ipv4 internet protocol version 4
1.2x32 ##二进制,32 位
ip 是由 32 个 01 组成
11111110.11111110.11111110.11111110 = 254.254.254.254
###2.子网掩码
用来划分网络区域
子网掩码非0的位对应的ip上的数字表示这个ip的网络位
子网掩码0位对应的数字是ip的主机位
网络位表示网络区域
主机位表示网络区域里某台主机
255.255.255.0
^网络位 ^主机位
###3.ip 通信判定
网络位一致,主机位不一致的2个IP可以直接通讯
172.25.254.1/24
24=255.255.255.0
172.25.254.2/24
172.25.0.1/16
###4、网络设定工具
进入/etc/sysconfig/network_scripts
ping ##检测网络是否通畅
ifconfig ##查看或设定网络接口h
ifconfig device(eth0) ip/24 ##设定ip 临时设定,重新启动网络 systemctl restart network,会变成之前的ip;重启电脑;
ifconfig device(eth0) down ##关闭
ifconfig device(eth0) up ##开启
ip addr ##检测或这顶网络接口
ip addr show ##检测
ip addr add ip/24 dev device(eth0) ##设定
ip addr del dev eth0 ip/24 ##删除
##更改ip步骤
ip addr flush eth0 ##清空所有ip
ip addr add ip/24 dev eth0
注意:device 的名字一个物理事实,看到什么命令只能用什么命令;
自动获取ip必须连接网络;
### 5.图形方式设定 ip
1、nm-connection-editor ###nm = NetworkManager,图形方式改了ip重启网络(有可
能)不生效
systemctl stop NetworkManager ##记录网络服务状态
systemctl restart network
systemctl stop NetworkManager
2.nmtui
修改ip后未生效:
当进入nm-connection-editor后,更改了ip,查看ifconfig,ip未生效需要关闭
systemctl stop NetworkManager(网络管理器),再重新启动网络;若再次使用时,打开网络管理器;
为什么修改了ip重新启动后不能生效,但是查看ip addr 会是两个ip,但是nm-connection-editor只有一个,并且配置文件/etc/sysconfig/network_scripts/中只有一个配置文件?
重启电脑;
6.命令方式设定网络
nmcli+空格+连续点击两个TAB键;
nmcli ##NetworkManager 必须开启
nmcli device connection eth0 ##启用eth0网卡
nmcli device disconnection eth0 ##关闭eth0网卡
nmcli device show eth0 ##查看网卡信息
nmcli device status eth0 ##查看网卡服务接口信息
##用 nm-connection-editor 改名称为 westos
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
##对应 nm-connection-editor 来写
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/sysconfig/network_scripts
网络配置文件的命名规则
vim ifcfc-xxx ###进入对应的配置文件
DEVICE=XXXX #物理设备名称
BOOTPROTO=dhcp|none|static #设备工作方式
ONBOOT=yes #网络服务开启时自动激活网卡
IPADDR= #IP地址
PREFFIX=24 #子网掩码
NETMASK=255.255.255.0 #子网掩码
NAME= #接口名称
GATEWAY= #网关;
#nmcli connection delete westos 先删除,再添加(删除的是配置文件)
动态设定
dhcp网络设定
vim /etc/sysconfig/network_scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
systemctl restart network
示例:
静态网络设定文件
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yesIPADDR=172.25.254.100
NETMASK=255.255.255.0
BOOTPROTO=none
NAME=westos ##加与不加在图形化看区别
##不要乱动物理机上 br0
systemctl restart network
一块网卡上配置多个 IP
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
IPADDR0=172.25.254.100
NETMASK0=255.255.255.0
BOOTPROTO=none
NAME=westos
IPADDR1=172.25.0.100
PREFIX1=24
重启网络:systemctl restart network
查看ip:ip addr show eth0