Linux系列系统配置固定IP地址教程 包括但不限于centos ubuntu等等
前言
在运维工作中你会遇到很多场景下的挑战,诸如更换系统更换硬盘挂载硬盘配置IP地址等等,在这些场景中就需要要求你能够熟练的做出应对,在下面的课程中豆芽会教你如何完成各类挑战。
什么是IP地址
IP地址(Internet Protocol Address)是指互联网协议地址,又译为网际协议地址
Centos配置IP
Centos系统的网卡配置文件保存在/etc/sysconfig/network-scripts目录下 进入网卡配置目录
cd /etc/sysconfig/network-scripts
使用 ls -a 命令列出所有目录文件
[root@s12-196 network-scripts]# ls -a
. ifdown-post ifup-eth ifup-Team
.. ifdown-ppp ifup-ippp ifup-TeamPort
ifcfg-eth0 ifdown-routes ifup-ipv6 ifup-tunnel
ifcfg-eth1 ifdown-sit ifup-isdn ifup-wireless
ifdown ifdown-Team ifup-plip init.ipv6-global
ifdown-bnep ifdown-TeamPort ifup-plusb network-functions
ifdown-eth ifdown-tunnel ifup-post network-functions-ipv6
ifdown-ippp ifup ifup-ppp
ifdown-ipv6 ifup-aliases ifup-routes
ifdown-isdn ifup-bnep ifup-sit
[root@s12-196 network-scripts]#
在这里我们可以看到各种各样的配置文件 配置IP地址我只需要用到网卡配置文件即可 其他文件的作用在这里不做说明。
那么如何判断那个文件为网卡配置文件呢?centos的网卡配置文件一般以 ifcfg 开头 对比上面的文件 我们可以看到有ifcfg-eth0 ifcfg-eth1 2个网卡配置文件。
为什么有2个网卡配置文件?
这是因为当前使用的服务器为双网卡服务器,有2个物理网卡,如果你的服务器只有一个网卡那么配置文件里面就只会出现一个网卡配置,当然也有2个以上网卡的服务器,相应的也就有多个网卡配置文件
如何判断服务器使用的网卡?
既然服务器有多个网卡,那么我们如何判断当前使用的网卡是哪一个呢,方法也很简单,使用 mii-tool 工具即可查询
这里我们使用mii-tool工具分别查询网卡eth0 eth1的连接情况
命令格式为 mii-tool 网卡名称 所以这里我们分别运行mii-tool eth0 与 mii-tool eth0
[root@s12-196 network-scripts]# mii-tool eth0
eth0: negotiated 1000baseT-FD flow-control, link ok
[root@s12-196 network-scripts]#
[root@s12-196 network-scripts]# mii-tool eth1
eth1: no link
[root@s12-196 network-scripts]#
注意终端输出的提示内容 其中 eth0: negotiated 1000baseT-FD flow-control, link ok 及代表网卡eth0正在使用中 反之eth1: no link及代表网卡eth1未使用
所以我们要配置固定IP的网卡文件为 ifcfg-eth0
使用vi编辑器编辑ifcfg-eth0配置文件
vi ifcfg-eth0
下面是配置案例 按照下面的案例修改配置文件即可
DEVICE=eth0 #网卡名称 一般默认无需修改
BOOTPROTO=static #使用静态模式配置IP地址 动态获取为dhcp
IPADDR=IP地址 #这里填写需要配置的固定IP地址
NETMASK= 掩码 #填写掩码
GATEWAY=网关 #填写网关
ONBOOT=yes #开机自动启动 这里一定要设置为yes开机自动启动
TYPE=Ethernet
NM_CONTROLLED=no
DNS1=主DNS地址 #服务器的dns地址
DNS2=附DNS地址 #服务器的dns地址
MTU=1500 #MTU 一般默认1500
下面是我配置好的网卡配置文件
[root@s12-196 network-scripts]# cat ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
IPADDR=117.24.12.196
NETMASK=255.255.255.0
GATEWAY=117.24.12.1
ONBOOT=yes
TYPE=Ethernet
NM_CONTROLLED=no
DNS1=117.24.13.8
DNS2=110.80.137.52
MTU=1410
[root@s12-196 network-scripts]#
配置完成以后保存配置文件 重启网卡 使用命令
service network restart
出现下面的提示即代表网卡重启成功
[root@s12-196 network-scripts]# service network restart
Restarting network (via systemctl): [ OK ]
[root@s12-196 network-scripts]#
打开cmd工具ping下你配置的IP地址 验证一下
附:centos添加多个IP地址教程kk.biger.me/教程/centos_chg_ip.mp4
Ubuntu16.04配置IP
ubuntu16.04的网卡配置文件保存在/etc/network/interfaces文件中
使用nano编辑器编辑配置
nano /etc/network/interfaces
案例
auto eth0
iface eth0 inet static
address
netmask
gateway
dns-nameservers 114.114.114.114
mtu 1410
重启网络服务
/etc/init.d/networking restart
Ubuntu18.01配置IP
注:该方法同样适用于ubuntu20.04以上系统
Ubuntu18.04系统 netplan配置文件位于/etc/netplan/目录下以**-cloud-init.yaml命名 默认情况下使用的DHCP获取IP地址,如果需要设置固定IP地址那么我们就需要修改相关的配置文件
进入netplan文件目录 使用编辑器打开配置文件
cd /etc/netplan/
nano **-cloud-init.yaml
按下面的案例进行配置即可
network:
ethernets:
ens33:
addresses: [10.0.0.2/24] #IP地址跟掩码
dhcp4: false #关闭自动获取IP
gateway4: 10.0.0.1 #网关地址
nameservers:
addresses: [192.168.1.1] #DNS服务器地址
optional: true
version: 2
配置完成以后重启网卡
sudo netplan apply
附ubuntu18.04以上系统多IP配置案例
network:
ethernets:
eno1:
addresses:
- 45.113.202.22/24
- 45.113.202.21/24
gateway4: 45.113.202.1
nameservers:
addresses:
- 114.114.114.114
version: 2
在上面的案例中相较于单IP地址 只需要修改addresses这项配置即可
- 45.113.202.22/24 #代表第一个IP
- 45.113.202.21/24 #代表第二个IP
Debian10配置IP
Debian的网卡配置文件存放在/etc/network/interfaces目录下,设置固定IP地址我们只需要修改此配置文件即可
使用nano编辑器打开/etc/network/interfaces
nano /etc/network/interfaces
以下是interfaces最基础的配置文件案例以供参考
auto lo #开机自动启动lo接口
iface lo inet loopback
auto eno1 #开机自动启动eno1接口
iface eno1 inet static #设置静态IP模式 其中static代表静态 如果自动获取改为dhcp
address 192.168.2.22 #静态ip地址
netmask 255.255.255.0 #掩码
gateway 192.168.2.1 #网关
dns-nameservers 114.114.114.114 #dns地址
dns-nameservers 8.8.8.8 #dns地址
mtu 1410 #MTU
配置完成以后执行命令重启网卡
/etc/init.d/networking restart
附:debian配置多个IP地址案例
auto lo
iface lo inet loopback
auto eno1 eno1:1
iface eno1 inet static
address 112.5.37.16
netmask 255.255.255.192
gateway 112.5.37.1
dns-nameservers 110.80.137.52
dns-nameservers 117.24.13.8
iface eth0:1 inet static
address 192.168.0.200
netmask 255.255.255.0
相较于单IP地址配置这里多了一个iface eth0:1 inet static的配置
iface eth0:1 inet static
address 192.168.0.200
netmask 255.255.255.0
多个IP我们只要新建相应的多个子网卡即可
猜你喜欢
发表评论

暂无评论,你要说点什么吗?