CentOS 手动添加网卡配置文件并设置静态 IP

环境

CentOS Linux release 7.5.1804

Q

新安装CentOS后,为了方便远程ssh,想设置静态IP,但是在/etc/sysconfig/network-scripts/下面只有ifcfg-lo这张Local Loopback网卡的配置文件,并没有我们想要的enp0s3(不同机器的网卡名不一样,也可能是ens33或其它名字)网卡配置文件,想要配置静态IP根本无从下手。

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-lo     ifdown-post      ifup          ifup-plip    ifup-TeamPort
ifdown       ifdown-ppp       ifup-aliases  ifup-plusb   ifup-tunnel
ifdown-bnep  ifdown-routes    ifup-bnep     ifup-post    ifup-wireless
ifdown-eth   ifdown-sit       ifup-eth      ifup-ppp     init.ipv6-global
ifdown-ippp  ifdown-Team      ifup-ippp     ifup-routes  network-functions
ifdown-ipv6  ifdown-TeamPort  ifup-ipv6     ifup-sit     network-functions-ipv6
ifdown-isdn  ifdown-tunnel    ifup-isdn     ifup-Team

A

  • 使用nmcli con show查看网卡的UUID
[root@localhost network-scripts]# nmcli con show
NAME        UUID                                  TYPE      DEVICE  
enp0s3  ad633ea8-e358-255e-a167-6c14742b347d  ethernet  enp0s3 
  • /etc/sysconfig/network-scripts/目录下新建ifcfg-enp0s3(网卡名改成自己机器上的网卡名)
vim ifcfg-enp0s3  # 文件名改为自己的网卡名
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
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=enp0s3  # 改为自己的网卡名
UUID=ad633ea8-e358-255e-a167-6c14742b347d  # 改为自己的UUID
DEVICE=enp0s3  # 改为自己的网卡名
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.222  # 改为自己想要的IP,前三段保持和原来的IP一致
NETMASK=255.255.255.0
GATEWAY=192.168.0.1  # 一般为x.x.x.1,具体可在物理机上使用ipconfig命令查看
DNS1=114.114.114.114
DNS2=114.114.115.115
  • 配置文件保存后重启网络服务
systemctl restart network
  • 查看网络信息
[root@localhost network-scripts]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.222  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 de12::f1a4:3694:6189:d250 prefixlen 64  scopeid 0x20<link>
        ether 05:01:24:0a:54:15  txqueuelen 1000  (Ethernet)
        RX packets 221235  bytes 325157863 (310.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 129166  bytes 10537641 (10.0 MiB)
        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 40  bytes 3528 (3.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 40  bytes 3528 (3.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 惯例,调戏下百度
[root@localhost network-scripts]# ping www.baidu.com
PING www.a.shifen.com (115.239.211.112) 56(84) bytes of data.
64 bytes from 115.239.211.112 (115.239.211.112): icmp_seq=1 ttl=54 time=12.0 ms
64 bytes from 115.239.211.112 (115.239.211.112): icmp_seq=2 ttl=54 time=11.9 ms
  • 搞定

博客更新地址

说点什么

  Subscribe  
提醒