首页 > 技术文章 > CentOS7 网络管理工具nmcli

Trees 2017-11-01 13:38 原文

  今天帮别人调试虚拟机的网络问题(CentOS 7系统),习惯性直接改/etc/sysconfig/network-scripts/ifcfg-xxx配置文件,但是不知道为什么重启network后静态ip没有生效。然后百度了一下,CentOS使用NetworkManager来管理网络配置,就顺便接触了下网络管理命令行工具NetworkManager command line tool,也叫nmcli。

  初次使用也没研究太深,以下只给出一部分使用过的语句。

  查看连接服务设备

[yu@yu ~]$ nmcli connection show 
名称  UUID                                  类型            设备 
p2p1  649cf3f6-8d93-4517-b299-7b5671e37069  802-3-ethernet  p2p1 

  查看特定连接的详情

[yu@yu ~]$ nmcli connection show p2p1 
connection.id:                          p2p1
connection.uuid:                        649cf3f6-8d93-4517-b299-7b5671e37069
connection.stable-id:                   --
...

  查看网络设备状态

[yu@yu ~]$ nmcli device status
设备  类型      状态    连接 
p2p1  ethernet  连接的  p2p1 
lo    loopback  未托管  --

  使用“dhcp”创建连接

[yu@yu ~]$ nmcli connection add con-name "dhcp" type ethernet ifname enoxxxxxx
Connection 'dhcp' (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) successfully added.

  其中:connection add - 添加新的连接;

    con-name - 连接名;

    type - 设备类型;

    ifname - 接口名。

  使用“static”创建连接

[yu@yu ~]$ nmcli connection add con-name "static" ifname enoxxxxxx autoconnect no type ethernet ip4 192.168.1.xxx gw4 192.168.1.1
Connection 'static' (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) successfully added.

  其中:connection add - 添加新的连接;

    con-name - 连接名;

    ifname - 接口名;

    type - 设备类型;

    autoconnect - 自动连接;

    ip4 - 指定ip;

    gw4 - 网关。

  更新连接

[yu@yu ~]$ nmcli connection up enoxxxxxx

  单独设置连接

  设置静态ip地址

nmcli connection modify <interface> ipv4.addresses '192.168.1.xxx'

  设置DNS

nmcli connection modify <interface> ipv4.dns '8.8.8.8'

  设置网关

nmcli connection modify <interface> ipv4.gateway '192.168.1.1'

  设置IP地址为手动指定

nmcli connection modify <interface> ipv4.method manual

  设置开机自动连接

nmcli connection modify <interface> connection.autoconnect yes

  重新加载配置

nmcli connection reload

  配置完成需要重启网络

systemctl restart network

  

 

推荐阅读