网络配置之-nmcli

使用nmcli配置网络

NetworkManager是管理和监控网络设置的守护进程,设备既就是网络接口,连接是对网络接口的配置,一个网络接口可以有多个连接配置,但同时只有一个连接配置生效。

1 使用nmcli配置主机名

CentOS6 之前主机配置文件:/etc/sysconfig/network

CentOS7主机名配置文件:/etc/hostname,默认没有这个文件,通过DNS反向解析获取主机名,主机名默认为:localhost.localdomain

显示主机名信息

hostname

hostnamectl status

创建并修改文件并生效

hostnamectl set-hostname deskX.example.com

[root@centos73 ~]# hostnamectl set-hostname centos7.3.com
[root@centos73 ~]# hostname
centos7.3.com
[root@centos73 ~]# cat /etc/hostname
centos7.3.com

可以看到已经修改成功。

删除文件。回复默认主机名localhost.localdomain

2 常用命令

nmcli connection show 显示所有连接

nmcli connection show –active    显示所有活动的连接状态

nmcli connection show “ens33”    显示网络连接配置

nmcli device status            显示设备状态

nmcli device show ens33        显示网络接口属性

nmcli connection add help        查看帮助

nmcli connection reload        重新加载配置

nmcli connection down test2        禁用test2的配置,注意一个网卡可以有多个配置。

nmcli connection up test2        启用test2的配置

nmcli device disconnect ens33    禁用ens33网卡,物理网卡

nmcli device disconnect ens33    启用ens33网卡

图形工具

nm-connection-editor

字符工具

nmtui

nmtui-connect

nmtui-edit

nmtui-hostname

3 创建新连接配置

(1)创建新连接配置default,IP通过DHCP自动获取

[root@centos73 ~]# nmcli connection add con-name default type Ethernet ifname eth0
Connection 'default' (d62f98af-86ed-4735-80aa-2d80351765b2) successfully added.

删除连接

[root@centos73 ~]# nmcli connection delete default
Connection 'default' (d62f98af-86ed-4735-80aa-2d80351765b2) successfully deleted.

(2)创建新的连接配置test2,指定静态IP,不自动连接

[root@centos73 ~]# nmcli connection add con-name test2 ipv4.method manual ifname ens33 autoconnect no type Ethernet ipv4.addresses 172.25.100.100/24 gw4 172.25.100.1
Connection 'test2' (98385bf5-8cc3-47e3-a5f2-51e2d736be6d) successfully added.

参数说明:

con-name 指定连接名字,没有特殊要求,

ipv4.methmod 指定获取IP地址的方式

ifname 指定网卡设被名,既就是次配置所生效的网卡

autoconnect 指定是否自动启动

ipv4.addresses 指定IPv4地址

gw4指定网关

4查看/etc/sysconfig/network-scripts/目录

会看到多出一个文件ifcfg-test2。说明添加确实生效了。

/etc/sysconfig/network-scripts/ifcfg-test2

[root@centos73 ~]# ls /etc/sysconfig/network-scripts/ifcfg-*
/etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-ens34 /etc/sysconfig/network-scripts/ifcfg-test2

5 启用test2连接配置

[root@centos73 ~]# nmcli connection up test2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)
[root@centos73 ~]# systemctl restart network

看下是否生效

[root@centos73 ~]# nmcli device show ens33
GENERAL.DEVICE: ens33
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:0C:29:3A:00:82
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: test2
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/7
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 172.25.100.100/24
IP4.GATEWAY: 172.25.100.1
IP6.ADDRESS[1]: fe80::5a5b:2cc3:a448:e2e1/64
IP6.GATEWAY:

好了,基本的IP地址配置成功

6 修改连接设置

1)修改test2为自动启动

[root@centos73 ~]# nmcli connection modify test2 connection.autoconnect yes

(2)修改DNS为172.25.1.1

[root@centos73 ~]# nmcli connection modify test2 ipv4.dns 172.25.1.1

(3)添加DNS 114.114.114.114

[root@centos73 ~]# nmcli connection modify test2 +ipv4.dns 114.114.114.114

看下是否成功

[root@centos73 ~]# cat /etc/sysconfig/network-scripts/ifcfg-test2
TYPE=Ethernet
[……]
NAME=test2
UUID=98385bf5-8cc3-47e3-a5f2-51e2d736be6d
DEVICE=ens33
ONBOOT=yes
DNS1=172.25.1.1
IPADDR=172.25.100.100
PREFIX=24
GATEWAY=172.25.100.1
DNS2=114.114.114.114

可以看到均已生效

(4)删除DNS

[root@centos73 ~]# nmcli connection modify test2 -ipv4.dns 114.114.114.114

(5)修改IP地址

[root@centos73 ~]# nmcli connection modify test2 ipv4.addresses 200.100.100.100/24 gw4 200.100.100.1

(6)还可以添加多个IP

[root@centos73 ~]# nmcli connection modify test2 +ipv4.addresses 10.10.10.10/8

7 nmcli 命令和/etc/sysconfig/network-scripts/ifcfg-*文件的对应关系

ipv4.method manual                          BOOTPROTO=none 

ipv4.method auto                                 BOOTPROTO=dhcp

ipv4.addresses “192.0.2.1/24           IPADDR0=192.0.2.1

192.0.2.254″                                         PREFIX0=24 

                                                                GATEWAY0=192.0.2.254

ipv4.dns 8.8.8.8                                     DNS0=8.8.8.8

ipv4.dns-search example.com            DOMAIN=example.com

ipv4.ignore-auto-dns true                     PEERDNS=no

connection.autoconnect yes                ONBOOT=yes

connection.id eth0                                   NAME=eth0

connection.interface-name eth0          DEVICE=eth0

802-3-ethernet.mac-address . . .          HWADDR= . . .



原创文章,作者:linux is not unix,如若转载,请注明出处:http://www.178linux.com/74907

(0)
linux is not unixlinux is not unix
上一篇 2017-05-07 15:41
下一篇 2017-05-07 15:56

相关推荐

  • 由摩根定律引发的思考

    在Linux中,我们常常需要对一些条件进行判断,而对于多个条件的组合判断是基于摩根定律而进行的。所以理解摩根定律对于我们学习条件判断是很有必要的,下面我们就先介绍摩根定律进而引出其在一些具体场景上的应用。   一、摩根定律 在Linux中的条件判断中,摩根定律可以以下式来表示: !( A || B )=!A && !B !( A &…

    Linux干货 2016-08-16
  • n22-第一次作业-对Linux基础知识的回顾

    1、描述计算机的组成及其功能   根据冯诺依曼计算机体系结构模型,计算机由运算器、控制器、存储器、输入设备、输出设备五大部分组成。如上图:    运算器主要由算数逻辑单元ALU和通用寄存器组成,主要用于完成算术运算和逻辑运算。      控制器是整个计算机的控制中心,它的主要功能是周而复始…

    Linux干货 2016-08-15
  • redis主从复制(2)— replication buffer与replication backlog

    1、redis主从复制过程先不解释replication buffer和replication backlog,而先看看redis主从复制的过程。 redis的主从复制分为两个阶段: 1)同步(sync rdb snapshot):slave复制master的某时间点(t)的全量数据,t为master接收到slave的sync命令后执行rdb bgsave的…

    Linux干货 2016-04-05
  • Linux不同发行版之间的联系与区别

    Linux发行版有商业版、社区版两类。两者的共同点都是GNU/Linux发行版,区别在于商业版是由商业公司维护,社区版是社区组织维护。由于linux发行版都要遵循GPL协定,任何人都有使用、共享、修改软件源代码的自由,所以商业版的收入主要来自向企业提供支持服务,它的软件基本都是免费的。

    Linux干货 2018-03-04
  • linux基础1

    linux基础

    Linux干货 2018-03-08

评论列表(1条)

  • idcdeng
    idcdeng 2017-06-05 23:00

    我知道使用nmtui,不过和文本管理器有冲突。不能用