Linux————网络配置

本文详细介绍Linux下网络配置的核心概念,包括IP地址、子网掩码的功能及应用,以及如何通过命令行工具进行网络设定和管理。同时介绍了网络配置文件的编辑方法,帮助读者掌握Linux网络配置的关键技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

########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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值