CentOS 7 下网络管理之命令行工具nmcli

在CentOS7中默认使用NetworkManager守护进程来监控和管理网络设置。nmcli是命令行的NetworkManager工具,会自动把配置写到/etc/sysconfig/network-scripts/目录下面。

NetworkManager最初由 Redhat 公司开发,现在由 GNOME 管理。

CentOS7之前的网络管理是通过ifcfg文件配置管理接口(device),而现在是通过NetworkManager服务管理连接(connection)。一个接口(device)可以有多个连接(connection),但是同时只允许一个连接(connection)处于激活(active)状态。

简单理解就是,一个连接就是(connection)就是/etc/sysconfig/network-scripts/目录下的一个配置文件,接口(device)是物理设备,一个物理设置可以拥有多个配置文件,但只能有一个配置文件属于使用(active)状态;配置文件的生成与使用状态均由NetworkManager控制。

当然,依旧支持ifcfg文件配置管理网络,但不推荐

命令学习

查看帮助

[root@karate ~]# nmcli -h
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }

OPTIONS
  -t[erse]                                   terse output
  -p[retty]                                  pretty output
  -m[ode] tabular|multiline                  output mode
  -f[ields] <field1,field2,...>|all|common   specify fields to output
  -e[scape] yes|no                           escape columns separators in values
  -n[ocheck]                                 don't check nmcli and NetworkManager versions
  -a[sk]                                     ask for missing parameters
  -w[ait] <seconds>                          set timeout waiting for finishing operations
  -v[ersion]                                 show program version
  -h[elp]                                    print this help

OBJECT
  g[eneral]       NetworkManager's general status and operations
  n[etworking]    overall networking control
  r[adio]         NetworkManager radio switches
  c[onnection]    NetworkManager's connections
  d[evice]        devices managed by NetworkManager
  a[gent]         NetworkManager secret agent or polkit agent

有六个OBJECT,常用的有connection,device,general查看它们的帮助

[root@karate ~]# nmcli c -h
Usage: nmcli connection { COMMAND | help }

COMMAND := { show | up | down | add | modify | edit | delete | reload | load }

  show [--active] [[--show-secrets] [id | uuid | path | apath] <ID>] ...

  up [[id | uuid | path] <ID>] [ifname <ifname>] [ap <BSSID>] [passwd-file <file with passwords>]

  down [id | uuid | path | apath] <ID> ...

  add COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS IP_OPTIONS

  modify [--temporary] [id | uuid | path] <ID> ([+|-]<setting>.<property> <value>)+

  edit [id | uuid | path] <ID>
  edit [type <new_con_type>] [con-name <new_con_name>]

  delete [id | uuid | path] <ID>

  reload

  load <filename> [ <filename>... ]

常用命令

查看接口设备信息

# 简单信息
nmcli device status
# 详细的接口信息
nmcli device show
# 接口的详细信息
nmcli device show interface-name

查看连接(connection)的信息

# 简单信息
nmcli connection show
# 详细的连接信息
nmcli connection show
# 某个连接的详细信息
nmcli connection show connection-name

启动和停止接口

nmcli connection down connection-name
nmcli connection up connection-name
nmcli device disconnect interface-name
nmcli device connect interface-name

建议使用 nmcli dev disconnect interface-name 命令,而不是 nmcli con down connection-name 命令,因为连接断开可将该接口放到“手动”模式,这样做用户让 NetworkManager 启动某个连接前,或发生外部事件(比如载波变化、休眠或睡眠)前,不会启动任何自动连接。

创建连接

nmcli connection add type ethernet con-name connection-name ifname interface-name
nmcli connection add type ethernet con-name connection-name ifname interface-name ip4 address gw4 address

## e.g. 创建一个基于eth1接口的连接
# 创建动态连接,即BOOTPROTO默认为DHCP
[root@localhost ~]# nmcli c add type eth con-name dynamic-eth1 ifname eth1
Connection 'dynamic-eth1' (9c0ad8a9-21f6-40b5-9313-e5c7e4b356f1) successfully added.
# 创建静态连接
[root@localhost ~]# nmcli connection add type eth con-name static-eth1 ifname eth1 ip4 172.16.60.10/24
# nmcli connection add type eth con-name static-eth1 ifname eth1 ip4 172.16.60.10/24 gw4 192.168.60.1
Connection 'static-eth1' (0640bf7f-9490-44a8-be96-2e710fb650e6) successfully added.

创建连接后,NetworkManager 自动将 connection.autoconnect 设定为 yes。还会将设置保存到 /etc/sysconfig/network-scripts/ connection-name 文件中,且自动将 ONBOOT 参数设定为 yes。

激活连接

nmcli connection up connection-name

## e.g. 激活eth1接口的static-eth1连接
[root@localhost ~]# nmcli c up static-eth1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)

修改连接的IP地址

# 可修改的属性可通过以下命令查看
nmcli c show static-eth1
# 修改命令
nmcli connection modify [--temporary] [id | uuid | path] <ID> ([+|-]<setting>.<property> <value>)+

## e.g. 修改连接static-eth1的ip地址
[root@localhost ~]# ip addr | grep eth1
4: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 172.16.60.10/24 brd 172.16.60.255 scope global eth1
[root@localhost ~]# nmcli c mod static-eth1 ipv4.addr 172.16.60.20/24
[root@localhost ~]# nmcli c up static-eth1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
[root@localhost ~]# ip a | grep eth1
4: eth1: <BROADCAST,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 172.16.60.20/24 brd 172.16.60.255 scope global eth1

配置连接的DNS

# 设定单个DNS
nmcli connection modify static-eth1 ipv4.dns DNS1
# 设定多个DNS
nmcli connection modify static-eth1 ipv4.dns "DNS1 DNS2"
# 以上命令会替换之前的DNS设置
# 添加某个连接的DNS,需要使用前缀“+”
nmcli connection modify static-eth1 +ipv4.dns DNS3

## e.g. 配置static-eth1连接的DNS
[root@localhost ~]# grep DNS /etc/sysconfig/network-scripts/ifcfg-static-eth1
IPV6_PEERDNS=yes
[root@localhost ~]# nmcli c mod static-eth1 ipv4.dns "114.114.114.114 223.5.5.5"
# 修改连接后,需要重新激活
[root@localhost ~]# nmcli c up static-eth1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
[root@localhost ~]# grep DNS /etc/sysconfig/network-scripts/ifcfg-static-eth1
DNS1=114.114.114.114
DNS2=223.5.5.5
IPV6_PEERDNS=yes
# 新增DNS
[root@localhost ~]# nmcli c mod static-eth1 +ipv4.dns 223.5.5.6
[root@localhost ~]# nmcli c up static-eth1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[root@localhost ~]# grep DNS /etc/sysconfig/network-scripts/ifcfg-static-eth1
DNS1=114.114.114.114
DNS2=223.5.5.5
DNS3=223.5.5.6
IPV6_PEERDNS=yes

设置主机名

# 查询当前主机名
nmcli general hostname
# 更改主机名
nmcli general hostname my-hostname
# 重启hostnamed服务
systemctl restart systemd-hostnamed

CentOS7下的主机名管理是基于系统服务systemd-hostnamed,服务自身提供了hostnamectl命令用于修改主机名,推荐这种方式进行修改
使用nmcli命令更改主机名时,systemd-hostnamed服务并不知晓 /etc/hostname 文件被修改,因此需要重启服务去读取配置;

命令交互模式

nmcli con edit
# Valid connection types: generic,802-3-ethernet (ethernet),pppoe,802-11-wireless (wifi),wimax,gsm,cdma,infiniband,adsl,bluetooth,vpn,802-11-olpc-mesh (olpc-mesh),vlan,bond,team,bridge,bond-slave,team-slave,bridge-slave
# 也可以直接指定connection-name进行交互修改
# 还是非交互配置方便

接口绑定(interface bonding)

CentOS7下新增了一种特性team,用于取代bond。

接口绑定步骤是:创建一个组接口(Team interface),创建一个接口连接,指定网卡接口(device)到组接口里

nmcli connection add type team con-name connection-name ifname interface-name [config JSON]
# JSON  指定所使用的处理器(runner)。JSON语法 '{"runner":{"name":"METHOD"}}' 
# METHOD可以是:broadcast、activebackup、roundrobin、loadbalance 或者 lacp
nmcli connection add type team-slave con-name connection-name ifname interface-name master team-name

## e.g. 创建组接口team0,并把eth1和eth2加入其中,网段为192.168.233.0/24
[root@localhost ~]# nmcli d status
DEVICE  TYPE      STATE      CONNECTION
eth0    ethernet  connected  eth0
eth1    ethernet  connected  Wired connection 1
eth2    ethernet  connected  Wired connection 2
lo      loopback  unmanaged  --
[root@localhost ~]# nmcli c show
NAME                UUID                                  TYPE            DEVICE
Wired connection 2  34494b9d-f056-4f30-841c-7e6fad3b73d0  802-3-ethernet  eth2
Wired connection 1  b7ca472c-67f7-4885-ba3b-1b572d3e0d40  802-3-ethernet  eth1
eth0                54bd03bd-1300-409b-974f-d98ed3bb8891  802-3-ethernet  eth0
[root@localhost ~]# nmcli c del "Wired connection 2"
[root@localhost ~]# nmcli c del "Wired connection 1"
[root@localhost ~]# nmcli c show
NAME  UUID                                  TYPE            DEVICE
eth0  54bd03bd-1300-409b-974f-d98ed3bb8891  802-3-ethernet  eth0
[root@localhost ~]# nmcli d status
DEVICE  TYPE      STATE         CONNECTION
eth0    ethernet  connected     eth0
eth1    ethernet  disconnected  --
eth2    ethernet  disconnected  --
lo      loopback  unmanaged     --

# 创建组接口,并分配ip地址
[root@localhost ~]# nmcli c add type team con-name team0 ifname team0 config '{"runner":{"name":"activebackup"}}' ip4 192.168.233.10/24 gw4 192.168.233.2
Connection 'team0' (4e75c1da-6ce5-4cbc-85fe-da5aa289b7d8) successfully added.
[root@localhost ~]# nmcli c mod team0 ipv4.dns "114.114.114.114 223.5.5.5"
[root@localhost ~]# nmcli c show
NAME   UUID                                  TYPE            DEVICE
eth0   54bd03bd-1300-409b-974f-d98ed3bb8891  802-3-ethernet  eth0
team0  4e75c1da-6ce5-4cbc-85fe-da5aa289b7d8  team            team0

# 将网卡接口加入到组接口中
[root@localhost ~]# nmcli c add type team-slave ifname eth1 master team0
Connection 'team-slave-eth1' (3ef0011b-6b69-4dfb-998b-13bf3d729c9c) successfully added.
[root@localhost ~]# nmcli c add type team-slave ifname eth2 master team0
Connection 'team-slave-eth2' (fe3fc939-dbff-485e-aef6-9fbf9f807926) successfully added.

# 启动组接口
[root@localhost ~]# nmcli c up team0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)

# 查看 team0 当前活动的端口,活动端口基于接口的连接
[root@localhost ~]# teamnl team0 ports
 4: eth2: up 1000Mbit FD
 3: eth1: up 1000Mbit FD
[root@localhost ~]# nmcli d status
DEVICE  TYPE      STATE      CONNECTION
eth0    ethernet  connected  eth0
eth1    ethernet  connected  team-slave-eth1
eth2    ethernet  connected  team-slave-eth2
team0   team      connected  team0
lo      loopback  unmanaged  --
[root@localhost ~]# nmcli c show
NAME             UUID                                  TYPE            DEVICE
eth0             54bd03bd-1300-409b-974f-d98ed3bb8891  802-3-ethernet  eth0
team-slave-eth2  fe3fc939-dbff-485e-aef6-9fbf9f807926  802-3-ethernet  eth2
team-slave-eth1  3ef0011b-6b69-4dfb-998b-13bf3d729c9c  802-3-ethernet  eth1
team0            4e75c1da-6ce5-4cbc-85fe-da5aa289b7d8  team            team0
[root@localhost ~]# ip a s team0
5: team0: <BROADCAST,LOWER_UP> mtu 1500 qdisc noqueue state UP
    link/ether 00:0c:29:d0:a2:77 brd ff:ff:ff:ff:ff:ff
    inet 192.168.233.10/24 brd 192.168.233.255 scope global team0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed0:a277/64 scope link
       valid_lft forever preferred_lft forever
[root@localhost ~]# teamdctl team0 state
setup:
  runner: activebackup
ports:
  eth2
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
  eth1
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
runner:
  active port: eth1
[root@localhost ~]# nmcli c show team0
connection.id:                          team0
connection.uuid:                        4e75c1da-6ce5-4cbc-85fe-da5aa289b7d8
connection.interface-name:              team0
connection.type:                        team
connection.autoconnect:                 yes
connection.autoconnect-priority:        0
connection.timestamp:                   1464621245
connection.read-only:                   no
connection.permissions:
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.secondaries:
connection.gateway-ping-timeout:        0
ipv4.method:                            manual
ipv4.dns:                               114.114.114.114,223.5.5.5
ipv4.dns-search:
ipv4.addresses:                         192.168.233.10/24
ipv4.gateway:                           192.168.233.2
ipv4.routes:
ipv4.route-metric:                      -1
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.never-default:                     no
ipv4.may-fail:                          yes
ipv6.method:                            auto
ipv6.dns:
ipv6.dns-search:
ipv6.addresses:
ipv6.gateway:                           --
ipv6.routes:
ipv6.route-metric:                      -1
ipv6.ignore-auto-routes:                no
ipv6.ignore-auto-dns:                   no
ipv6.never-default:                     no
ipv6.may-fail:                          yes
ipv6.ip6-privacy:                       -1 (unknown)
ipv6.dhcp-send-hostname:                yes
ipv6.dhcp-hostname:                     --
team.config:                            {"runner":{"name":"activebackup"}}
GENERAL.NAME:                           team0
GENERAL.UUID:                           4e75c1da-6ce5-4cbc-85fe-da5aa289b7d8
GENERAL.DEVICES:                        team0
GENERAL.STATE:                          activated
GENERAL.DEFAULT:                        no
GENERAL.DEFAULT6:                       no
GENERAL.VPN:                            no
GENERAL.ZONE:                           --
GENERAL.DBUS-PATH:                      /org/freedesktop/NetworkManager/ActiveConnection/6
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/Settings/3
GENERAL.SPEC-OBJECT:                    /
GENERAL.MASTER-PATH:                    --
IP4.ADDRESS[1]:                         192.168.233.10/24
IP4.GATEWAY:                            192.168.233.2
IP4.DNS[1]:                             114.114.114.114
IP4.DNS[2]:                             223.5.5.5
IP6.ADDRESS[1]:                         fe80::20c:29ff:fed0:a277/64

测试的话,可以开个ping窗口持续ping,然后禁用team0组中的eth2;理论上ping包是不会丢失的。

# 关闭eth2网卡,看ping状态
[root@localhost ~]# nmcli d dis eth2
Device 'eth2' successfully disconnected.
[root@localhost ~]# teamdctl team0 state
setup:
  runner: activebackup
ports:
  eth1
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
runner:
  active port: eth1
[root@localhost ~]# nmcli d con eth2
Device 'eth2' successfully activated with 'fe3fc939-dbff-485e-aef6-9fbf9f807926'.
[root@localhost ~]# teamdctl team0 state
setup:
  runner: activebackup
ports:
  eth1
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
  eth2
    link watches:
      link summary: up
      instance[link_watch_0]:
        name: ethtool
        link: up
runner:
  active port: eth1
[root@localhost ~]# teamnl team0 options
 queue_id (port:eth2) 0
 priority (port:eth2) 0
 user_linkup_enabled (port:eth2) false
 user_linkup (port:eth2) true
 enabled (port:eth2) false
 queue_id (port:eth1) 0
 priority (port:eth1) 0
 user_linkup_enabled (port:eth1) false
 user_linkup (port:eth1) true
 enabled (port:eth1) true
 activeport 3
 mcast_rejoin_interval 0
 mcast_rejoin_count 1
 notify_peers_interval 0
 notify_peers_count 1
 mode activebackup
作者:无限小BUG 链接:http://www.jianshu.com/p/5d5560e9e26a 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


#使用军哥的lnmp配置虚拟主机,需要注意的是要配置hosts文件(这一点官方没有讲到)具体方法:1、修改hosts文件sudo vim /etc/hosts2、在hosts文件中新增一行(这里类似于windows上配置hosts文件)127.0.0.1 www.baidu.com3、保存并重启网络
一、配置编译环境 yum update &amp;&amp; yum upgrade yum groupinstall &quot;Development Tools&quot; yum install gcc gcc-c++ pcre* openssl* gd-devel* zlib-devel
一、下载repo文件 二、备份并替换系统的repo文件 三、执行yum源更新操作
LAMP -- Linux Apache MySQL PHP 在CentOS安装的顺序,我一般是Apache&#160;-&gt;&#160;MySQL&#160;-&gt;&#160;PHP 第一步、安装并配置Apache 1、使用yum安装 [plain]&#160;view plaincopy
一、下载nginx安装包,官网下载中心http://nginx.org/download [root@localhost software]# wget http://nginx.org/download/nginx-1.10.1.tar.gz 二、解压nginx安装包 [root@localhos
1.下载composer curl -sS https://getcomposer.org/installer | php ps:如果出现php无法运行的情况,请先把PHP加入环境变量,具体操作参考《centOS安装PHP》 2.移动或复制composer到环境下可执行 cp composer.ph
一、卸载旧版本的vim 二、安装终端字符处理库ncurses 三、下载Vim8 四、编译安装 五、添加到系统环境变量 六、安装成功
引言 我们在日常的开发过程中,很多时候需要连接服务器查看日志或者在服务器上调试代码。但是,使用 ssh 命令登录服务器每次都需要输出密码,就比较繁琐。因此我们可以使用 sshpass 通过参数指定密码进行登录。解决了这个问题之后,我发现登录服务器之后,还需要手动 cd 切换到项目目录或者日志目录。为
iptables basics Introduction iptables 是一个配置 Linux 防火墙的工具,是 netfilter 项目的一部分。 在 Linux Kernel 3.13 及以上版本中, nftables 将作为后继者取代 iptables 以成为更强大易用的防火墙工具 Bas
一、下载tengine 二、安装tenginx(nginx)的模块依赖库 三、解压并指定安装路径 四、编译安装 五、启动(tengine)nginx 六、访问(tengine)nginx
这篇文章介绍了如何快速部署一套Kubernetes集群,下面就快速开始吧! 准备工作 机器部署规划 主机 | IP | 部署组件 | | master主节点 | 192.168.199.206 | etcd、kube apiserver、kube controller manager、kube sc
一、pip下载 二、解压安装 三、安装完成
1、WebBench安装: yum install ctags(先安装依赖库) mkdir /usr/local/man (创建一个目录不然会报错) wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz tar zxv
1:查看环境: [root@10-4-14-168 html]# cat /etc/redhat-release CentOS release 6.5 (Final) 2:关掉防火墙 [root@10-4-14-168 html]# chkconfig iptables off 3:配置CentOS
安装 1.检查当前是否有安装php rpm&#160;-qa|grep&#160;php 如果有安装PHP,那么请先删除这些安装包: yum&#160;remove&#160;php* 2.安装php源 Centos 5 安装php源: rpm&#160;-ivh&#160;http://mirro
一、安装 二、配置 三、修改配置文件 四、program 配置 五、启动
Sed 日常使用介绍 简介 是 环境下常用的流处理工具, 可以处理字符流, 文件或者二进制文件流。 各个 发行版都会配备 及其衍生的命令工具, 因此, 掌握该工具的基本用法, 足以满足我们日常的许多文本处理的任务。 以下采用示例的方式对 进行基本用法介绍. 如果有兴趣了解更高级的使用技巧, 本文采用
linux下开机自启: 在/etc/init.d目录下新建文件elasticsearch 并敲入shell脚本: 注意, 前两行必须填写,且要注释掉。 第一行为shell前行代码,目的告诉系统使用shell。 第二行分别代表运行级别、启动优先权、关闭优先权,且后面添加开机服务会用到。 shell脚本
1、因为在centos7中/etc/rc.d/rc.local的权限被降低了,所以需要赋予其可执行权 chmod +x /etc/rc.d/rc.local 2、赋予脚本可执行权限假设/usr/local/script/autostart.sh是你的脚本路径,给予执行权限 chmod +x /usr
最简单的查看方法可以使用ls -ll、ls-lh命令进行查看,当使用ls -ll,会显示成字节大小,而ls- lh会以KB、MB等为单位进行显示,这样比较直观一些。 通过命令du -h –max-depth=1 *,可以查看当前目录下各文件、文件夹的大小,这个比较实用。 查询当前目录总大小可以使用d