This document introduces the basic network configuration of the system. Additionally, to meet the requirement of modifying network configurations within programs, we provide a simple example of using the yaml-cpp library to modify network configurations.
1.1 netplan and yaml Files1.1.1 BackgroundThe runtime environment of this analysis system is Ubuntu 22.04. Since Ubuntu 18.04, the system no longer uses ifupdown for network configuration and has switched to netplan instead. Therefore, configuring a static IP in /etc/network/interfaces is ineffective; network configurations must be done in /etc/netplan/xxxx.yaml files. This means we need to understand the basic syntax of YAML files.
The following syntax does not cover all YAML syntax, but only the parts required for network configuration. For more detailed syntax, please refer to the official YAML website: https://yaml.org/
- Data structures use key-value pairs in the format
Key Name: Value(note: there must be a space after the colon). - Arrays have two expression methods: for example, when configuring dual IP addresses, each element is on a separate line starting with
-; when configuring DNS, square brackets[]are used, with elements separated by commas. As shown in the figure below.
- Indentation is used to indicate hierarchical relationships. Spaces (instead of tabs) are recommended for indentation, as tab lengths may vary across different systems.
- Case sensitivity applies to all letters.
Use the following command to view the storage path of the development board's network configurations; you will find 4 configuration files. Among them, 50-eth0-init.yaml is the netplan configuration for 【NET0】, and 51-eth1-init.yaml is the netplan configuration for 【NET1】.
ls /etc/netplan/The content is as follows:
Details about the configuration of 【DHCP】 and 【Static IP】 are shown in the figure below:
Note: Whether using 【DHCP】 or 【Static IP】, a network cable must be connected for the configuration to take effect, and the configured IP address can only be viewed via ifconfig after that.
Network configurations can be written separately or in the same YAML file. Below is an example (99_config.yaml) that combines the configurations for NET0 and NET1, setting eth0 to DHCP and eth1 to a static IP:
If modifications are made to the YAML file, execute the following commands to update the configuration:
sudo netplan generate
sudo netplan apply2. Quick Start2.1 Development Environment PreparationIf you are reading this document for the first time, please refer to《Getting Started Guide/Development Environment Preparation/Easy-EAI Compilation Environment Preparation and Update》,and deploy the compilation environment according to the relevant operations.
Execute the run script in the Ubuntu system on the PC to enter the EASY-EAI compilation environment, as follows:
cd ~/develop_environment
./run.shFirst, in the background terminal of the virtual machine, execute the following commands to create a directory for managing peripheral singleton source code:
cd /opt
mkdir -p EASY-EAI-Nano-TB/demoFirst, download the relevant singleton program from 【Baidu Netdisk】:
Link: https://pan.baidu.com/s/1Br608Hiff2Xs65PzWO_qWQ?pwd=1234
Extraction Code: 1234First, download the relevant singleton program from 【Baidu Netdisk】:Link: https://pan.baidu.com/s/1Br608Hiff2Xs65PzWO_qWQ?pwd=1234
For example, download the singleton program to: This PC\D:\BaiduNetdisk (no fixed rules; users can choose freely), as shown in the figure below.
Then copy the downloaded singleton program to the file system of the virtual machine, as shown in the figure below.
Finally, enter the corresponding example directory and execute the compilation command, as follows:
cd EASY-EAI-Nano-TB/demo/01_network
./build.shNotes:
- Since the dependent libraries are deployed on the board, the
/mntmount must be maintained during the cross-compilation process.
If the following error occurs, you need to install the libyaml-cpp-dev library 【on the development board】:
sudo apt-get install libyaml-cpp-dev2.3 Example ExecutionAccess the background of the board via serial port debugging or ssh调试,and navigate to the deployment location of the example, as follows:
cd /userdataThe command to run the example is as follows:
./test-ethernetThe effect of this demo is to modify the configuration of eth0 to 【Static IP: 192.168.1.170】. If the compilation environment is mounted to eth0 or you are debugging via SSH, and the previously DHCP-assigned IP is not 192.168.1.170, all connections will be disconnected after executing this example.
2.4 Execution EffectThe execution effect is as follows.
The configuration file is also modified accordingly:
The example is located at 01_network/test-ethernet/main.cpp.
The following code demonstrates a usage case for setting the IP address, DNS, and gateway parameters of eth0, for users' coding reference:
int main() {
// 加载YAML文件
YAML::Node config = YAML::LoadFile("/etc/netplan/50-eth0-init.yaml");
// =====================修改eth0的参数==========================
//把DHCP禁用
config["network"]["ethernets"]["eth0"]["dhcp4"] = "false";
// 修改eth0的IP地址
//方式1:固定写入第一个位置
config["network"]["ethernets"]["eth0"]["addresses"][0] = "192.168.1.170/24";
//方式2:追加插入
// 输入 IP 地址和子网掩码
std::string eth0_ip_str = "192.168.1.171";
std::string eth0_mask_str = "255.255.255.0";
std::string eth0_cidr = mask_transition_cidr(eth0_ip_str, eth0_mask_str);
//config["network"]["ethernets"]["eth0"]["addresses"].push_back(eth0_cidr);
// 修改eth0的DNS地址
YAML::Node addresses_eht0_dns = YAML::Load("[8.8.8.8,8.8.4.4]");//[8.8.8.8, 8.8.4.4]这是一个序列,需要转换
config["network"]["ethernets"]["eth0"]["nameservers"]["addresses"] = addresses_eht0_dns;
// 修改eth0的routes地址
config["network"]["ethernets"]["eth0"]["routes"][0]["to"] = "0.0.0.0/0";
config["network"]["ethernets"]["eth0"]["routes"][0]["via"] = "192.168.1.1";
config["network"]["ethernets"]["eth0"]["routes"][0]["metric"] = "100";
#if 0
// =====================修改eth1的参数=========================
config["network"]["ethernets"]["eth1"]["dhcp4"] = "false";
//方式1:固定写入第一个位置
config["network"]["ethernets"]["eth1"]["addresses"][0] = "192.168.1.172/24";
//方式2:追加插入
//std::string eth1_ip_str = "192.168.1.171";
//std::string eth1_mask_str = "255.255.255.0";
//std::string eth1_cidr = mask_transition_cidr(eth1_ip_str,eth1_mask_str);
//config["network"]["ethernets"]["eth1"]["addresses"].push_back(eth1_cidr);
YAML::Node addresses_eht1_dns = YAML::Load("[8.8.8.8,8.8.4.4]");
config["network"]["ethernets"]["eth1"]["nameservers"]["addresses"] = addresses_eht1_dns;
config["network"]["ethernets"]["eth1"]["routes"][0]["to"] = "0.0.0.0/0";
config["network"]["ethernets"]["eth1"]["routes"][0]["via"] = "192.168.1.1";
config["network"]["ethernets"]["eth1"]["routes"][0]["metric"] = "150";
#endif
// ==============将修改后的YAML文档写回到文件中==================
std::ofstream fout("/etc/netplan/50-eth0-init.yaml");
fout << config;
fout.close();
// =======================重启网卡===============================
system("sudo netplan generate");
system("sudo netplan apply");
return 0;
}





Comments