Centos7中更改IP为静态地址

Centos7中更改IP为静态地址

1. 查看当前ip信息

  1. 可以看到当前的IP为192.168.31.20
  2. 这里网卡名称为ens33,具体以自己机器实际查到的为主
[root@dev ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.31.20  netmask 255.255.255.0  broadcast 192.168.31.255
        inet6 fe80::946b:95a9:8723:fc8b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:be:f2:72  txqueuelen 1000  (Ethernet)
        RX packets 4045  bytes 414510 (404.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 769  bytes 107460 (104.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 32  bytes 2592 (2.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32  bytes 2592 (2.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@dev ~]#

2. 查找ens33文件位置

  1. 查找网络配置文件

一般网络配置文件的名称以ifcfg-开头,所以我们查找名称为ifcfg-ens33的文件,就可以查到具体地址

[root@dev dev]# find / -name 'ifcfg-ens33'
/etc/sysconfig/network-scripts/ifcfg-ens33
[root@dev dev]# 
  1. 跟进查找出的网络配置文件,进入到其根目录

进入 /etc/sysconfig/network-scripts目录,然后查看此目录下的所以文件

# 进入  /etc/sysconfig/network-scripts 目录
[root@dev ~]# cd /etc/sysconfig/network-scripts
# 查看目录下的所有文件,可以看到第一个就是 ifcfg-ens33 文件
[root@dev network-scripts]# ll
总用量 232
-rw-r--r--. 1 root root   310 1120 14:40 ifcfg-ens33
-rw-r--r--. 1 root root   254 522 2020 ifcfg-lo
lrwxrwxrwx. 1 root root    24 1120 14:35 ifdown -> ../../../usr/sbin/ifdown
-rwxr-xr-x. 1 root root   654 522 2020 ifdown-bnep
-rwxr-xr-x. 1 root root  6532 522 2020 ifdown-eth
-rwxr-xr-x. 1 root root   781 522 2020 ifdown-ippp
-rwxr-xr-x. 1 root root  4540 522 2020 ifdown-ipv6
lrwxrwxrwx. 1 root root    11 1120 14:35 ifdown-isdn -> ifdown-ippp
-rwxr-xr-x. 1 root root  2130 522 2020 ifdown-post
-rwxr-xr-x. 1 root root  1068 522 2020 ifdown-ppp
-rwxr-xr-x. 1 root root   870 522 2020 ifdown-routes
-rwxr-xr-x. 1 root root  1456 522 2020 ifdown-sit
-rwxr-xr-x. 1 root root  1621 129 2018 ifdown-Team
-rwxr-xr-x. 1 root root  1556 129 2018 ifdown-TeamPort
-rwxr-xr-x. 1 root root  1462 522 2020 ifdown-tunnel
lrwxrwxrwx. 1 root root    22 1120 14:35 ifup -> ../../../usr/sbin/ifup
-rwxr-xr-x. 1 root root 12415 522 2020 ifup-aliases
-rwxr-xr-x. 1 root root   910 522 2020 ifup-bnep
-rwxr-xr-x. 1 root root 13758 522 2020 ifup-eth
-rwxr-xr-x. 1 root root 12075 522 2020 ifup-ippp
-rwxr-xr-x. 1 root root 11893 522 2020 ifup-ipv6
lrwxrwxrwx. 1 root root     9 1120 14:35 ifup-isdn -> ifup-ippp
-rwxr-xr-x. 1 root root   650 522 2020 ifup-plip
-rwxr-xr-x. 1 root root  1064 522 2020 ifup-plusb
-rwxr-xr-x. 1 root root  4997 522 2020 ifup-post
-rwxr-xr-x. 1 root root  4154 522 2020 ifup-ppp
-rwxr-xr-x. 1 root root  2001 522 2020 ifup-routes
-rwxr-xr-x. 1 root root  3303 522 2020 ifup-sit
-rwxr-xr-x. 1 root root  1755 129 2018 ifup-Team
-rwxr-xr-x. 1 root root  1876 129 2018 ifup-TeamPort
-rwxr-xr-x. 1 root root  2780 522 2020 ifup-tunnel
-rwxr-xr-x. 1 root root  1836 522 2020 ifup-wireless
-rwxr-xr-x. 1 root root  5419 522 2020 init.ipv6-global
-rw-r--r--. 1 root root 20678 522 2020 network-functions
-rw-r--r--. 1 root root 30988 522 2020 network-functions-ipv6
[root@dev network-scripts]# 

3. 查看ifcfg-ens33网络配置

[root@dev network-scripts]# cat ifcfg-ens33 
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="fd81f97a-8c9d-410c-a08f-8e12a945cb05"
DEVICE="ens33"
ONBOOT="yes"

4. 修改为静态IP地址

  1. 这里将IP改为192.168.31.100

  2. 编辑/etc/sysconfig/network-scripts/ifcfg-ens33网络配置文件并改相关配置即可

# 编辑 ifcfg-ens33
[root@dev]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

# 打开编辑器后,按下面的说进行修改或添加内容
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static" # 这里将 dhcp(自动获取) 改为 static
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="fd81f97a-8c9d-410c-a08f-8e12a945cb05"
DEVICE="ens33"
ONBOOT="yes" # 这里如果为no的话,就改为yes
# 2.新增下面内容
IPADDR=192.168.31.100 #静态IP  
GATEWAY=192.168.31.1 #默认网关  
NETMASK=255.255.255.0 #子网掩码  
DNS1=192.168.31.1 #DNS 配置  
DNS2=114.114.114.114   # 国内DNS地址,国内用户推荐使用,速度较快

5. 重启网卡

[root@dev]# systemctl restart network 

6. 查看改后的IP

[root@dev ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.31.100  netmask 255.255.255.0  broadcast 192.168.31.255
        inet6 fe80::946b:95a9:8723:fc8b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:be:f2:72  txqueuelen 1000  (Ethernet)
        RX packets 6555  bytes 640304 (625.2 KiB)
        RX errors 0  dropped 24  overruns 0  frame 0
        TX packets 1635  bytes 285875 (279.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 32  bytes 2592 (2.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32  bytes 2592 (2.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@dev ~]# 

重启网卡后可看到IP已经被更改为192.168.31.100了。至此,静态IP修改完成。

相关推荐

  1. Centos7更改IP静态地址

    2024-04-30 04:52:02       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-30 04:52:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-30 04:52:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-30 04:52:02       87 阅读
  4. Python语言-面向对象

    2024-04-30 04:52:02       96 阅读

热门阅读

  1. RK3588 Linux5.10 GT9XX 调试

    2024-04-30 04:52:02       38 阅读
  2. Chrome插件开发

    2024-04-30 04:52:02       32 阅读
  3. 强化学习Upper Confidence Bound策略笔记

    2024-04-30 04:52:02       31 阅读
  4. H5 录音功能

    2024-04-30 04:52:02       33 阅读
  5. db2 export

    2024-04-30 04:52:02       31 阅读
  6. 90天玩转Python—20—Python面向对象编程入门指南

    2024-04-30 04:52:02       33 阅读
  7. 如何拥有自己的私有docker仓库

    2024-04-30 04:52:02       36 阅读
  8. 思科Cisco2960 交换机 多端口配置VLAN

    2024-04-30 04:52:02       34 阅读
  9. 垃圾分类子项目2 - 加入舵机控制

    2024-04-30 04:52:02       24 阅读