OpenWrt 路由器系统配置教程

OpenWrt 是全球最流行的开源路由器操作系统,为数百种硬件路由器和软路由设备提供完全可定制的 Linux 路由固件。它将消费级路由器转变为功能强大的网络设备,支持高级路由协议、流量整形、广告过滤等功能。OpenWrt 也可以在搬瓦工 VPS 的虚拟机中运行,用于学习和测试网络配置。

一、OpenWrt 概述

  • 高度可定制:通过 opkg 包管理器安装数千种软件包。
  • LuCI Web 界面:功能完善的图形管理界面。
  • UCI 配置系统:统一的命令行配置接口。
  • 广泛硬件支持:支持上千款路由器型号。
  • 活跃社区:大量文档和社区支持。

二、在虚拟机中安装 OpenWrt

# 下载 x86_64 虚拟机镜像
wget https://downloads.openwrt.org/releases/23.05.5/targets/x86/64/openwrt-23.05.5-x86-64-generic-ext4-combined.img.gz
gunzip openwrt-23.05.5-x86-64-generic-ext4-combined.img.gz

# 转换为 QEMU 格式并扩展磁盘
qemu-img convert -f raw -O qcow2 openwrt-23.05.5-x86-64-generic-ext4-combined.img openwrt.qcow2
qemu-img resize openwrt.qcow2 512M

三、基本网络配置

3.1 使用 UCI 配置网络

# 查看当前网络配置
uci show network

# 配置 LAN 接口
uci set network.lan.ipaddr='192.168.1.1'
uci set network.lan.netmask='255.255.255.0'

# 配置 WAN 接口(DHCP)
uci set network.wan.proto='dhcp'

# 配置 WAN 接口(静态 IP)
uci set network.wan.proto='static'
uci set network.wan.ipaddr='10.0.0.2'
uci set network.wan.netmask='255.255.255.0'
uci set network.wan.gateway='10.0.0.1'
uci set network.wan.dns='8.8.8.8 8.8.4.4'

# 提交并应用
uci commit network
/etc/init.d/network restart

3.2 配置 DNS

# 配置 dnsmasq
uci set dhcp.@dnsmasq[0].server='8.8.8.8'
uci add_list dhcp.@dnsmasq[0].server='1.1.1.1'
uci commit dhcp
/etc/init.d/dnsmasq restart

四、防火墙配置

# 查看防火墙规则
uci show firewall

# 添加端口转发规则
uci add firewall redirect
uci set firewall.@redirect[-1].name='SSH Forward'
uci set firewall.@redirect[-1].src='wan'
uci set firewall.@redirect[-1].src_dport='2222'
uci set firewall.@redirect[-1].dest='lan'
uci set firewall.@redirect[-1].dest_ip='192.168.1.100'
uci set firewall.@redirect[-1].dest_port='22'
uci set firewall.@redirect[-1].proto='tcp'
uci set firewall.@redirect[-1].target='DNAT'
uci commit firewall
/etc/init.d/firewall restart

# 添加自定义防火墙规则
uci add firewall rule
uci set firewall.@rule[-1].name='Allow-ICMP'
uci set firewall.@rule[-1].src='wan'
uci set firewall.@rule[-1].proto='icmp'
uci set firewall.@rule[-1].target='ACCEPT'
uci commit firewall

五、软件包管理

# 更新软件源
opkg update

# 安装 LuCI Web 界面
opkg install luci luci-i18n-base-zh-cn

# 安装常用软件包
opkg install tcpdump iperf3 mtr bind-dig
opkg install luci-app-sqm        # 流量整形
opkg install luci-app-adblock     # 广告过滤
opkg install luci-app-ddns        # 动态 DNS

六、配置无线网络

# 配置 WiFi
uci set wireless.radio0.disabled='0'
uci set wireless.default_radio0.ssid='MyNetwork'
uci set wireless.default_radio0.encryption='psk2'
uci set wireless.default_radio0.key='WiFiPassword123'
uci commit wireless
wifi reload

七、高级功能

7.1 VLAN 配置

uci set network.@switch_vlan[0].vlan='1'
uci set network.@switch_vlan[0].ports='0t 2 3 4 5'
uci add network switch_vlan
uci set network.@switch_vlan[-1].device='switch0'
uci set network.@switch_vlan[-1].vlan='10'
uci set network.@switch_vlan[-1].ports='0t 1'
uci commit network

7.2 策略路由

opkg install ip-full
# 创建路由表
echo "100 custom" >> /etc/iproute2/rt_tables
ip rule add from 192.168.1.0/24 table custom
ip route add default via 10.0.0.1 table custom

八、系统维护

# 备份配置
sysupgrade -b /tmp/backup-openwrt.tar.gz

# 恢复配置
sysupgrade -r /tmp/backup-openwrt.tar.gz

# 系统升级
sysupgrade -v openwrt-new-firmware.img

总结

OpenWrt 将普通路由器转变为专业的网络设备,是学习网络技术的绝佳平台。无论是实际的路由器刷机还是在虚拟环境中实验,掌握 OpenWrt 都能显著提升你的网络运维能力。配合 GNS3 可以搭建更复杂的网络拓扑实验。选购搬瓦工 VPS 请参考 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的优惠,通过 bwh81.net 进入官网购买。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。