Linux - CentOS7 设置静态IP地址

配置静态IP

1.查看IP配置信息

ifconfig

如上图所示,我的em1网卡已配置好

2、编辑em1对应的配置文件,位于/etc/sysconfig/network-scripts/ifcfg-你的网卡名字

操作:vim /etc/sysconfig/network-scripts/ifcfg-你的网卡名字,按i键进入编辑。编辑结束后Esc退出编辑→Shift+q→!wq→x

修改配置文件如下图

 参数说明:

复制代码
BOOTPROTO="static" # 使用静态IP地址,默认为dhcp 
IPADDR="19.37.33.66" # 设置的静态IP地址 NETMASK="255.255.255.0" # 子网掩码 GATEWAY="19.37.33.1" # 网关地址 DNS1="192.168.241.2" # DNS服务器(此设置没有用到,所以我的里面没有添加) ONBOOT=yes #设置网卡启动方式为 开机启动 并且可以通过系统服务管理器 systemctl 控制网卡
复制代码

3、修改/etc/sysconfig/network

# Created by anaconda
NETWORKING=yes
GATEWAY=19.37.33.1

4、重启网络服务

service network restart

 

Best commands in my opinion:

They disables network manager off and on. It is an exact equivalent of unchecking enable networking, and re-checking it to regain connectivity.-

nmcli networking off hitenter

enter image description here

nmcli networking on hit enter

enter image description here

Hope this helps.

 

I'd use the nmcli commands for that purpose:

nmcli nm enable false && nmcli nm enable true

 

man page:

DESCRIPTION
       nmcli is a command‐line tool for controlling NetworkManager and report‐
       ing on its status.  It is not meant as a full replacement for nm‐applet
       or other similar clients but as a complementary utility to those pro‐
       grams.  The main usage for nmcli is on servers, headless machines or
       for power users who prefer the command line.

       [...]

  nm     NetworkManager
          Use this object to inquire and change state of NetworkManager.

         [...]

          enable [true|false]
                 Get networking‐enabled status or enable/disable network‐
                 ing by NetworkManager.  All interfaces managed by Net‐
                 workManager are deactivated when networking has been dis‐
                 abled.
                 Reference to D‐Bus:
                 interface: org.freedesktop.NetworkManager
                 method:    Enable
                 arguments: TRUE or FALSE

 

 

posted @ 2021-04-09 09:22  7bGWFm  阅读(132)  评论(0编辑  收藏  举报