linux怎么修改本地的ip
linux怎么修改本地的ip详细介绍
在 Linux 系统中,修改本地 IP 地址的方法会因系统使用的网络管理工具和版本而有所不同。下面为你介绍几种常见的修改方式。
1. 使用
nmcli(NetworkManager 命令行工具)
nmcli是 NetworkManager 的命令行接口,很多现代的 Linux 发行版都默认使用 NetworkManager 管理网络。
查看网络连接
bash
nmcli connection show
修改 IP 地址
假设要修改的连接名称为eth0,将其 IP 地址设置为192.168.1.100,子网掩码为255.255.255.0,网关为192.168.1.1,DNS 为8.8.8.8和8.8.4.4,可以使用以下命令:
bash
nmcli connection modify eth0 ipv4.addresses .1.100/24nmcli connection modify eth0 ipv4.gateway .1.1
nmcli connection modify eth0 ipv4.dns
nmcli connection modify eth0 ipv4.method manual
重启网络连接
bash
nmcli connection down eth0nmcli connection up eth0
2. 编辑网络配置文件(以 Ubuntu 为例)
编辑配置文件
Ubuntu 18.04 及以后的版本使用 Netplan 管理网络,配置文件通常位于/etc/netplan目录下,文件扩展名一般为.yaml。
使用文本编辑器打开配置文件,例如:
bash
/etc/netplan/01-netcfg.yaml
修改配置文件内容如下:
yaml
networkd
no
192.168.1.100/24
192.168.1.1
8.8.8.8 8.8.4.4
应用配置
bash
netplan apply
3. 编辑网络配置文件(以 CentOS 为例)
编辑配置文件
CentOS 系统的网络配置文件通常位于/etc/sysconfig/network-scripts目录下,文件名以ifcfg-开头,例如ifcfg-eth0。
使用文本编辑器打开配置文件,例如:
bash
/etc/sysconfig/network-scripts/ifcfg-eth0
修改配置文件内容如下:
plaintext
TYPE=EthernetBOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
重启网络服务
bash
systemctl restart network
912 systemctl network
以上就是在 Linux 系统中修改本地 IP 地址的常见方法,你可以根据自己的系统和需求选择合适的方式。