第 9 節

Other operations

0瀏覽次數0訪問次數--跳出率--平均停留

How to connect the industrial computer to Wi-Fi

When our industrial computer moves from one environment to another, and we haven't connected to the Wi-Fi in the new environment, how can we get the industrial computer online without a screen?

Navigate to the hard drive directory of the industrial computer, enter /etc/netplan/, where there are many yaml files. Find the one that best matches the format below:

network: 
  version: 2 
  wifis: 
    NM-6f414fe0-2658-48ff-89ee-c7981b87bc96: 
      renderer: NetworkManager 
      match: 
        name: "wlan0" 
      dhcp4: true 
      dhcp6: true 
      access-points: 
        "EMIS_Vinci_Robocon_5G": 
          auth: 
            key-management: "psk" 
            password: "vinci666" 
          networkmanager: 
            uuid: "6f414fe0-2658-48ff-89ee-c7981b87bc96" 
            name: "EMIS_Vinci_Robocon_5G" 
            passthrough: 
              wifi-security.auth-alg: "open" 
              ipv6.addr-gen-mode: "default" 
              ipv6.ip6-privacy: "-1" 
              proxy._: "" 
      networkmanager: 
        uuid: "6f414fe0-2658-48ff-89ee-c7981b87bc96" 
        name: "EMIS_Vinci_Robocon_5G"

Change it to the new Wi-Fi name and password:

network:
  version: 2
  wifis:
    wlan0:
      dhcp4: true
      dhcp6: true
      access-points:
        "EMIS_Vinci_Robocon_5G":
          password: "vinci666"

The industrial computer connects to the internet as soon as it boots up.

SSH remote development

Environment Setup

1. Hardware Preparation

First, you need an industrial computer (X86 small computer, professional industrial computer, Raspberry Pi, etc.).

The hardware carriers of control systems are diverse, with commonly used processors based on architectures such as ARM and x86, for example: PCs, industrial PCs, Raspberry Pi, NVIDIA Jetson, and so on. Different processors have their own advantages and disadvantages. PCs and industrial PCs offer powerful processing performance but come with high power consumption, large size, and poor flexibility, while embedded systems are the opposite. For us, we can autonomously select an appropriate control system based on various factors of the robot platform, such as electrical requirements, load capacity, space, and intended use.

Regardless of which processor is chosen, as long as Robot Operating System (ROS) or ROS2 is installed for robot development, there is no fundamental difference in usage for developers. In other words, as a software engineer, in some scenarios, there is no need to focus on hardware selection.

2. Setting a Static IP

When connecting remotely, regardless of the tool used, you need to locate the target host by its IP address and then log in using an account and password. Therefore, we first need to obtain that IP address. Since the IP is required for every connection, to ensure convenience and stability, it is best to set the target host's IP address as a static IP. The specific steps are as follows.

  1. Enter the settings interface

Start the connected host machine (a monitor or HDMI capture card is required during startup; after configuring SSH remote access, the monitor or HDMI capture card is no longer needed), and enter the settings interface.

  1. Configure the connected network

  1. Set a Static IP

Check the current IP address.

Set a static IP.

At this point, the IP configuration is complete.

Environment Setup

Concept

SSH (Secure Shell) is a versatile, powerful, software-based network security solution. Every time a computer sends data over the network, SSH automatically encrypts it. When the data reaches its destination, SSH automatically decrypts the encrypted data. The entire process is transparent, and using OpenSSH tools will enhance your system's security. SSH is easy to install and simple to use.

Implementation

The SSH implementation architecture is divided into two main parts: the client and the server. The client is the sender of data, and the server is the receiver. In this scenario, we need to send data from the local host to the remote host. Therefore, the local host needs to install and start an SSH client, while the remote host needs to install and start an SSH server. The specific implementation process is as follows:

  1. Install the SSH client on the local host and the SSH server on the remote host.
  2. Start the SSH service on the remote host.
  3. Log into a remote host from a local machine.
  4. Implement data transmission.
  5. Install SSH client and server

By default, the Ubuntu system already has the SSH client installed, so you only need to install the SSH server on the remote host. The installation command is as follows:

 sudo apt install openssh-server

If the SSH client needs to be installed manually, run the following command:

sudo apt install openssh-client
  1. Start the SSH service on the remote host

The command to start the SSH service on a remote host is as follows:

sudo /etc/init.d/ssh start

After starting, you can use the following command to check whether the service is running properly:

ps -e | grep ssh

If startup is successful, it will include the sshd program.

If you need to frequently use SSH for remote login, to simplify the process, you can configure the SSH service on the remote host to start automatically at boot. The command is as follows:

sudo systemctl enable ssh
  1. Local Remote Login

To log into a remote host, you can use the following command:

ssh -X 用户名@ip地址

Then, follow the prompt to enter the login password, and you will be successfully logged in.

If you want to exit, you can call the exit command:

exit

4. Implementing Data Transmission

By using SSH on the local host, you can easily upload or download data to and from a remote host by simply calling the relevant commands. The command format is as follows:

The format for the file upload command is as follows:

scp 本地文件路径 账号@ip:路径

The command format for uploading a folder is as follows:

scp -r 本地文件夹路径 账号@ip:路径

The download file command format is as follows:

scp 账号@ip:路径 本地文件夹路径

The download folder command format is as follows:

scp -r 账号@ip:路径 本地文件夹路径

Optimization

Every time you log in remotely, you need to enter a password, which is inconvenient. You can use SSH keys to simplify the login process, enabling passwordless login and improving operational efficiency. The idea is to generate a pair of public and private keys: the private key is stored locally, and the public key is uploaded to the server. Each time you log in, the local machine sends the private key to the server. If the server has a matching public key, it recognizes you as a legitimate user and establishes an SSH connection directly. The implementation involves only two steps:

  1. Generate a local key pair.
  2. Upload the public key to the remote host.

1. Generate a key pair

Local client generates public and private keys: (just press Enter all the way to accept defaults)

ssh-keygen

The command above will create a public-private key pair in the .ssh folder under the user's home directory.

  1. id_rsa (private key);
  2. id_rsa.pub (public key).

2. Uploading the Public Key

The upload instructions are as follows:

ssh-copy-id -i ~/.ssh/id_rsa.pub 账号@ip

This command uploads the local public key to the SSH directory on the remote host, where the authorized_keys file stores the public key content.

From now on, you won't need to enter a password for remote logins.

Optimizing with VSCode

In the previous section, we introduced the use of SSH remote connections. However, using SSH alone has some limitations. For example, editing file content requires the vi editor, and within a single terminal, you cannot edit multiple files simultaneously. This section will introduce a more practical feature — remote development using VSCode combined with plugins. This allows us to perform remote development in a graphical way, making it more convenient and faster than using SSH alone, and can greatly improve program development efficiency.

1. Preparation

VScode remote development relies on SSH. Please first configure the SSH remote connection according to the previous section.

2. Install the Remote Development Plugin for VSCode

Launch VScode, first click the Extensions button in the sidebar, then type Remote Development into the search bar of 扩展:商店 and click the plugin with the same name, and finally click 安装 in the display area on the right.

3. Configure Remote Connection

Step 1: Use the shortcut ctrl + shift + p to open the command input window, then enter Remote-SSH:Connect to Host.... From the pop-up list, select the option with the same name.

Step 2: After Step 1 is completed, a new command window will pop up as shown below. Select Add New SSH Host from the dropdown list.

Step 3: After completing step 2, a new command window will pop up. In it, enter: ssh -X ubuntu@192.168.43.164, where ubuntu needs to be replaced with your login username, and 192.18.43.164 should be replaced with the IP address of the remote host you are connecting to.

Step 4: Select the first option in the pop-up list after completing Step 3 (or simply press Enter) to finish the configuration. A confirmation message will appear once the configuration is successful.

4. Usage

Step 1: Continue using the shortcut ctrl + shift + p to open the command input window, and enter Remote-SSH:Connect to Host.... The list will then display the IP address configured in step 3. Select it directly, and after selection, VScode will open a new window.

Alternatively, you can click 远程资源管理器 in the sidebar, select the server you want to connect to from the pop-up server list, right-click it, and choose to establish a remote connection in the current window or a new window.

Step 2: Select 打开文件夹 under the 文件 menu bar, choose the folder you want to open from the pop-up list, and click OK.

Finally, we can achieve remote development just like working with local files.

进阶

使用hostname进行ssh

当一个设备他的IP一直变化,可以用hostname来进行连接。

可以先看你要远程开发的设备的hostname是什么?

hostname

比如输出的hostname是Dell-G15-5511

那么在这个设备上装上avahi-daemon:

sudo apt install avahi-daemon
sudo systemctl enable --now avahi-daemon

然后你就可以在同局域网的电脑上尝试:

ping Dell-G15-5511.local

如果能够ping通就可以了。

那么以后ssh就可以

ssh 用户名@Dell-G15-5511.local

Remote Desktop

Because X11 forwarding is far too inefficient, it's necessary to use remote desktop access to view Rviz2, Gazebo, and similar tools (if needed).

We choose to use VNC to view it.

  1. Install the VNC server:

Here, TigerVNC is used as an example for installation:

sudo apt install tigervnc-standalone-server tigervnc-common
  1. Set VNC password:

To set a password for the VNC user, run the following command:

vncpasswd
  1. Configure the VNC startup script:

Create a VNC configuration file and create a startup file in the .vnc folder within the user's home directory:

mkdir -p ~/.vnc
nano ~/.vnc/xstartup

In the opened editor, enter the following content (using GNOME as an example):

#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
gnome-session

Save and exit (in nano, press Ctrl + O to save, then press Ctrl + X to exit).

  1. Grant execute permission:

Set execute permission for the xstartup file:

chmod +x ~/.vnc/xstartup
  1. Start the VNC server:
vncserver -geometry 1920x1080 -localhost=0

The geometry option specifies the window size, and the localhost option is set to 0 to allow external connections. (Setting it to 1 only allows local connections.)

You will see output similar to :1, which indicates the display number of the VNC session. For example, if the output is :1, the VNC listening port is 5901 (5900 + display number).

  1. Check the enabled VNC servers:
vncserver -list

  1. Connect to the VNC server:

Use a VNC client (such as vncviewer) to connect to the VNC server by entering your server's IP address and port. For example, if the server IP is 192.168.31.10 and the display number is 1, you should connect to 192.168.31.10:1, or simply enter 192.168.31.10:5901.

  1. Stop the VNC server:

If you need to stop the VNC server, you can use the following command:

vncserver -kill :1

Replace :1 with the actual display number you are using.

开关图形界面

可以先看当前默认模式:

systemctl get-default

如果是graphical.target就是有图形模式,如果是multi-user.target就是命令行模式。

关掉图形界面(设置为命令行模式):

sudo systemctl isolate multi-user.target

默认关闭图形界面(默认进入命令行模式):

sudo systemctl set-default multi-user.target

Restart

sudo reboot

USB设备名持久化

USB port settings

在 Linux 中,USB 串口设备插拔之后,系统分配的设备节点可能会发生变化,例如:

/dev/ttyUSB0
/dev/ttyUSB1
/dev/ttyACM0
/dev/ttyACM1

如果程序、ROS 节点或者启动脚本直接写死 /dev/ttyUSB0,当设备重新插拔、增加新的 USB 设备或者系统启动顺序发生变化时,就可能出现端口号改变的问题。

因此,在机器人、无人车、嵌入式设备等场景中,一般需要给 USB 设备建立一个稳定的设备路径或固定别名。

常见方案主要有:

  1. 使用 Linux 自动生成的 /dev/serial/by-id/;
  1. 使用 Linux 自动生成的 /dev/serial/by-path/;
  1. 根据设备的 idVendor、idProduct、serial 等属性编写 udev 规则;
  1. 根据主机 USB 物理接口的位置编写 udev 规则;
  1. 对摄像头等设备使用 /dev/v4l/by-id/ 或 /dev/v4l/by-path/。

首先配置串口权限

Linux 下普通用户默认可能没有串口设备的访问权限,因此首先将当前用户加入 dialout 用户组。

#### 将当前用户加入 dialout 用户组
sudo usermod -aG dialout $USER

#### 临时在当前终端加载新的用户组
newgrp dialout

#### 查看当前用户组中是否存在 dialout
groups

如果输出中包含:

dialout

说明当前终端已经具有串口访问权限。

也可以注销当前桌面会话后重新登录,使用户组权限在整个用户会话中生效。


优先使用 Linux 自带的固定设备路径

在自己编写 udev 规则之前,建议首先查看系统是否已经为设备生成了稳定的符号链接。

对于 USB 串口设备:

ls -l /dev/serial/by-id/
ls -l /dev/serial/by-path/

For example:

usb-1a86_USB_Serial-if00-port0 -> ../../ttyUSB0
usb-STMicroelectronics_STM32_Virtual_ComPort_123456-if00 -> ../../ttyACM0

Among them:

  • /dev/serial/by-id/:主要根据设备本身的身份信息生成;
  • /dev/serial/by-path/:主要根据设备连接到主机的 USB 物理路径生成。

使用 /dev/serial/by-id/

如果设备具有稳定且唯一的序列号,通常优先使用:

/dev/serial/by-id/xxx

For example:

/dev/serial/by-id/usb-STMicroelectronics_STM32_Virtual_ComPort_123456-if00

即使对应的设备节点从:

/dev/ttyACM0

Change to:

/dev/ttyACM2

by-id 路径通常仍然保持不变。

因此,ROS 2 参数中可以直接写:

port: /dev/serial/by-id/usb-STMicroelectronics_STM32_Virtual_ComPort_123456-if00

这种方案最大的优点是:

  • 不依赖 ttyUSBn 或 ttyACMn 的编号;
  • USB 设备换到其他接口后,一般仍然能够识别;
  • 对于有唯一序列号的设备非常稳定;
  • 不需要自己编写 udev rules。

使用 /dev/serial/by-path/

如果设备没有唯一序列号,或者系统无法通过设备本身区分多台完全相同的设备,可以使用:

/dev/serial/by-path/

查看:

ls -l /dev/serial/by-path/

For example:

pci-0000:00:14.0-usb-0:3:1.0-port0 -> ../../ttyUSB0
pci-0000:00:14.0-usb-0:4:1.0-port0 -> ../../ttyUSB1

这种方式实际上是根据:

设备插在主机的哪个 USB 接口

来区分设备。

例如无人车有两个完全相同的雷达:

前雷达 -> 固定插在 USB 口 A
后雷达 -> 固定插在 USB 口 B

此时可以直接在程序中使用两个不同的 by-path 路径。

缺点是:

如果交换 USB 接口,设备对应的逻辑身份也会发生变化。


查看 USB 设备信息

使用 lsusb

接入 USB 设备之后,可以首先执行:

lsusb

For example:

Bus 001 Device 007: ID 1a86:7523 QinHeng Electronics CH340 serial converter

Among them:

1a86:7523

分别对应:

idVendor  = 1a86
idProduct = 7523

需要注意:

idVendor + idProduct 只能表示某一类 USB 产品,通常不能唯一表示某一个具体设备。

例如两块完全相同的 CH340:

CH340 A -> 1a86:7523
CH340 B -> 1a86:7523

它们的 idVendor 和 idProduct 是完全相同的。

因此:

  • 不同型号设备可以通过 idVendor + idProduct 区分;
  • 多个相同型号设备不能只依赖 idVendor + idProduct 区分。

查看设备完整属性

对于串口设备,可以使用:

udevadm info --query=property --name=/dev/ttyACM0

或者:

udevadm info --query=property --name=/dev/ttyUSB0

只查看常用信息:

udevadm info --query=property --name=/dev/ttyACM0 \
    | grep -E 'ID_(VENDOR_ID|MODEL_ID|SERIAL|SERIAL_SHORT|PATH|USB_INTERFACE_NUM)'

常见输出:

ID_VENDOR_ID=0483
ID_MODEL_ID=5740
ID_SERIAL=STMicroelectronics_STM32_Virtual_ComPort_123456
ID_SERIAL_SHORT=123456
ID_PATH=pci-0000:00:14.0-usb-0:3:1.0
ID_USB_INTERFACE_NUM=00

常用属性说明:

attributeMeaning
ID_VENDOR_IDUSB 厂商 ID

|ID_MODEL_ID|USB 产品 ID|

|ID_SERIAL|完整设备序列信息|

|ID_SERIAL_SHORT|USB 设备序列号|

|ID_PATH|设备连接到主机的物理路径|

|ID_USB_INTERFACE_NUM|USB Interface 编号|


查看 udev 可匹配属性

如果需要自己编写 udev 规则,可以使用:

udevadm info -a -p $(udevadm info -q path -n /dev/ttyACM0)

或者:

udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB0)

也可以使用:

udevadm info --attribute-walk --name=/dev/ttyUSB0

例如只查看 KERNELS:

udevadm info --attribute-walk --name=/dev/ttyUSB0 | grep KERNELS

这些信息可以用于编写自定义 udev rules。


根据 USB 设备型号绑定端口

Requirement

Ubuntu 系统中连接一台雷达和一台智能小车,两台设备型号不同,希望给它们建立固定别名。

For example:

雷达 -> /dev/mylidar
小车 -> /dev/mycar

查找 idVendor 和 idProduct

调用:

lsusb

假设得到:

雷达:10c4:ea60
小车:1a86:7523

Among them:

10c4 -> idVendor
ea60 -> idProduct

1a86 -> idVendor
7523 -> idProduct

编写 udev 规则

在:

/etc/udev/rules.d/

目录下创建规则文件,例如:

sudo vim /etc/udev/rules.d/99-usb-serial.rules

输入:

KERNEL=="ttyUSB*", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", MODE:="0777", SYMLINK+="mylidar"

KERNEL=="ttyUSB*", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", MODE:="0777", SYMLINK+="mycar"

Among them:

  • KERNEL:匹配 Linux 内核分配的设备名称;
  • ATTRS{idVendor}:USB 厂商 ID;
  • ATTRS{idProduct}:USB 产品 ID;
  • MODE:="0777":设置设备节点权限;
  • SYMLINK+="xxx":为设备建立固定符号链接。

For example:

/dev/mylidar -> /dev/ttyUSB0
/dev/mycar   -> /dev/ttyUSB1

使规则生效

Ubuntu 和 Fedora 都可以统一使用:

sudo udevadm control --reload-rules
sudo udevadm trigger

然后重新插拔 USB 设备。

也可以只触发 tty 子系统:

sudo udevadm control --reload-rules
sudo udevadm trigger --subsystem-match=tty

test
ls -l /dev/mylidar
ls -l /dev/mycar

或者:

readlink -f /dev/mylidar
readlink -f /dev/mycar

可能得到:

/dev/ttyUSB0
/dev/ttyUSB1

Disadvantages

这种方式只适合:

不同 USB 型号的设备。

如果接入两台完全相同型号的设备:

CH340 A -> 1a86:7523
CH340 B -> 1a86:7523

那么两个设备都会匹配同一条 udev rule。

因此:

KERNEL=="ttyUSB*", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", SYMLINK+="mycar"

不能可靠地区分两个完全相同的 USB 设备。

此时需要继续使用:

  • USB Serial Number;
  • /dev/serial/by-id/;
  • /dev/serial/by-path/;
  • 主机 USB 物理接口。

根据设备 Serial Number 绑定端口

如果多台设备型号相同,但是每台设备具有不同的 USB 序列号,那么这是最推荐的自定义 udev 绑定方式。

For example:

设备 A:
idVendor  = 1a86
idProduct = 55d4
serial    = 0001

设备 B:
idVendor  = 1a86
idProduct = 55d4
serial    = 0002

可以编写:

KERNEL=="ttyACM*", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="55d4", ATTRS{serial}=="0001", MODE:="0777", SYMLINK+="ttyACM_Lidar"

KERNEL=="ttyACM*", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="55d4", ATTRS{serial}=="0002", MODE:="0777", SYMLINK+="ttyACM_MCU"

这样:

/dev/ttyACM_Lidar
/dev/ttyACM_MCU

就可以稳定对应两台设备。

即使:

/dev/ttyACM0
/dev/ttyACM1

的编号发生交换,两个固定别名依然可以指向正确设备。


推荐检查方法
udevadm info --query=property --name=/dev/ttyACM0 | grep SERIAL

For example:

ID_SERIAL=STMicroelectronics_STM32_Virtual_ComPort_123456
ID_SERIAL_SHORT=123456

如果 ID_SERIAL_SHORT 是唯一的,就非常适合作为设备标识。


根据主机 USB 物理接口绑定端口

Requirement

无人车上安装前后两台完全相同型号的雷达。

两台雷达:

  • idVendor 相同;
  • idProduct 相同;
  • 没有唯一 Serial Number。

此时可以根据主机 USB 接口的位置区分。


方法一:直接使用 /dev/serial/by-path/

首先执行:

ls -l /dev/serial/by-path/

For example:

pci-0000:00:14.0-usb-0:3:1.0-port0 -> ../../ttyUSB0
pci-0000:00:14.0-usb-0:4:1.0-port0 -> ../../ttyUSB1

可以规定:

USB 口 0:3 -> 前雷达
USB 口 0:4 -> 后雷达

程序直接使用:

/dev/serial/by-path/pci-0000:00:14.0-usb-0:3:1.0-port0
/dev/serial/by-path/pci-0000:00:14.0-usb-0:4:1.0-port0

即可。


方法二:根据 KERNELS 创建自定义别名

查看第一台设备:

udevadm info --attribute-walk --name=/dev/ttyUSB0 | grep KERNELS

查看第二台设备:

udevadm info --attribute-walk --name=/dev/ttyUSB1 | grep KERNELS

假设对应:

第一台:
KERNELS=="1-1.3:1.0"

第二台:
KERNELS=="1-1.4:1.0"

则可以创建:

sudo vim /etc/udev/rules.d/99-lidar.rules

Write:

KERNEL=="ttyUSB*", KERNELS=="1-1.3:1.0", MODE:="0777", SYMLINK+="rplidar_front"

KERNEL=="ttyUSB*", KERNELS=="1-1.4:1.0", MODE:="0777", SYMLINK+="rplidar_back"

重新加载:

sudo udevadm control --reload-rules
sudo udevadm trigger

重新插拔之后:

ls -l /dev/rplidar_front
ls -l /dev/rplidar_back

Disadvantages

这种方式绑定的是:

主机 USB 接口

而不是:

USB 设备本身

因此设备必须插在固定 USB 接口。

如果前后两台雷达交换接口:

前雷达插到后雷达 USB 口

那么系统仍然会将该接口识别为:

/dev/rplidar_back

根据 ID_PATH 绑定端口

与使用 KERNELS 类似,也可以根据 ID_PATH 建立规则。

查看:

udevadm info --query=property --name=/dev/ttyUSB0 | grep ID_PATH

For example:

ID_PATH=pci-0000:00:14.0-usb-0:3:1.0

Yes, you can write:

SUBSYSTEM=="tty", ENV{ID_PATH}=="pci-0000:00:14.0-usb-0:3:1.0", MODE:="0777", SYMLINK+="rplidar_front"

另一个接口:

SUBSYSTEM=="tty", ENV{ID_PATH}=="pci-0000:00:14.0-usb-0:4:1.0", MODE:="0777", SYMLINK+="rplidar_back"

相比直接写 KERNELS,这种写法通常更加直观。


综合多个属性绑定端口

实际工程中,可以同时匹配多个属性。

For example:

KERNEL=="ttyACM*", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="55d4", ATTRS{serial}=="0001", MODE:="0777", SYMLINK+="ttyACM_Lidar"

KERNEL=="ttyACM*", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="55d4", ATTRS{serial}=="0002", MODE:="0777", SYMLINK+="ttyACM_MCU"

KERNEL=="ttyUSB*", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", MODE:="0777", SYMLINK+="ttyUSB_IMU"

也可以使用:

SUBSYSTEM=="tty", ENV{ID_VENDOR_ID}=="0483", ENV{ID_MODEL_ID}=="5740", ENV{ID_SERIAL_SHORT}=="12345678", MODE:="0777", SYMLINK+="ysu_mcu"

最终得到:

/dev/ysu_mcu

这种方式非常适合机器人项目。


推荐使用统一业务设备名称

对于机器人项目,不建议 ROS 2 节点直接使用:

/dev/ttyUSB0
/dev/ttyACM0

可以统一建立业务名称,例如:

/dev/ysu_mcu
/dev/ysu_gimbal
/dev/ysu_imu
/dev/ysu_lidar_front
/dev/ysu_lidar_back

For example:

SUBSYSTEM=="tty", ENV{ID_VENDOR_ID}=="0483", ENV{ID_MODEL_ID}=="5740", ENV{ID_SERIAL_SHORT}=="12345678", MODE:="0777", SYMLINK+="ysu_mcu"

SUBSYSTEM=="tty", ENV{ID_VENDOR_ID}=="10c4", ENV{ID_MODEL_ID}=="ea60", ENV{ID_SERIAL_SHORT}=="ABCDEF", MODE:="0777", SYMLINK+="ysu_imu"

SUBSYSTEM=="tty", ENV{ID_PATH}=="pci-0000:00:14.0-usb-0:3:1.0", MODE:="0777", SYMLINK+="ysu_lidar_front"

然后 ROS 2 参数写:

serial_port: /dev/ysu_mcu
imu_port: /dev/ysu_imu
lidar_port: /dev/ysu_lidar_front

这样后续更换设备节点编号时,ROS 2 配置文件完全不需要修改。


ttyUSB 与 ttyACM 的区别

并非所有 USB 串口设备都使用:

/dev/ttyUSBn

常见类型如下:

Device TypeKernel name exampleExplanation
USB 转串口芯片ttyUSB*CH340、CP210x、FTDI 等

|USB CDC ACM|ttyACM*|STM32 CDC、Arduino 等|

|主机物理串口|ttyS*|PC 板载 UART|

|USB 摄像头|video*|V4L2 摄像头|

|磁盘|sd*|USB 硬盘、U 盘|

|Input device|event*|键盘、鼠标、手柄|

|蓝牙串口|rfcomm*|Bluetooth RFCOMM|

所以 udev 规则中的:

KERNEL=="ttyUSB*"

并不是固定写法。

如果 STM32 CDC 对应:

/dev/ttyACM0

则应该匹配:

KERNEL=="ttyACM*"

也可以统一使用:

SUBSYSTEM=="tty"

再配合其他属性进行筛选。


USB 摄像头固定设备路径

USB 摄像头通常对应:

/dev/video0
/dev/video1
/dev/video2
...

而且一台摄像头有时会生成多个 /dev/videoN 节点。

因此不建议简单假设:

video0、video2、video4 一定是摄像头采集节点

因为设备节点编号与接口顺序可能发生变化。


查看系统生成的摄像头固定路径

执行:

ls -l /dev/v4l/by-id/
ls -l /dev/v4l/by-path/

For example:

/dev/v4l/by-id/usb-USB_Camera-video-index0
/dev/v4l/by-id/usb-USB_Camera-video-index1

如果摄像头具有唯一身份信息,推荐使用:

/dev/v4l/by-id/

如果需要按照固定 USB 接口区分:

/dev/v4l/by-path/

查看摄像头节点

可以安装:

sudo apt install v4l-utils

然后:

v4l2-ctl --list-devices

For example:

USB Camera:
    /dev/video0
    /dev/video1

也可以进一步查看某个节点支持的格式:

v4l2-ctl -d /dev/video0 --all

磁盘设备的固定路径

对于 USB 硬盘、U 盘、SSD 等,不建议直接依赖:

/dev/sda
/dev/sdb

Linux 同样提供:

/dev/disk/by-id/
/dev/disk/by-path/
/dev/disk/by-uuid/
/dev/disk/by-label/

For example:

ls -l /dev/disk/by-id/

因此,不同类型设备都遵循类似原则:

动态设备节点
        ↓
系统生成固定路径
        ↓
必要时自己编写 udev rule

使 udev 规则生效

修改:

/etc/udev/rules.d/*.rules

之后,Ubuntu 和 Fedora 均可以使用:

sudo udevadm control --reload-rules
sudo udevadm trigger

然后重新插拔设备。

对于串口设备,也可以:

sudo udevadm trigger --subsystem-match=tty

如果规则仍然没有生效,可以直接重启系统。


测试固定别名

查看符号链接:

ls -l /dev/ysu_mcu

查看最终实际设备:

readlink -f /dev/ysu_mcu

For example:

/dev/ttyACM1

即使之后重新插拔设备变成:

/dev/ttyACM3

如果 udev 规则匹配正确:

/dev/ysu_mcu

仍然会指向正确设备。


排查 udev 规则

如果规则没有生效,可以按照下面的方法检查。


检查规则语法
sudo udevadm verify /etc/udev/rules.d/99-usb-serial.rules

如果当前系统版本支持该命令,可以直接检查规则文件。


查看设备属性
udevadm info --query=property --name=/dev/ttyUSB0

或者:

udevadm info --attribute-walk --name=/dev/ttyUSB0

重点检查:

idVendor
idProduct
serial
ID_SERIAL
ID_SERIAL_SHORT
ID_PATH
KERNELS
SUBSYSTEM

监控设备插拔事件

执行:

udevadm monitor --environment --udev

然后重新插拔 USB 设备。

可以观察系统识别设备时生成的 udev 属性。


查看设备实际权限
ls -l /dev/ttyUSB0

For example:

crwxrwxrwx 1 root dialout ...

如果规则中使用:

MODE:="0777"

设备重新生成后会按照规则设置权限。


推荐选择顺序

USB 设备固定端口时,推荐按照下面的顺序选择。

                USB设备
                   │
                   ▼
       是否已有系统固定路径?
         /dev/serial/by-id
                   │
             ┌─────┴─────┐
             │           │
            有           无
             │           │
             ▼           ▼
          直接使用     是否存在唯一Serial?
                         │
                   ┌─────┴─────┐
                   │           │
                  有           无
                   │           │
                   ▼           ▼
             按Serial绑定   是否固定USB接口?
                               │
                         ┌─────┴─────┐
                         │           │
                        是           否
                         │           │
                         ▼           ▼
                    by-path/      尽量从设备固件
                    ID_PATH       层增加唯一Serial

简单来说:

能够识别设备本身,就按设备本身绑定;无法识别设备本身,就按设备连接的 USB 接口绑定。


最终推荐方案

情况一:单个设备,具有唯一 Serial

首选:

/dev/serial/by-id/

或者自己建立:

/dev/ysu_mcu

情况二:多个不同型号设备

可以使用:

idVendor + idProduct

建立自定义 udev rule。


情况三:多个相同设备,但 Serial 不同

使用:

idVendor + idProduct + serial

这是非常稳定的方案。


情况四:多个完全相同设备,没有唯一 Serial

使用:

/dev/serial/by-path/

或者:

KERNELS
ID_PATH

根据主机 USB 物理接口区分。


情况五:STM32 自定义 USB CDC

如果设备固件由自己控制,推荐在 USB Descriptor 中配置唯一 Serial Number。

这样 Linux 可以根据设备自身身份建立稳定路径,例如:

/dev/serial/by-id/usb-STMicroelectronics_STM32_Virtual_ComPort_xxxxx-if00

之后无论插在哪一个 USB 接口,都可以识别同一块控制板。


总结

Linux USB 设备的固定命名,本质上可以分为两类:

根据设备是谁
    ↓
by-id
Serial Number
idVendor + idProduct + serial


根据设备插在哪里
    ↓
by-path
ID_PATH
KERNELS

工程中建议优先级:

/dev/serial/by-id/
        ↓
唯一 Serial Number
        ↓
/dev/serial/by-path/
        ↓
自定义 udev rules

而在机器人项目中,最终可以统一建立:

/dev/ysu_mcu
/dev/ysu_gimbal
/dev/ysu_imu
/dev/ysu_lidar_front
/dev/ysu_lidar_back

之后 ROS 2、C++ 程序和启动文件全部使用这些固定名称,而不再依赖:

/dev/ttyUSB0
/dev/ttyUSB1
/dev/ttyACM0
/dev/ttyACM1

这样即使 USB 设备重新插拔、启动顺序发生变化或者系统重新分配端口编号,也不会影响上层程序。

Linux Partition GUI Tool

sudo apt install gparted
sudo dnf install gparted

DDNS

Mount the intranet webpage

You can mount local web pages on the router, or on other devices within the same local network segment, and even mount them locally.

Below, using an Android device as an example: since Android devices cannot directly open HTML files for now, we can host the HTML on a local server and then view it through a browser.

Since the Android system is based on the Linux kernel, we can download an app called Termux to run some Linux commands:

https://github.com/termux/termux-app

First, after opening the app, update the software cache:

pkg update

Then change the source.

nano $PREFIX/etc/apt/sources.list

Replace the content of this file with the following:

deb https://mirrors.bfsu.edu.cn/termux/termux-packages-24 stable main
  • If you are using the nano editor:
    • Save the file as Ctrl+O.
    • Confirm the filename according to Enter.
    • Press Ctrl+X to exit the editor.
  • If you are using the vim editor:
    • Press Esc to exit edit mode.
    • Press :wq to save and exit.

Next, update the software sources again:

pkg update && pkg upgrade

Install Python

pkg install python3

Accessing Device Storage via Termux

If the file is on an Android device, you can use Termux's file manager to access the device's storage:

termux-setup-storage
  1. Navigate to the directory containing the HTML file:
    1. Use the cd command to enter the directory where the HTML file is located:
cd ~/your_html_folder
  1. Start Python HTTP Server
    1. Run the following command to start a simple HTTP server:
    python3 -m http.server 8000
    
    1. This will start a web server on port 8000.
  • After starting the server, Termux will display log information, for example:
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
  • This indicates the server has started successfully.

Accessing an HTML file in a browser

  1. Accessing on the same device :
    1. Open the browser on your Android device (such as Chrome) and enter the following address:
    http://localhost:8000
    
    1. If the HTML file contains index.html, it will load automatically; otherwise, you need to manually click the file link.

RustDesk

RustDesk unattended remote control

Currently, RustDesk cannot perform unattended operations under Wayland, so we need to disable login passwords, screen lock, and sleep/standby entirely.

Set up KDE6 automatic login
  1. You can first check your SDDM type.
ls /usr/share/xsessions/
ls /usr/share/wayland-sessions/

Check which one outputs content; the folder that outputs content is the SDDM you have. For example, if xsessions outputs content, then it's the X11 SDDM. If wayland-sessions outputs content, then you have the Wayland SDDM.

  1. Edit the SDDM configuration:
sudo nano /etc/sddm.conf

At the very top of the file, there is a section called Autologin. Add the following content underneath it:

[Autologin]
User=你的用户名
Session=plasma.desktop   # 或 plasma.desktop(X11),方案一可用 Wayland
  1. Save and exit, then restart SDDM:
sudo systemctl restart sddm

✅ Automatically logs into the desktop after startup, no password required.

To disable the lock screen in KDE6 system settings:

Open System Settings → Screen Locking. Disable auto-lock Turn off the screen / Screen saver lock This way, RustDesk can directly control the desktop after booting, without being blocked by the lock screen.

Disable sleep system settings → Power Management

Disable "Sleep" or "Auto-Suspend" Ensure the computer stays online at all times, so RustDesk can be remotely controlled at any time. If you must use hibernation, you can disable it in the BIOS settings to ensure the device connects to the network immediately upon startup.

RustDesk self-hosted server

Install on the server side
Command line method
  1. Open the official website: https://rustdesk.com/docs/zh-cn/self-host/

alt text

  1. To access the server where you need to install RustDesk via SSH.
ssh tungchiahui@10.0.0.4

alt text

  1. Use the command from method two.

alt text

wget https://raw.githubusercontent.com/techahold/rustdeskinstall/master/install.sh
chmod +x install.sh
./install.sh

See the next section for the ./install.sh setup.

  1. Configuring and Viewing the KEY

Here, if you want to directly use a public IP, select 1)IP; if you are using DDNS or similar to bind a domain name to an IP address, select 2).

alt text

This selects 2)No.

alt text

Copy this KEY down, as you'll need it later during software configuration.

alt text

  1. Configure the firewall
    1. If you are a Debian-based system Install the firewall
    sudo apt install ufw
    

Configure the firewall ```bash sudo ufw allow 22

ufw allow 21114:21119/tcp
ufw allow 21116/udp
sudo ufw enable
```
2. If you are using a RHEL-based system

Install the firewall bash # 如果你是RHEL系 sudo dnf install firewalld sudo systemctl enable --now firewalld

Configure the firewall ```bash sudo firewall-cmd --add-port=22/tcp --permanent sudo firewall-cmd --reload

sudo firewall-cmd --add-port=21114-21119/tcp --permanent
sudo firewall-cmd --reload

sudo firewall-cmd --add-port=21116/udp --permanent
sudo firewall-cmd --reload

sudo systemctl enable --now firewalld
```
1Panel method
  1. Search for RustDesk in the panel and install it.

alt text

  1. Configure the firewall

alt text

Add a few ports. alt textalt textalt text

  1. View KEY alt text

Copy KEY

alt text

After setting up your own server, configure the RustDesk software.
  1. PC side

alt text

alt text

If "Ready" appears, the configuration is successful. alt text

  1. Mobile

alt text

alt text

Points to note

If you are using a home internal network server, you also need to configure the router to allow the corresponding ports.

Find the OpenWrt firewall settings and click Add. alt text

alt text

alt text

然后还有ipv6也要进行放行,那个debian一般默认ipv6不设防,但是会被上游openwrt防住,所以,要在openwrt里开启对应规则。

去防火墙 - 通信规则里添加下面俩配置。

Allow-RustDesk-IPv6-TCP
WAN → LAN
目标:你服务器的ipv6公网
TCP 21115-21119
动作:接受转发

Allow-RustDesk-IPv6-UDP
WAN → LAN
目标:你服务器的ipv6公网
UDP 21116
动作:接受转发

Only then can your server port be truly and successfully unblocked.

WireGuard

Introduction

WireGuard是用于在室外通过VPN来访问内网设备的一种工具.

这里我的教程将会教你如何在室外访问你家里的内网设备.

我的环境

内网

家庭内网网段:10.0.0.0/24

alt text

外部设备

如果你是在办公室等地方链接办公室的wifi,那么你需要知道室外内网网段,如果你直接用手机数据流量的话,那么什么都不需要知道.

室外内网网段:192.168.31.0/24

室外数据流量运营商:中国联通

Install

服务端-软路由端

SSH登录OpenWrt,或者用LuCI界面:

opkg update
opkg install wireguard-tools luci-proto-wireguard kmod-wireguard qrencode

装完刷新一下浏览器LuCI页面.

客户端-手机,电脑端

https://www.wireguard.com/install/

  1. Android

https://play.google.com/store/apps/details?id=com.wireguard.android&hl=zh&pli=1

  1. iOS

美区:https://apps.apple.com/us/app/wireguard/id1441195209

日区:https://apps.apple.com/jp/app/wireguard/id1441195209

台区:https://apps.apple.com/tw/app/wireguard/id1441195209

  1. Linux
    1. Fedora
     sudo dnf install wireguard-tools
    
    1. Ubuntu/Debian
sudo apt install wireguard

配置

服务端-初始化配置

在luci界面中,点击网络 → 接口 → 添加新接口

  1. 协议选 WireGuard VPN
  1. 名称填 wg0
  1. 点下一步,进到详情页

alt text

按照上图的步骤,依次配置.

这里解释一下.

监听端口:51820(可自定义,使用一个未被占用的端口即可,记住这个端口,不懂的话就照我的设置)

IP地址:10.10.10.1/24(VPN内网网段,跟你软路由的局域网段不能冲突,不懂的话就照我的设置)

你必须确保你的家庭内网网段和你的室外内网网段以及刚才填的那个IP地址不能属于同一网段.

比如我的家庭内网网段是10.0.0.0/24,而室外内网网段是192.168.31.0/24,VPN的网段10.10.10.0/24.

然后点击防火墙设置,选择WireGuard.

alt text

服务端-对端配置

这里配置的就是你的手机/笔记本作为客户端连进来的信息:

这个界面很方便,LuCI可以直接帮你生成客户端的密钥对并直接导出配置文件/二维码,不需要你手动在手机上先生成公钥再倒腾过来,效率高很多。按下面顺序填:

  1. description phone (或者你自己好记的名字,比如"iPhone17")
  1. 公钥 + 私钥 —— 直接点"生成新的密钥对"

点这个按钮,系统会自动帮你同时生成一对新的公钥+私钥,并自动填到"公钥"和"私钥"两个框里。

这个做法比你自己在手机App里生成再抄公钥过来省事得多,因为最后可以直接导出完整配置文件甚至二维码给手机扫码导入,私钥也是配置好的,不用手动输入。

  1. 预共享密钥(PSK) —— 建议点"生成预共享密钥"

这是可选的额外加密层,加上更安全(抗未来量子计算破解),点一下"生成预共享密钥"按钮自动填好即可,不需要手动操作。

  1. 允许的IP

点绿色+号,填:

10.10.10.2/32 这是分配给这台手机的隧道内网IP(如果以后要加第二台设备,比如笔记本,就用10.10.10.3/32,以此类推)。

  1. 为允许的IP创建路由 —— 勾选

这个决定了服务端要不要给这个IP自动建路由,勾上,否则服务端不知道怎么把去往10.10.10.2的包转发给这个peer。

  1. 对端地址 (Image 2)

这个字段填的是客户端的地址,但因为你的手机是移动设备、IP会变,服务端这边留空即可,不用填。(这个字段一般是给"两个固定IP的服务器互联"场景用的,你这种手机/笔记本客户端场景不需要)

  1. 对端端口

同理,留空或者保持默认51820占位符不用管,服务端这边这个字段对客户端场景没意义,跳过。

  1. 持续保持连接(Persistent Keepalive)

填:

25 因为手机大概率处在NAT环境下(无论是WiFi还是4G/5G),这个值能防止连接因为长时间无流量被NAT设备判定断开。

  1. 保存后 —— 点"生成配置..."

这一步是重点:全部填完先点右下角"保存",保存完之后再进来点 配置导出 那个"生成配置..."按钮,系统会:

自动生成一份完整的客户端配置文件(包含私钥、服务端公钥、Endpoint地址等全部信息)

通常还会显示一个二维码

手机WireGuard App直接扫这个二维码就能一键导入配置,完全不用你手动输入任何字段,最省事。

  1. 填完这些保存之后,点"生成配置"
![alt text](https://cdn.tungchiahui.cn/tungwebsite/assets/images/2024/03/30/1783172425569.webp)

1. 连接端点 —— 改成DDNS域名
没有ddns域名的请看[ddns教程](/wiki/2024-03-30-linux-jiao-cheng/0900-qi-ta-cao-zuo#ddns).
现在显示的是 `112.233.39.5` 这个具体的公网IPv4,这是系统自动探测到的当前公网IP,但建议改成:`ddns.xxxxxx.cn`
原因很简单:家庭宽带的公网IP大概率是动态的(如果你是中国移动,你完全不需要担心这件事情,你没有公网ipv4,你只有公网ipv6),过一阵子运营商重新分配IP后,这个写死的IP就失效了,配置文件也就废了。用DDNS域名的话,只要你路由器上的DDNS客户端正常更新,域名自动跟着IP变化解析,配置文件永久有效,不用来回改。
直接把这个下拉框里的值改成 `ddns.tungchiahui.cn` 即可,如果下拉框里没有这个选项,看能不能手动输入替换。
2.  允许的IP —— 这个要看你的使用需求,建议改
现在是 0.0.0.0/0 + ::/0,意思是全局代理模式:连上VPN后,手机/电脑的所有上网流量(刷网页、看视频、聊天全部)都会先绕道回你家里,再从家里的网络出去上网。
这跟最初的需求("在外面访问家里NAS")不太一致,全局模式的影响:
✅ 优点:所有流量都通过加密隧道,公共WiFi环境下更安全
❌ 缺点:手机所有网络流量的速度都取决于你家宽带的上传带宽,如果你家是常见的百兆宽带(上传可能才几十M甚至更低),刷视频、下载会明显变慢;而且家里NAT要承担所有流量的转发压力
如果你只是想访问家里内网的NAS/PVE/Debian,建议改成分离隧道模式,把这两行改成:
10.10.10.0/24
10.0.0.0/24
删掉 0.0.0.0/0 和 ::/0 这两行(点它们右边的红色×号),只保留上面这两个网段。这样只有"访问家里内网"这一类流量才走VPN隧道,其他正常上网流量不受影响,速度不受影响。
如果你确实希望VPN也当"全局代理"用(比如出门用境外的WiFi想用家里的网络环境访问某些内容),那就保留 0.0.0.0/0 不用改。
3. DNS服务器:10.0.0.1 —— 这是你家庭内网的软路由的LAN地址,连VPN后用它解析域名没问题,能顺带解析你家内网可能配置的一些本地域名
4. 地址:10.10.10.2/32 —— 这个就是你想给你的这个手机分配一个VPN网段

alt text

然后掏出手机的客户端,使用QRcode导入配置即可.

alt text

服务端-防火墙设置

需要加的规则

去 网络 → 防火墙 → 通信规则(Traffic Rules),点"添加":

alt text

名称:WireGuard(随便起)

协议:UDP 或者 TCP/UDP

源区域:wan

目标区域:设备(输入) —— 因为WireGuard监听的是软路由本机端口,不是转发给内网其他设备

目标端口:51820(跟你wg0接口"监听端口"那里填的一致)

操作:接受(Accept)

alt text

保存,应用。

客户端

拿iOS举例子

我这里使用数据流量来测试,使用的是中国联通4G.

像使用普通的VPN一样打开你导入的这个配置即可.

alt text

test

然后可以尝试访问你的内网设备,

比如我的

openwrt是10.0.0.1,

pve是10.0.0.2,

nas是10.0.0.5都可以被正常访问了.

alt text

alt text

alt text

将WireGuard交由Clash Meta管理(可选)

将纯WireGuard配置放入Clash Meta

如果手机或电脑上已经长期运行支持Mihomo / Clash Meta内核的客户端,再单独启动WireGuard客户端会比较麻烦,而且同一台移动设备通常不能同时启用两个VPN隧道。

因此,可以将WireGuard节点直接写入Clash Meta配置,让Clash Meta统一负责:

  1. 普通互联网流量直连;
  1. 家庭内网流量通过WireGuard访问;
  1. 根据规则自动判断流量应该走哪个出口。

本节只介绍不包含机场或其他代理节点的纯WireGuard配置。

配置完成后,不再需要打开独立的WireGuard客户端。使用该配置时,应关闭WireGuard App中的隧道,只打开Clash Meta。

查看WireGuard客户端参数

先打开已经能够正常使用的WireGuard客户端配置,需要记录以下信息:

|Clash Meta字段|WireGuard客户端中的对应字段|

|:---|:---| |server|Peer中的Endpoint域名或公网IP|

|port|Peer中的Endpoint端口|

|ip|Interface中的Addresses,只填写IPv4地址,不写/32|

|private-key|Interface中的Private key|

|public-key|Peer中的Public key,也就是WireGuard服务器公钥|

|pre-shared-key|Peer中的Preshared key|

|persistent-keepalive|Peer中的Persistent keepalive|

这里最容易弄错的是两个公钥:

  • Interface中的Public key是当前客户端的公钥,不能填入Clash Meta的public-key;
  • Peer中的Public key才是WireGuard服务器公钥,需要填入Clash Meta的public-key。

建议截图中保留以下字段所在位置:

  • Interface:Private key、Public key、Addresses
  • Peer:Public key、Preshared key、Endpoint、Allowed IPs、Persistent keepalive

建议完全遮挡Private key和Preshared key;

客户端公钥、服务器公钥可只保留开头6~10位;

个人DDNS域名可替换为ddns.example.com。

-->

WireGuard客户端参数位置

这一张图同时包含Interface和Peer两部分,已经足够说明各个字段应该从哪里获取,不需要再增加其他截图。

创建YAML配置

新建一个扩展名为.yaml的文件,例如:

home-wireguard.yaml

写入下面的模板:

# 仅让家庭内网流量通过WireGuard
# 普通互联网流量保持直连
# 启用本配置时,不要同时开启独立的WireGuard客户端

mixed-port: 7890
allow-lan: false
mode: rule
log-level: info
ipv6: true

tun:
  enable: true
  stack: mixed
  auto-route: true
  auto-detect-interface: true

proxies:
  - name: 家庭-WG
    type: wireguard

    # WireGuard客户端Peer中的Endpoint
    server: ddns.example.com
    port: 51820

    # WireGuard客户端Interface中的Addresses
    # 例如原配置为10.10.10.2/32,这里只填写10.10.10.2
    ip: 10.10.10.2

    # WireGuard客户端Interface中的Private key
    private-key: "gBzeTbZH..."

    # WireGuard客户端Peer中的Public key,即服务器公钥
    # 不要填写Interface中的客户端公钥
    public-key: "UUS0nrKE..."

    # WireGuard客户端Peer中的Preshared key
    pre-shared-key: "NGYvFsCle..."

    allowed-ips:
      - 0.0.0.0/0

    udp: true
    persistent-keepalive: 25
    mtu: 1380

rules:
  # 家庭局域网网段
  - IP-CIDR,10.0.0.0/24,家庭-WG,no-resolve

  # WireGuard隧道网段
  - IP-CIDR,10.10.10.0/24,家庭-WG,no-resolve

  # 其他网络流量保持直连
  - MATCH,DIRECT
修改模板中的参数

需要按照自己的WireGuard配置修改以下内容。

1. server

将:

server: ddns.example.com

改成WireGuard客户端Peer中Endpoint冒号前面的域名或公网IP。

例如Endpoint为:

ddns.example.com:51820

那么填写:

server: ddns.example.com

2. port

填写Endpoint冒号后面的端口,例如:

port: 51820

3. ip

填写WireGuard客户端Interface中的Addresses,但不要填写CIDR后缀。

例如原配置是:

10.10.10.2/32

Clash Meta中填写:

ip: 10.10.10.2

4. private-key

填写WireGuard客户端Interface中的Private key:

private-key: "客户端私钥"

5. public-key

填写WireGuard客户端Peer中的Public key,也就是服务器公钥:

public-key: "服务器公钥"

不要误填Interface中的Public key。Interface中的Public key属于当前客户端,Peer中的Public key才属于服务器。

6. pre-shared-key

填写WireGuard客户端Peer中的Preshared key:

pre-shared-key: "预共享密钥"

WireGuard密钥是Base64字符串,复制时不要漏掉末尾可能存在的=。

7. rules

将规则中的家庭局域网网段和WireGuard隧道网段改成自己的实际网段。

For example:

rules:
  - IP-CIDR,10.0.0.0/24,家庭-WG,no-resolve
  - IP-CIDR,10.10.10.0/24,家庭-WG,no-resolve
  - MATCH,DIRECT

Indicates:

  • 访问10.0.0.0/24家庭局域网时走WireGuard;
  • 访问10.10.10.0/24WireGuard隧道网段时走WireGuard;
  • 其他流量全部直连,不经过家庭宽带。
关于allowed-ips

模板中的:

allowed-ips:
  - 0.0.0.0/0

可以保持不变。

它表示这个WireGuard代理节点能够承载全部IPv4流量,但实际哪些流量交给WireGuard,仍然由最下面的rules决定。

For example:

rules:
  - IP-CIDR,10.0.0.0/24,家庭-WG,no-resolve
  - IP-CIDR,10.10.10.0/24,家庭-WG,no-resolve
  - MATCH,DIRECT

在这套规则下,只有家庭内网和WireGuard隧道网段走WireGuard,其他互联网流量继续直连。

需要注意,WireGuard服务端Peer中的Allowed IPs仍然应该是分配给该客户端的地址,例如:

10.10.10.2/32

不要把WireGuard服务端Peer中的Allowed IPs改成0.0.0.0/0。

导入并测试

将YAML文件导入支持Mihomo / Clash Meta内核的客户端,然后:

  1. 关闭独立的WireGuard客户端隧道;
  1. 在Clash Meta中选择刚才导入的配置;
  1. 使用“规则”模式;
  1. 打开Clash Meta连接开关;
  1. 访问家庭内网设备进行测试。

例如访问软路由:

http://10.0.0.1

或者在iOS文件App中连接NAS:

smb://10.0.0.5

同时可以进入WireGuard服务端,查看对应Peer的Latest Handshake。

如果开启Clash Meta后握手时间刷新,并且能够访问家庭内网设备,就说明配置成功。

Frequently Asked Questions

如果YAML能够启动,但无法产生WireGuard握手,优先检查:

  1. public-key是否误填成客户端公钥;
  1. private-key是否为当前客户端私钥;
  1. pre-shared-key是否完整,末尾的=是否遗漏;
  1. ip是否与服务端分配给该Peer的地址一致;
  1. Endpoint域名和端口是否正确;
  1. 软路由防火墙是否已经放行WireGuard端口;
  1. 独立WireGuard客户端是否仍然处于开启状态。

如果报错类似:

decode pre shared key: illegal base64 data

通常说明pre-shared-key复制不完整,常见原因是末尾的=被遗漏。

将WireGuard加入包含代理节点的YAML

Alist

Introduction

Install

WebUI

S3协议

开启S3

先进行使能S3协议。

去/opt/1panel/apps/alist/alist/data/data目录修改掉如下图的设置从false改成true。

"s3": {
  "enable": true,
  "port": 5246,
  "ssl": false
}

最后点击重启。

测试S3链接

在部署AList服务器本身敲

curl -i http://127.0.0.1:5246/

如果显示HTTP/1.1 200 OK,那证明是OK的。

后续我也把他反向代理成了一个域名s3.tungchiahui.cn,经测试,也没毛病:

配置S3

在AList的管理界面找到设置里的S3,然后点底下的Add来添加哪个路径为桶:

  • Name就是桶的名字
  • Path是你想让AList的哪个路径作为桶子的根目录
  • S3 access key id 是S3授权ID
  • S3 secret access key是S3授权密钥

测试S3

先准备好你的配置

Endpoint:
http://127.0.0.1:5246
或者
https://s3.tungchiahui.cn

Bucket:
ObjectStorage

Access Key:
AList 后台生成的 S3 Access Key ID

Secret Key:
AList 后台生成的 S3 Secret Access Key

Region:
us-east-1

使用一个可以使用S3的软件,比如我使用我做的VScode插件来进行测试。

插件链接如下:

https://marketplace.visualstudio.com/items?itemName=tungchiahui.md-image-uploader

我把配置填到了VScode中的settings.json使用我的插件进行了测试:

{
  "mdImageUploader.s3.accessKeyId": "2Vxxxxxxxxxxxxxxxxxxxxxx",
  "mdImageUploader.s3.secretAccessKey": "FWx6xxxxxxxxxxxxxxxxxx",
  "mdImageUploader.s3.endpoint": "https://s3.tungchiahui.cn",
  "mdImageUploader.s3.region": "us-east-1",
  "mdImageUploader.s3.bucket": "ObjectStorage",
  "mdImageUploader.cdnUrl": "https://cdn.tungchiahui.cn",
  "mdImageUploader.datedUploadPath": "/tungwebsite/assets/images",
  "mdImageUploader.undatedUploadPath": "/tungwebsite/assets/images/undate"
}

经过测试,文件已经成功被保存到了桶子里。

创建文件直链

去AList后台

管理
→ 设置
→ 全局
→ 关闭Sign All

如果开着 Sign All,关掉。因为只要全局 Sign All 开着,下面单独存储关签名也没用。AList 官方明确说 Sign All 会给所有文件直链添加签名。

然后找到你的存放AList的桶子的存储地点进行编辑。

关闭启用签名 / Enable signing。

然后保存。

然后你可以通过![](https://alist.tungchiahui.cn/d/AList文件的路径/文件名.webp)来访问文件了。

高阶操作(可选):

把cdn.tungchiahui.cn也和alist.tungchiahui.cn同源,然后在腾讯云edgeone里加速,在规则引擎里新加一个cdn路径前缀增加的规则。

  • HOST选择cdn.tungchiahui.cn
  • 操作选回源URL重写
  • 类型选增加路径前缀
  • 增加路径前缀选/d/AList文件的多余路径/

比如增加路径前缀我填的/d/NAS/ObjectStorage,

那么我用https://cdn.tungchiahui.cn/tungwebsite/assets/images/2024/03/30/1787159371048-e2e3749f.webp

将会同等于https://alist.tungchiahui.cn/d/NAS/ObjectStorage/tungwebsite/assets/images/2024/03/30/1787159371048-e2e3749f.webp,

都会回源到我的https://服务器ip地址/d/NAS/ObjectStorage/tungwebsite/assets/images/2024/03/30/1787159371048-e2e3749f.webp。

再设置一个缓存规则:

缓存规则可以这样理解:

  • 节点缓存 TTL:30 天

EdgeOne 会把图片缓存到边缘节点 30 天。后续访问优先从 CDN 节点返回,可以明显减少 AList、服务器 和 服务器宽带 的压力。

  • 强制缓存:关闭

保持关闭更稳妥。如果源站明确要求某些资源不要缓存,EdgeOne 会尊重源站设置,避免把特殊资源错误缓存。

  • Cache Key:查询字符串全部忽略

For example:

xxx.webp
xxx.webp?a=1
xxx.webp?a=2
xxx.webp?a=3

都会被认为是同一个缓存资源。这样可以防止别人不断修改 URL 参数绕过缓存,反复回源打你的 AList 和 服务器,而我这里文件还存储在NAS上,也会进一步防止他反复打我的NAS。

  • 忽略大小写:关闭

abc.webp 和 ABC.webp 会被当成两个不同文件,避免不同文件被错误合并到同一个缓存。

  • PHP / JSP / ASP / ASPX:不缓存

这些通常属于动态页面,单独设为不缓存可以防止动态内容被 CDN 长时间缓存。对于纯图片 CDN 来说基本属于防呆设置。

  • 图片浏览器缓存 TTL:1 天

jpg、png、gif、bmp、svg、webp 等图片会在用户浏览器中缓存 1 天。用户短时间内重复访问时,浏览器可以直接读取本地缓存,连 EdgeOne 都不用重新请求。

最终缓存链路大致如下:

浏览器
  ↓ 本地缓存 1 天
EdgeOne
  ↓ 节点缓存 30 天
AList
  ↓
NAS

绝大多数重复访问会停在浏览器或 EdgeOne,不会真正回源到家里的 服务器 和 NAS。

MinIO 自建对象存储

Introduction

|项目|腾讯 COS|自建 MinIO|

| ----------- | ------------- | ---------------------- | |对象存储|✅|✅|

|S3 API|✅|✅| |图片/附件|✅|✅|

|Bucket|✅|✅| |SDK 上传|✅|✅|

|私有对象/签名 URL|✅|✅|

|Versioning|✅|✅| |存储空间|腾讯提供|吃你服务器硬盘|

|带宽|腾讯提供|吃学校服务器出口带宽|

|高可用|腾讯负责|你负责|

|数据冗余|腾讯负责|你负责|

|硬盘损坏|云厂商处理|可能直接丢数据|

|运维|几乎没有|Docker、磁盘、备份、升级、安全都自己搞|

|cost|存储+流量|软件可以自建,但硬盘/服务器/带宽自己承担|

|CDN|很容易接腾讯 CDN/EO|MinIO 本身不是 CDN|

|可控性|General|非常高|

Install

首先你需要服务器上安装一个1panel.

进入商店,搜索minio.

点击安装.

大多数设置都默认即可,但账号密码要自己填写,这个端口外部访问这里,如果你的服务器在内网,可以选择打开,如果你的服务器的IP是一个公网IP,那么不建议你打开.

|Configuration item|建议|

| --------- | ------------------ | |Name|minio| |版本|选 1Panel 当前提供的稳定版本|

|用户|自己设一个,不要用太简单的|

|密码|自己设一个密码复杂点的|

|WebUI 端口|9001|

|S3 API 端口|9000|

|会话持续时间|默认 12h 即可|

|启用 WebUI|开|

|WebUI 动画|随意|

|端口外部访问|建议不开公网暴露,但如果测试的话,先开公网暴露也可以.|

最后点击安装.

配置MinIO

登陆MinIO

在浏览器输入服务器ip:9001:

比如我这个内网10.0.0.4:9001

然后登陆账号密码.

测试桶

创建一个桶,比如我这里叫test

再随便上传一张图片试试.

测试S3协议

S3协议的网址应该是服务器ip:9000:

比如我这个内网10.0.0.4:9000

在你电脑上测试 S3 API 健康状态:

curl -i http://10.0.0.4:9000/minio/health/live

如果显示HTTP/1.1 200 OK,那证明是OK的。

同样,测试下面这个是否S3就绪:

curl -i http://10.0.0.4:9000/minio/health/ready

如果显示HTTP/1.1 200 OK,那证明是OK的。

接入AList测试

这个一定要开着。

配置完后,可以返回文件列表,发现刚才的文件可以被正常查看了。

音乐页