构建 OpenWRT 软路由系统

不学无术 现有 2 评论

在历尽艰辛攒了一个基于联想 M73 Tiny 小主机的软路由后,需要选择一个软路由系统。

在 RouterOS 和 OpenWRT 中最终选择了 OpenWRT 作为软路由的发行版,一来是开源自由度高一些,二来是免费使用的,无需使用授权破解等繁杂操作。

OpenWRT 除了官方版本外,还有两个常用分支,KoolShare 版和 Lean 版两个分支,在考虑稳定性等多方面因素后选择 Lean 版,文章后续为了简化描述,都简称为 L 版。

更新

构建

仓库中实际上已经有编译及使用的方法,对于熟悉 Linux 系统的用户来说,实际动手编译几遍即可对其有个较为深入的了解。

1.环境准备

  • 推荐 Debian 11 或 Ubuntu LTS

    小贴士:一定要在支持科学上网的环境内安装,虚拟机物理机皆可,如果实在没有可供科学上网的环境,可以考虑购买一个海外的服务器来进行编译,一定要选购硬盘大的,或者可以添加额外磁盘的。

2.安装依赖

sudo apt update -y
sudo apt full-upgrade -y
sudo apt install -y ack antlr3 asciidoc autoconf automake autopoint binutils bison build-essential \
bzip2 ccache cmake cpio curl device-tree-compiler fastjar flex gawk gettext gcc-multilib g++-multilib \
git gperf haveged help2man intltool libc6-dev-i386 libelf-dev libglib2.0-dev libgmp3-dev libltdl-dev \
libmpc-dev libmpfr-dev libncurses5-dev libncursesw5-dev libreadline-dev libssl-dev libtool lrzsz \
mkisofs msmtp nano ninja-build p7zip p7zip-full patch pkgconf python2.7 python3 python3-pip qemu-utils \
rsync scons squashfs-tools subversion swig texinfo uglifyjs upx-ucl unzip vim wget xmlto xxd zlib1g-dev

3.克隆源码(务必用普通用户)

git clone https://github.com/coolsnowwolf/lede

4.更新安装插件(记得取消 feeds.conf.default 文件内的 helloworld 行前注释 )

./scripts/feeds update -a
./scripts/feeds install -a
make menuconfig

5.下载dl库(用于交叉编译,必须全局科学上网)

make -j8 download V=s

6.编译(-j 后面是线程数。第一次编译务必用单线程进行编译)

make -j1 V=s

补充

Target Images

▲ 在 Target Images 中修改 Kernel partition size (in MB) 数值为 32 ,默认内核(引导)分区大小,适当增大可以优化更新内核流程。

Global build settings

▲ 在 Global build settings 中勾选 Select all kernel module packages by default 默认编译全部内核模块,这样防止安装额外的包时出现内核不支持的现象。

最后在 Luca 中勾选需要编译的程序即可。


常见问题

a) 部署常用包

执行命令

opkg install block-mount collectd cfdisk curl open-vm-tools rng-tools ss vim \
    luci-lib-ipkg \
    kmod-tcp-bbr kmod-br-netfilter \
    luci-i18n-base-zh-cn luci-i18n-firewall-zh-cn luci-i18n-opkg-zh-cn \
    luci-app-smartdns luci-i18n-smartdns-zh-cn \
    luci-compat \
    iptables-nft ip6tables-nft \
    sudo shadow-useradd shadow-usermod shadow-groupadd
## TCP 拥塞算法
opkg install kmod-tcp-scalable
sysctl net.ipv4.tcp_available_congestion_control
opkg install kmod-fs-btrfs
opkg install kmod-fs-exfat
opkg install kmod-fs-xfs

opkg install luci-lib-ipkg
# https://github.com/jerrykuku/luci-theme-argon/issues/151
opkg install adguardhome
# https://openwrt.org/docs/guide-user/services/dns/adguard-home

b) 解决 dnsmasq 冲突

安装部分代理软件时会出现此问题,是因为官方包中已经安装 dnsmasq,而软件依赖 dnsmasq-full,这两个包并非依赖关系,因此会冲突。

opkg remove dnsmasq
rm /etc/config/dhcp
opkg install dnsmasq-full

c) 旁路由模式

如果 OpenWrt 作为单臂路由或者从路由时,非顶层路由无需安装以下包,例如 UPnP 和 WoL 等。

luci-app-acl luci-i18n-acl-zh-cn
luci-app-upnp luci-i18n-upnp-zh-cn
luci-app-wol luci-i18n-wol-zh-cn

d) 配置单用户

如果禁用根用户,使用普通用户进行配置

groupadd -g 27 sudo
useradd -m -s /bin/ash kane
usermod -a -G sudo kane
visudo

e) 外置磁盘

格式化外置磁盘

cfdisk /dev/sdb

常用操作

a) 批量更新包

内置命令没有批量更新功能,需要使用组合命令:

# 更新软件列表
opkg update

# 更新所有 LUCI 插件
opkg list-upgradable | grep luci- | cut -f 1 -d ' ' | xargs opkg upgrade

# 更新所有软件,包括内核、固件等
opkg list-upgradable | cut -f 1 -d ' ' | xargs opkg upgrade

附录

参考链接

本文撰写于一年前,如出现图片失效或有任何问题,请在下方留言。博主看到后将及时修正,谢谢!
禁用 / 当前已拒绝评论,仅可查看「历史评论」。
  1. avatarImg Vinceyin

    来自尊贵的 MacBook Pro 16 的留言

    Chrome 87.0 macOS Big Sur
    IP 属地 未知
    1. avatarImg LoongSun
      @Vinceyin

      简单测试下邮件系统

      Safari 14.0.1 macOS Catalina
      IP 属地 未知