How to use the command 'lxc network' (with examples)
The ’lxc network’ command is used to manage networks for LXD containers. It provides various functionalities for creating, configuring, and managing networks. This article illustrates several common use cases of the ’lxc network’ command.
Use case 1: List all available networks
Code:
lxc network list
Motivation: This use case is helpful when you want to see a list of all available networks in LXD.
Explanation: The ’lxc network list’ command lists all the available networks in LXD.
Example output:
+------+----------+---------+-------------+--------------------------------------------+-------------+
| NAME | TYPE | MANAGED | DESCRIPTION | IPV4 CIDR | IPV6 CIDR |
+------+----------+---------+-------------+--------------------------------------------+-------------+
| lxdbr0 | bridge | YES | | 10.244.136.1/24 | none |
+------+----------+---------+-------------+--------------------------------------------+-------------+
Use case 2: Show the configuration of a specific network
Code:
lxc network show network_name
Motivation: This use case is useful when you need to view the configuration details of a specific network in LXD.
Explanation: The ’lxc network show’ command displays the configuration details of a specific network specified by ’network_name'.
Example output:
config:
ipv4.nat: "true"
ipv6.address: none
ipv6.nat: "false"
description: ""
name: lxdbr0
type: bridge
used_by:
- /1.0/containers/container_name
managed: "true"
status: Created
locations:
- none
Use case 3: Add a running instance to a specific network
Code:
lxc network attach network_name container_name
Motivation: This use case is helpful when you want to connect a running LXD container to a specific network.
Explanation: The ’lxc network attach’ command attaches a running LXD container specified by ‘container_name’ to the network specified by ’network_name’.
Example output: No output will be displayed if the command is successful.
Use case 4: Create a new managed network
Code:
lxc network create network_name
Motivation: This use case is useful when you need to create a new managed network in LXD.
Explanation: The ’lxc network create’ command creates a new managed network with the provided ’network_name'.
Example output: No output will be displayed if the command is successful.
Use case 5: Set a bridge interface of a specific network
Code:
lxc network set network_name bridge.external_interfaces eth0
Motivation: This use case is helpful when you want to specify the bridge interface for a specific network in LXD.
Explanation: The ’lxc network set’ command allows you to modify the configurations of a specific network. In this example, we are setting the ‘bridge.external_interfaces’ configuration for the network specified by ’network_name’ to ’eth0’.
Example output: No output will be displayed if the command is successful.
Use case 6: Disable NAT for a specific network
Code:
lxc network set network_name ipv4.nat false
Motivation: This use case is useful when you want to disable NAT for a specific network in LXD.
Explanation: The ’lxc network set’ command is used to modify the configurations of a specific network. In this example, we are disabling the ‘ipv4.nat’ configuration for the network specified by ’network_name’.
Example output: No output will be displayed if the command is successful.
Conclusion:
The ’lxc network’ command provides a comprehensive set of functionalities to manage networks for LXD containers. With the examples discussed in this article, you now have an understanding of how to list available networks, show network configurations, add instances to networks, create new networks, set bridge interfaces, and disable NAT for specific networks.