CentOS、Ubuntu配置网卡IP

自定义网卡名称

CentOS6及以前网络接口使用连续号码命名,如eth0、eth1、eth2等。当增加或删除网卡时,名称可能发送变化
CentOS7及以上版本使用基于硬件设备拓扑和设备类型命名,可以保持网卡名称的稳定
CentOS8中已启用network.service,采用NetworkManager(NM)为网卡启动命令。不过在此期间仍然可以用network.service作为网卡服务,只是默认没有安装(安装方式dnf install network-script)。不过官方已经明确在下一个大版本中,将彻底放弃network.service,不建议继续使用network.service管理网络。
 

Redhat、CentOS、Rocky系列修改网卡名

#修改内核参数
vim /etc/default/grub
找到此行GRUB_CMDLINE_LINUX=""   , 双引号内末尾添加 net.ifnames=0

#生产grub配置文件
grub2-mkconfig -o /boot/grub2/grub.cfg

#重启生效
reboot

 

Ubuntu修改网卡名

默认ubuntu的网卡名称和CentOS7类似,如ens33 ens196等

#修改内核参数
vim /etc/default/grub
找到此行GRUB_CMDLINE_LINUX=""   , 双引号内末尾添加 net.ifnames=0

#生产grub配置文件
grub-mkconfig -o /boot/grub/grub.cfg
或者update-grub

#重启生效
reboot

 

修改网卡ip

方法1 ifconfig

ifconfig命令 来自net-tools工具包

#临时添加
ifconfig eth0 10.0.0.68 netmask 255.255.255.0

#临时删除
ifconfig eth0 0.0.0.0 或者 ifconfig eth0 0

 

方法2 ip

ip命令 来自iproute工具包

ip addr add 172.16.100.100/16 dev eth0 (ip addr add 可以简写成 ip a a )

 

方法3 nmcli

nmcli命令 来自NetworkManager工具包

nmcli connection add con-name static ifname eth0 autoconnect yes type Ethernet ipv4.addresses 172.25.x.10/24 ipv4.gateway 172.25.x.254

 

方法4 修改配置文件

CentOS系列、Rocky系列网卡配置路径相同

/etc/sysconfig/network-scripts

DEVICE=eth0
NAME=eth0
BOOTPROTO=static
IPADDR=10.0.0.8
PREFIX=24
GATEWAY=10.0.0.2
DNS1=10.0.0.2
DNS2=180.76.76.76
ONBOOT=yes

 

Ubuntu网卡配置路径 

vim /etc/netplan/01-netcfg.yarm
#网卡配置文件采用YAML格式,必须以 /etc/netplan/XXX.yaml 文件命名方式存放
#可以每个网卡对应一个单独的配置文件,也可以将所有网卡都放在一个配置文件里

network:
    version: 2
    renerer: networkd
    ethernets:
        eth0:
            addresses: [192.168.8.10/24,10.0.0.10/8]   #或者用下面两行,两种格式不用混用
            - 192.168.8.10
            - 10.0.0.10/8
            gateway4: 10.0.0.2
            nameservers:
                search: [xxxx.com, xxx.org]
                addresses: [180.76.76.76, 8.8.8.8, 1.1.1.1]
                
#使配置生效
netplay apply

 

posted @   whisper2008  阅读(199)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示