Skip to content

一、前言

  • 适用的系统版本
    • CentOS 6
    • CentOS 7

二、操作步骤

1. 确认网卡配置文件

  • 查看网络连接信息
    shell
    [root@localhost ~]# ifconfig
    enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.56.103  netmask 255.255.255.0  broadcast 192.168.56.255
            inet6 fe80::42b9:b238:84e2:e586  prefixlen 64  scopeid 0x20<link>
            ether 08:00:27:e3:b8:cc  txqueuelen 1000  (Ethernet)
            RX packets 52  bytes 7763 (7.5 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 50  bytes 7259 (7.0 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 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    输出的第一行信息说明,网卡设备名是:enp0s3 那么网卡配置文件就是:/etc/sysconfig/network-scripts/ifcfg-enp0s3

2. 修改网卡配置文件

  • 修改网卡配置文件

    shell
    vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
  • 修改以下配置

    shell
    # 将BOOTPROTO由dhcp改为static
    BOOTPROTO=static
    # 将ONBOOT改成yes
    ONBOOT=yes
  • 增加以下配置

    shell
    	IPADDR=192.168.0.200 #静态IP
    	NETMASK=255.255.255.0 #子网掩码
    	GATEWAY=192.168.0.1 #默认网关  
    	DNS1=192.168.0.1 #DNS1 配置
    	DNS2=192.168.1.1 #DNS2 配置

3. 重启网络服务并验证

  • 重启网络服务

    shell
    #CentOS6
    service network restart 
    
    #CentOS7
    systemctl restart network
  • 查看IP

    shell
    # 如果命令没找到 yum 安装相关包 yum -y install net-tools
    ifconfig

Released under the MIT License.