使用 systemd 创建可靠的后台服务
系统服务配置指南
1
创建 systemd 服务文件
创建并配置 VirtualHere 的 systemd 服务单元文件
执行以下命令创建服务文件
sudo nano /etc/systemd/system/virtualhere.service
粘贴以下配置内容
[Unit]
Description=VirtualHere USB Sharing Daemon
After=network.target
[Service]
Type=simple
ExecStart=/usr/virtualhere/vhusbdarm -b # -b 参数使其后台运行
Restart=always
RestartSec=5
User=root # 根据需求替换为其他用户
[Install]
WantedBy=multi-user.target
补充说明与高级配置
日志查看
实时查看服务日志输出
journalctl -u virtualhere -f
安全隔离用户
创建专用用户提高安全性
sudo useradd -r -s /usr/sbin/nologin virtualhere
然后修改服务文件中 User=virtualhere
自定义配置
修改默认端口(7575)或使用配置文件
ExecStart=/usr/virtualhere/vhusbdarm -b -c /etc/virtualhere/config
创建配置文件目录和文件
sudo mkdir /etc/virtualhere
echo "VIRTUALHERE_PORT=7580" | sudo tee /etc/virtualhere/config
防火墙配置
开放 VirtualHere 使用的端口
sudo ufw allow 7575/tcp
sudo ufw reload
卸载方法
完全移除 VirtualHere 服务
sudo systemctl stop virtualhere
sudo systemctl disable virtualhere
sudo rm /etc/systemd/system/virtualhere.service
sudo systemctl daemon-reload
文章评论