Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and use NMState Network Manager under Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to install and use NMState network manager under Linux. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

NMState is a network manager that allows you to configure hosts in a declarative manner. This means that you define the required configuration state through a northbound declarative API, which applies the configuration through the southbound provider provider.

Currently, the only provider supported by NMState is NetworkManager, which is the main service that provides network functions for Fedora Linux. However, other providers will gradually be added to NMState's development plan.

Configuration methods: imperative and declarative

Network management is sometimes a very complex task, depending on the size and diversity of the environment. In the early days of IT, network management relied on network administrators to manually execute commands on network devices. Today, the infrastructure, the code Infrastructure as Code (IaC), allows these tasks to be automated in different ways. Z there are basically two ways to do this: imperative or declarative.

In the imperative approach, you define "how" to achieve the desired configuration state. The declarative paradigm defines "what" is the required configuration state, so it is not sure which steps are required or in what order they must be performed. This approach is currently gathering more people, and you can find it on most of the management and orchestration tools currently in use.

NMState: a declarative tool

NMState is a network manager that allows you to configure hosts in a declarative manner. This means that you define the required configuration state through a northbound declarative API, which applies the configuration through the southbound provider provider.

Currently, the only provider supported by NMState is NetworkManager, which is the main service that provides network functions for Fedora Linux. However, other providers will gradually be added to NMState's development plan.

For further information about NMState, visit its project site or GitHub repository.

Installation

NMState is available on Fedora Linux 29 + and requires NetworkManager 1.26 or later to be installed and running on the system. The following is the installation on Fedora Linux 34:

$sudo dnf-y install nmstate... Output excerpt. Installed: NetworkManager-config-server-1:1.30.4-1.fc34.noarch gobject-introspection-1.68.0-3.fc34.x86_64 nispor-1.0.1-2.fc34.x86_64 nmstate-1.0.3-2.fc34.noarch python3-gobject-base-3.40.1-1.fc34.x86_64 python3-libnmstate-1.0.3-2 .fc34.noarch python3-nispor-1.0.1-2.fc34.noarch python3-varlink-30.3.1-2.fc34.noarchComplete!

In this way, you can use nmstatectl as a command-line tool for NMState. Please refer to nmstatectl-help or man nmstatectl for further information about this tool.

Use NMstate

First check the version of NMState installed on the system:

$nmstatectl version1.0.3

Check the current configuration of a network interface, such as the configuration of eth0:

$nmstatectl show eth02021-06-29 10 root DEBUG Async action 2815 30 root DEBUG NetworkManager version 1.30.42021-06-29 10 root DEBUG Async action 2815 31 root DEBUG Async action: Retrieve applied config: ethernet eth0 started2021-06-29 10 root DEBUG Async action 28 1531 root DEBUG Async action: Retrieve applied config: ethernet eth2 started2021-06-29 10 Fringe 2821532 root DEBUG Async action: Retrieve applied config: ethernet eth0 finished2021-06-29 10 14 28 root DEBUG Async action: Retrieve applied config: ethernet eth2 finished---dns-resolver: config: {} running: search: [] server:-192.168.122.1route-rules: config: [] routes: config: [] running:-destination: fe80::/64 metric: 100next-hop-address:''next-hop-interface: eth0 table-id: 254-destination: 0.0.0.0 next-hop-address 0 metric: 100 next-hop-address : 192.168.122.1 next-hop-interface: eth0 table-id: 254-destination: 192.168.122.0 enabled 24 metric: 100 next-hop-address:''next-hop-interface: eth0 table-id: 254 interfaces enabled-name: eth0 type: ethernet state: enabled:-ip: 192.168.122.238 prefix-length: 24 auto-dns: true auto-gateway: true auto-route-table- Id: 0 auto-routes: true dhcp: true ipv6: enabled: true address:-ip: fe80::c3c9:c4f9:75b1:a570 prefix-length: 64 auto-dns: true auto-gateway: true auto-route-table-id: 0 auto-routes: true autoconf: true dhcp: true lldp: enabled: false mac-address: 52:54:00:91:E4:4E mtu: 1500

As you can see above, this network configuration shows four main parts:

Dns-resolver: this part is the name server configuration of this interface. Route-rules: it illustrates the routing rules. Routes: it includes both dynamic and static routes. Interfaces: this section describes the ipv4 and ipv6 settings. Modify configuration

You can modify the desired configuration state in two modes:

Interactive: edit the interface configuration through nmstatectl edit. This command invokes the text editor defined by the environment variable EDITOR, so you can edit the network state in yaml format. After editing, NMState will apply the new network configuration unless there is a syntax error. File-based: uses the nmstatectl apply application interface configuration, which imports a required configuration state from a previously created yaml or json file.

The following sections show you how to use NMState to change your network configuration. These changes can cause damage to the system, so it is recommended that you perform these tasks on the test system or customer virtual machine until you have a better understanding of NMState.

The test system used here has two Ethernet interfaces, eth0 and eth2:

Example of $ip-br-4 alo UNKNOWN 127.0.0.1/8eth0 UP 192.168.122.238/24eth2 UP 192.168.122.108swap 24 interactive configuration mode

Use the nmstatectl edit command to change the MTU of the eth0 interface to 9000 bytes, as follows:

$sudo nmstatectl edit eth0---dns-resolver: config: {} running: search: [] server:-192.168.122.1route-rules: config: [] routes: config: [] running:-destination: fe80::/64 metric: 100 next-hop-address:''next-hop-interface: eth0 table-id: 254-destination: 0.0.0.0 metric: 100 next-hop-address: 192.168.122 .1 next-hop-interface: eth0 table-id: 254-destination: 192.168.122.0 enabled 24 metric: 100next-hop-address:''next-hop-interface: eth0 table-id: 254interfaces enabled-name: eth0 type: ethernet state: up ipv4: enabled: true address:-ip: 192.168.122.123 prefix-length: 24 auto-dns: true auto-gateway: true auto-route-table-id: 0 auto- Routes: true dhcp: true ipv6: enabled: true address:-ip: fe80::c3c9:c4f9:75b1:a570 prefix-length: 64 auto-dns: true auto-gateway: true auto-route-table-id: 0 auto-routes: true autoconf: true dhcp: true lldp: enabled: false mac-address: 52:54:00:91:E4:4E mtu: 9000

After saving and exiting the editor, NMState applies the new network expected state:

2021-06-29 11 dns-resolver': 2905726 root DEBUG Nmstate version: 1.0.32021-06-29 11V 29V 05726 root DEBUG Applying desire state: {'dns-resolver': {' config': {}, 'running': {' search': [], 'server': [' 192.168.122.1']}}, 'route-rules': {' config': []}, 'routes': {' config': [] 'running': [{' destination': 'fe80::/64',' metric': 102, 'next-hop-address':', 'next-hop-interface':' eth0', 'table-id': 254}, {' destination': '0.0.0.0A zero,' metric': 102, 'next-hop-address':' 192.168.122.1, 'next-hop-interface':' eth0' 'table-id': 254}, {' destination': '192.168.122.0 table-id': 24,' metric': 102, 'next-hop-address':', 'next-hop-interface':' eth0', 'table-id': 254}]},' interfaces': [{'name':' eth0', 'type':' ethernet', 'state':' up', 'ipv4': {' enabled': True 'address': [{' ip': '192.168.122.238,' prefix-length': 24}], 'auto-dns': True,' auto-gateway': True, 'auto-route-table-id': 0,' auto-routes': True, 'dhcp': True},' ipv6': {'enabled': True,' address': [{'ip':' fe80::5054:ff:fe91:e44e', 'prefix-length': 64}] 'auto-dns': True,' auto-gateway': True, 'auto-route-table-id': 0,' auto-routes': True, 'autoconf': True,' dhcp': True}, 'lldp': {' enabled': False}, 'mac-address':' 52 lac 5400 lav 91v E4v 4e' 'mtu': 9000}]}-output omitted-- 2021-06-29 11 root DEBUG Async action: Update profile uuid:2bdee700-f62b-365a-bd1d-69d9c31a9f0c iface:eth0 type:ethernet started2021-06-29 11 root DEBUG Async action: 05792 root DEBUG Async action: Update profile uuid:2bdee700-f62b-365a-bd1d-69d9c31a9f0c iface:eth0 type:ethernet finished

Now, use the ip command and eth0's configuration file to check that the MTU of eth0 is 9000 bytes.

$ip link show eth02: eth0: mtu 9000 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link/ether 52:54:00:91:e4:4e brd ff:ff:ff:ff:ff:ff altname enp1s0 $sudo cat / etc/NetworkManager/system-connections/eth0.nmconnection [sudo] password for admin: [connection] id=eth0uuid=2bdee700-f62b-365a-bd1d-69d9c31a9f0ctype=ethernetinterface-name=eth0lldp=0permissions= [ethernet] cloned-mac-address=52:54:00:91:E4:4Emac-address-blacklist=mtu=9000 [ipv4] dhcp-client-id=macdhcp-timeout=2147483647dns -search=method= auto[ IPv6] addr-gen-mode=eui64dhcp-duid=lldhcp-iaid=macdhcp-timeout=2147483647dns-search=method=autora-timeout=2147483647 [proxy] example of file-based configuration mode

Let's use a file-based approach to set a new configuration state. Here we disable the IPv6 configuration of the eth2 interface.

First, create a yaml file to define the expected state of the eth2 interface. Use nmstatectl show to save the current settings, and then use nmstatectl edit to disable IPv6.

$nmstatectl show eth2 > eth2.yaml$ vi eth2.yaml---dns-resolver: config: {} running: search: [] server:-192.168.122.1route-rules: config: [] routes: config: [] running:-destination: fe80::/64 metric: 101 next-hop-address:''next-hop-interface: eth2 table-id: 254-destination: 0.0.0.0 metric: 101 next-hop- Address: 192.168.122.1 next-hop-interface: eth2 table-id: 254-destination: 192.168.122.0According 24 metric: 101next-hop-address:''next-hop-interface: eth2 table-id: 254Interfaces up ipv4-name: eth2 type: ethernet state: up ipv4: enabled: true address:-ip: 192.168.122.108 prefix-length: 24 auto-dns: true auto-gateway: true auto-route-table -id: 0 auto-routes: true dhcp: true ipv6: enabled: false address:-ip: fe80::5054:ff:fe3c:9b04 prefix-length: 64 auto-dns: true auto-gateway: true auto-route-table-id: 0 auto-routes: true autoconf: true dhcp: true lldp: enabled: false mac-address: 52:54:00:3C:9B:04 mtu: 1500

After saving the new configuration, use it to apply the new state:

$vi eth2.yaml---dns-resolver: config: {} running: search: [] server:-192.168.122.1route-rules: config: [] routes: config: [] running:-destination: fe80::/64 metric: 101 next-hop-address:''next-hop-interface: eth2 table-id: 254-destination: 0.0.0.0 metric: 101 next-hop-address: 192.168.122 .1 next-hop-interface: eth2 table-id: 254-destination: 192.168.122.0 take 24 metric: 10110next-hop-address:''next-hop-interface: eth2 table-id: 254interfaces enabled-name: eth2 type: ethernet state: up ipv4: enabled: true address:-ip: 192.168.122.110 prefix-length: 24 auto-dns: true auto-gateway: true auto-route-table-id: 0 auto- Routes: true dhcp: false ipv6: enabled: false lldp: enabled: false mac-address: 52:54:00:3C:9B:04 mtu: 1500

You can check to see that the eth2 interface is not configured with any IPv6:

$vi eth2.yaml---dns-resolver: config: {} running: search: [] server:-192.168.122.1route-rules: config: [] routes: config: [] running:-destination: fe80::/64 metric: 101 next-hop-address:''next-hop-interface: eth2 table-id: 254-destination: 0.0.0.0 metric: 101 next-hop-address: 192.168.122 .1 next-hop-interface: eth2 table-id: 254-destination: 192.168.122.0 take 24 metric: 10110next-hop-address:''next-hop-interface: eth2 table-id: 254interfaces enabled-name: eth2 type: ethernet state: up ipv4: enabled: true address:-ip: 192.168.122.110 prefix-length: 24 auto-dns: true auto-gateway: true auto-route-table-id: 0 auto- Routes: true dhcp: false ipv6: enabled: false lldp: enabled: false mac-address: 52:54:00:3C:9B:04 mtu: 1500 temporary application changes

An interesting feature of NMState allows you to temporarily configure a desired network state. If you are satisfied with this configuration, you can submit it afterwards. Otherwise, it will be rolled back after the timeout (default is 60 seconds).

Modify the eth2 configuration in the previous example so that it has an IPv4 static address instead of being dynamically obtained through DHCP.

$vi eth2.yaml---dns-resolver: config: {} running: search: [] server:-192.168.122.1route-rules: config: [] routes: config: [] running:-destination: fe80::/64 metric: 101 next-hop-address:''next-hop-interface: eth2 table-id: 254-destination: 0.0.0.0 metric: 101 next-hop-address: 192.168.122 .1 next-hop-interface: eth2 table-id: 254-destination: 192.168.122.0 take 24 metric: 10110next-hop-address:''next-hop-interface: eth2 table-id: 254interfaces enabled-name: eth2 type: ethernet state: up ipv4: enabled: true address:-ip: 192.168.122.110 prefix-length: 24 auto-dns: true auto-gateway: true auto-route-table-id: 0 auto- Routes: true dhcp: false ipv6: enabled: false lldp: enabled: false mac-address: 52:54:00:3C:9B:04 mtu: 1500

Now, use the option no-commit to temporarily apply this configuration so that it is only valid for 30 seconds. This can be done by adding the option timeout. In the meantime, we will run the ip-br a command three times to see how the IPv4 address configured on the eth2 interface changes, and then the configuration will be rolled back.

$ip-br a & & sudo nmstatectl apply-- no-commit-- timeout 30 eth2.yaml & & sleep 10 & & ip-br a & & sleep 25 & & ip-br alo UNKNOWN 127.0.1 sleep 8:: 1/128eth0 UP 192.168.122.238 br alo UNKNOWN 24 fe80::5054:ff:fe91:e44e/64eth2 UP 192.168.122. 108Accord 242021-06-29 17 dns-resolver': 29 config': 18266 root DEBUG Nmstate version: 1.0.32021-06-29 17V 29 18 267 root DEBUG Applying desire state: {'config': {} 'running': {' search': [], 'server': [' 192.168.122.1']}}, 'route-rules': {' config': []}, 'routes': {' config': [], 'running': [{' destination': 'fe80::/64',' metric': 101, 'next-hop-address':', 'next-hop-interface':' eth2' 'table-id': 254}, {' destination': '0.0.0.0 table-id':,' metric': 101, 'next-hop-address':' 192.168.122.1, 'next-hop-interface':' eth2', 'table-id': 254}, {' destination': '192.168.122.0 table-id': 2400,' metric': 101, 'next-hop-address':' 'next-hop-interface':' eth2', 'table-id': 254}]},' interfaces': [{'name':' eth2', 'type':' ethernet', 'state':' up', 'ipv4': {' enabled': True, 'address': [{' ip': '192.168.122.110,' prefix-length': 24}], 'dhcp': False},' ipv6': {'enabled': False} 'lldp': {' enabled': False}, 'mac-address':' 52 5400 lac 3C lac 9Blo 04' 'mtu': 1500}]}-- output omitted-Desired state applied:---dns-resolver: config: {} running: search: [] server:-192.168.122.1route-rules: config: [] routes: config: [] running:-destination: fe80::/64 metric: 101next-hop-address:' 'next-hop-interface: eth2 table-id: 254-destination: 0.0.0.0Comp0 Metric: 101 next-hop-address: 192.168.122.1 next-hop-interface: eth2 table-id: 254-destination: 192.168.122.0 next-hop-address 24 metric: 101 next-hop-address:''next-hop-interface: eth2 table-id: 254 interfaces name: eth2 type: ethernet state: up ipv4: enabled: true address:-ip: 192.168.122.110 prefix-length: 24 dhcp: false ipv6: enabled: False lldp: enabled: false mac-address: 52:54:00:3C:9B:04 mtu: 1500Checkpoint: NetworkManager | / org/freedesktop/NetworkManager/Checkpoint/7lo UNKNOWN 127.0.0.1 1/128eth0 UP 8:: 1/128eth0 UP 192.168.122.238 1/128eth0 UP 24 fe80::5054:ff:fe91:e44e/64eth2 UP 192.168.122.110/24lo UNKNOWN 127.0.0.1/8:: 1/128eth0 UP 192.168.122.238/24 fe80::5054:ff:fe91:e44e/64eth2 UP 192.168.122.108/24

As you can see above, the IP address of eth2 has temporarily changed from 192.168.122.108 to 192.168.122.110, and then returned to 192.168.122.108 after the timeout.

This is the end of this article on "how to install and use NMState Network Manager under Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report