2025年4月18日 星期五 乙巳(蛇)年 正月十九 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 系统应用 > Linux

Linux主流发行版本配置IP总结(Ubuntu、CentOS、Redhat、Suse)

时间:02-28来源:作者:点击数:10

我们先了解下IP的概念

IP地址简介

  • 电脑连接互联网的必要条件:IP地址+子网掩码+网关+DNS
  • IP地址是上网的唯一标识

IPv4地址分类:

IPv4地址分为A-E共计5类地址,其中A、B、C是基本类,D、E作为多播和保留使用

图片

特殊地址:

  • 0.0.0.0:常用于缺省路由,比如:ip route-static 0.0.0.0 0.0.0.0 10.10.10.1
  • 255.255.255.255 :指本网段内的所有主机
  • 127.0.0.1:Localhost,本地回环地址
  • 224.0.0.1 :组播地址,特指所有主机
  • 169.254.x.x :通常是Windows操作系统在DHCP租用失败时自动给客户机分配的IP地址

私有地址:

(企业内部分配的地址,不会出现在互联网的路由表中)

  • 10.x.x.x
  • 172.16.x.x ~ 172.31.x.x
  • 192.168.x.x

一、Ubuntu系统配置IP

1)Ubuntu 18.04以前版本

  • [root@qll253 ~]# vi /etc/network/interfaces
  • auto enp10s0
  • iface enp10s0 inet static
  • address 192.168.1.2
  • netmask 255.255.255.0
  • gateway 192.168.1.1
  • dns-nameservers 114.114.114.114,8.8.8.8
  • [root@qll253 ~]# /etc/init.d/networking restart #重启网络

2)Ubuntu 18.04以后版本(包含18.04版本)

  • [root@qll253 ~]# vi /etc/netplan/50-cloud-init.yaml
  • network:
  • version: 2
  • renderer: networkd
  • ethernets:
  • enp10s0:
  • dhcp4: no
  • addresses: [192.168.1.3/24]
  • gateway4: 192.168.1.1
  • nameservers:
  • addresses: [114.114.114.114,8.8.8.8]
  • [root@qll253 ~]# netplan try #检测配置文件是否有误。若语法有误,不生效;检测通过则配置生效
  • [root@qll253 ~]# netplan apply #跳过检测环节,直接应用配置文件

如果还想使用Ubuntu 18.04以前版本的方式配置IP,则安装ifupdown软件包即可

apt install ifupdown

二、CentOS(Redhat)系统配置IP

1)CentOS/Redhat (5.x、6.x、7.x)

  • [root@qll253 ~]# vi /etc/sysconfig/network-scripits/ifcfg-eth0
  • DEVICE=eth0
  • ONBOOT=yes
  • TYPE=Ethernet
  • BOOTPROTO=static
  • IPADDR=192.168.1.4
  • NETMASK=255.255.255.0
  • GATEWAY=192.168.1.1
  • DNS1=114.114.114.114
  • DNS2=8.8.8.8
  • [root@qll253 ~]# service network restart #重启网卡服务

2)CentOS/Redhat (8.x)

CentOS 8由于是新发布的系统(发布时间:2019.9),小伙伴们可能对它配置IP不太熟悉,我这里多介绍一点

  1. 配置静态IP
  • # 创建ens33网卡配置信息,包含:指定永久静态IP、网关、并ifup启动
  • nmcli connection add type ethernet con-name ens33 ifname ens33 ipv4.addresses 192.168.1.5/24 ipv4.gateway 192.168.1.1 ipv4.method manual
  1. 配置动态IP
  • # 创建ens33网卡配置信息,指定动态获取IP,并ifup启动
  • nmcli connection add type ethernet con-name ens33 ifname ens33 ipv4.method auto
  1. 修改IP(非交互式)
  • [root@qll253 ~]# nmcli connection modify ens33 ipv4.addresses 192.168.1.6/24
  • [root@qll253 ~]# nmcli connection up ens33 #相当于ifup ens33
  1. 修改IP(交互式)
  • [root@qll253 ~]# nmcli connection edit ens33
  • nmcli> goto ipv4.addresses
  • nmcli ipv4.addresses> change
  • Edit 'addresses' value: 192.168.1.7/24
  • Do you also want to set 'ipv4.method' to 'manual'? [yes]: yes
  • nmcli ipv4.addresses> back
  • nmcli ipv4> save
  • nmcli ipv4> activate
  • nmcli ipv4> quit
  • [root@qll253~]#
  1. 查看ip(类似于ifconfig、ip addr)
  • [root@qll253 ~]# nmcli

1. 通过修改配置文件更改网络配置,是由network.service提供。

2.centos8(rhel8)已废弃network.service,默认只能通过NetworkManager.service提供的nmcli命令修改网络配置

3. 如果希望8版本以后支持修改配置文件的方式,我们只需要安装network.service即可

yum install network-scripts

三、openSUSE 配置IP

  • [root@qll253 ~]# vi /etc/sysconfig/network/ifcfg-eth0
  • DEVICE=eth0
  • ONBOOT=yes
  • BOOTPROTO=static
  • IPADDR=192.168.1.8
  • NETMASK=255.255.255.0
  • GATEWAY=192.168.1.1
  • [root@qll253 ~]# service network restart #重启网络

四、总结

今天通过对主流的Linux系统配置IP的总结,觉得NetworkManager工具挺强大。大家以后可以多适应nmcli命令。

最后夸夸NetworkManager~

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐