一、场景说明
本文适用于CentOS 7系统通过USB/IP协议远程挂载Windows端共享的USB设备(如CA证书、U盘、加密狗等),实现开机自动连接 + 端口检测 + 异常日志,解决手动连接繁琐、网络波动导致连接失败的问题。
二、环境准备
1. 服务端(Windows)
安装
usbip-win作为 USB/IP 服务端;共享目标USB设备(本文以BUSID=2-3的USB大容量存储设备为例);
放行防火墙3240端口(USB/IP默认端口)。
2. 客户端(CentOS 7)
内核版本:5.4.278-1.el7.elrepo.x86_64(需编译USB/IP内核模块);
依赖工具:gcc、make、kernel-devel、nc等。
三、CentOS 7端完整部署步骤
1. 配置阿里云YUM源(解决依赖安装问题)
cd /etc/yum.repos.d
mkdir backup
mv *.repo backup
cat > CentOS-aliyun-lhr.repo << 'EOF'
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
EOF
yum clean all
yum makecache fast2. 升级内核(适配USB/IP模块编译)
# 上传内核包 5.4.278.zip 到 /root 目录
yum install -y unzip
cd /root
unzip 5.4.278.zip
cd 5.4.278
# 安装新内核
yum localinstall -y kernel-lt-*.rpm
# 设置默认内核并重启
awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
grub2-set-default 0
grub2-mkconfig -o /boot/grub2/grub.cfg
reboot
# 验证内核版本
uname -r
# 预期输出:5.4.278-1.el7.elrepo.x86_643. 编译安装USB/IP工具及内核模块
(1)安装编译依赖
yum install -y gcc make kernel-devel-$(uname -r) elfutils-libelf-devel flex bison openssl-devel bc automake autoconf libtool libudev-devel glibc-devel libusb-devel pkgconfig(2)编译USB/IP工具
# 上传 linux-5.4.278.tar.xz 到 /root 目录
cp /root/linux-5.4.278.tar.xz /usr/src/
cd /usr/src/
tar -xf linux-5.4.278.tar.xz
cd /usr/src/linux-5.4.278/tools/usb/usbip/
# 生成配置文件
chmod +x autogen.sh
./autogen.sh
# 编译安装
./configure --prefix=/usr
make
make install
# 解决库文件依赖
ldconfig
# 验证 usbip 命令
usbip
# 预期输出:显示 usbip 命令用法(3)编译安装USB/IP内核模块
cd /usr/src/linux-5.4.278
# 复制当前内核配置
cp /boot/config-$(uname -r) .config
# 启用 USB/IP 相关配置
sed -i 's/# CONFIG_USBIP_CORE is not set/CONFIG_USBIP_CORE=m/' .config
sed -i 's/# CONFIG_USBIP_VHCI_HCD is not set/CONFIG_USBIP_VHCI_HCD=m/' .config
sed -i 's/# CONFIG_USBIP_HOST is not set/CONFIG_USBIP_HOST=m/' .config
# 准备模块编译环境
make modules_prepare
# 编译 USB/IP 模块
make M=drivers/usb/usbip modules
# 安装模块
make M=drivers/usb/usbip modules_install
# 加载模块并验证
modprobe usbip-core
modprobe usbip-host
modprobe vhci-hcd
lsmod | grep -E "usbip|vhci"
# 预期输出:显示 usbip-core、usbip-host、vhci-hcd 模块4. 设置内核模块开机自动加载
# 创建模块加载配置文件
vi /etc/modules-load.d/usbip.conf
# 写入以下内容(一行一个模块)
usbip-core
usbip-host
vhci-hcd
# 保存退出(ESC → :wq),验证配置
systemctl restart systemd-modules-load.service
lsmod | grep -E "usbip|vhci"
# 重启后再次验证
reboot
lsmod | grep -E "usbip|vhci"四、编写开机自动连接脚本
1. 创建自动连接脚本
cat > /usr/local/bin/usbip_auto_connect.sh << 'EOF'
#!/bin/bash
# 开机自动连接 Windows USB/IP服务端(带端口检测+重试)
# 配置项(替换为实际信息)
WIN_IP="112.226.52.222" # Windows USB/IP 服务端公网IP
BUS_ID="2-3" # Windows 共享的USB设备BUSID
USBIP_PORT="3240" # USB/IP 默认端口
MAX_RETRY="5" # 最大重试次数
RETRY_INTERVAL="5" # 每次重试间隔(秒)
LOG_FILE="/var/log/usbip_auto_connect.log" # 日志文件
# 日志输出函数
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}
# 第一步:等待网络就绪(基础等待)
log "开始执行USB/IP自动连接脚本,先等待10秒网络就绪..."
sleep 10
# 第二步:检测Windows端3240端口是否连通
log "开始检测Windows服务器 $WIN_IP:$USBIP_PORT 端口连通性..."
retry_count=0
while [ $retry_count -lt $MAX_RETRY ]; do
# 使用nc检测端口(超时3秒)
if nc -z -w 3 $WIN_IP $USBIP_PORT >> $LOG_FILE 2>&1; then
log "成功:$WIN_IP:$USBIP_PORT 端口连通"
break
else
retry_count=$((retry_count + 1))
log "失败:$WIN_IP:$USBIP_PORT 端口不通,第 $retry_count 次重试(共 $MAX_RETRY 次)"
sleep $RETRY_INTERVAL
fi
done
# 如果重试完仍不通,退出脚本
if [ $retry_count -eq $MAX_RETRY ]; then
log "ERROR: 多次重试后 $WIN_IP:$USBIP_PORT 端口仍不通,终止连接"
exit 1
fi
# 第三步:执行USB/IP连接
log "开始连接Windows USB/IP设备:$WIN_IP $BUS_ID"
if usbip attach --remote=$WIN_IP --busid=$BUS_ID >> $LOG_FILE 2>&1; then
log "SUCCESS: USB设备连接成功!"
else
log "ERROR: USB设备连接失败,请检查BUSID是否正确"
exit 1
fi
exit 0
EOF2. 配置脚本开机自启
# 1. 赋予脚本执行权限
chmod +x /usr/local/bin/usbip_auto_connect.sh
# 2. 安装nc(端口检测依赖)
yum install -y nc
# 3. 添加到rc.local开机自启
echo "/usr/local/bin/usbip_auto_connect.sh &" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local五、验证部署结果
1. 手动执行脚本验证
/usr/local/bin/usbip_auto_connect.sh
# 查看日志
cat /var/log/usbip_auto_connect.log
# 预期日志输出:
# [2026-03-03 20:43:54] 开始执行USB/IP自动连接脚本,先等待10秒网络就绪...
# [2026-03-03 20:44:04] 开始检测Windows服务器 112.226.52.222:3240 端口连通性...
# [2026-03-03 20:44:04] 成功:112.226.52.222:3240 端口连通
# [2026-03-03 20:44:04] 开始连接Windows USB/IP设备:112.226.52.222 2-3
# [2026-03-03 20:44:04] SUCCESS: USB设备连接成功!2. 检查 USB 设备挂载状态
# 查看USB/IP挂载端口
usbip port
# 预期输出:
# Imported USB devices
# ====================
# Port 00: <Port in Use> at High Speed(480Mbps)
# unknown vendor : unknown product (346d:5678)
# 1-1 -> usbip://112.226.52.222:3240/2-3
# -> remote bus/dev 002/003
# 查看系统识别的USB设备
lsusb
# 预期输出:
# Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
# Bus 001 Device 002: ID 346d:5678
# Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub3. 重启系统验证开机自动连接
reboot
# 重启后检查
usbip port
lsusb
cat /var/log/usbip_auto_connect.log六、问题排查
1. 核心排查方式
# 查看自动连接日志(关键)
cat /var/log/usbip_auto_connect.log
# 常见错误及解决:
# - 端口不通:检查Windows防火墙3240端口、usbipd服务是否启动;
# - BUSID错误:在Windows端执行 `usbipd list` 核对BUSID;
# - 模块加载失败:检查 /etc/modules-load.d/usbip.conf 配置,重新加载模块;
# - 库文件缺失:执行 `ldconfig` 刷新库缓存。2. Windows 端关键操作
powershell
# 查看共享设备
usbipd list
# 绑定共享设备(需--force解决过滤器冲突)
usbipd bind -b 2-3 --force
# 启动usbipd服务
usbipd -d七、核心总结
部署核心:CentOS 7需升级内核并编译USB/IP模块,Windows端共享设备时需处理过滤器兼容性(加 --force);
自动连接:脚本包含网络等待、端口检测、重试机制,确保开机稳定连接;
验证标准:
usbip port显示端口占用、lsusb识别设备、日志无报错即为部署成功;端口保障:Windows 端必须放行3240端口,公网访问需确保网络可达。
本文中使用的 Linux 5.4.278内核包、内核源码、usbip-win服务端程序等相关软件,均为实操环境配套文件。如需获取上述软件安装包,可直接联系我获取,我会提供完整可直接使用的文件,避免大家自行下载版本不匹配、编译失败等问题。
八、下载链接
九、原创声明
本文为 个人原创技术教程,完整记录 CentOS 7编译USB/IP、远程挂载 Windows USB设备、实现开机自动连接的全过程。
未经授权,禁止转载、搬运、洗稿及商用,如需引用请注明原文出处及作者。
本文旨在分享真实可复现的运维实践,帮助更多开发者与运维人员解决远程USB设备映射、加密狗 / CA证书 / U盘 远程调用等实际问题。
评论区