Press "Enter" to skip to content

OpenWrt通过DD安装到VPS

OpenWrt 是一个路由器系统 资源占用很小 整个准系统大小也就几十MB而已

如果只是做网络通信 用这个足够了

体积小的系统可以直接DD 如果是那些几百兆的不能直接DD 体积大的要进内存救援系统mfslinux才能DD不然会出错

最新openwrt镜像去这里找:

https://openwrt.org/toh/views/toh_fwdownload

https://firmware-selector.openwrt.org/

找X86/64的,然后如果你的主机需要efi就下载efi结尾的,教程中对应的镜像名字也要改,IP地址改成你实际的就行了,磁盘用fdisk -l 看你的磁盘在哪。

# 创建内存临时文件夹
mount -t tmpfs tmpfs /tmp/
cd /tmp

# 下载OpenWrt x86_64镜像
wget https://downloads.openwrt.org/releases/22.03.0-rc1/targets/x86/64/openwrt-22.03.0-rc1-x86-64-generic-ext4-combined.img.gz

# 解压
gzip -d openwrt-22.03.0-rc1-x86-64-generic-ext4-combined.img.gz

# 加载镜像
kpartx -av openwrt-22.03.0-rc1-x86-64-generic-ext4-combined.img

# 挂载镜像
mount /dev/mapper/loop0p2 /mnt

# 改SSH密码 (LUCI密码跟SSH一样)
[root@localhost ~]# openssl passwd -1 www.xiaocaicai.com
$1$RkGRRec2$baJKm1x7X/9Ee/RZKRPlX/

nano /mnt/etc/shadow
root:$1$RkGRRec2$baJKm1x7X/9Ee/RZKRPlX/::0:99999:7:::

# 设置网络连接(DHCP获取IPV4 IPV6)
cat <<EOF> /mnt/etc/config/network
config interface 'loopback'
        option device 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config interface 'lan'
        option device 'eth0'
        option proto 'dhcp'

config interface 'ipv6'
        option device 'eth0'
        option proto 'dhcpv6'
        option reqaddress 'try'
        option reqprefix 'auto'
EOF

# 设置网络连接(静态IPV4 IPV6地址 注意/24 /64这些CIDR代码要根据实际来定 可以小 但是不能大 CIDR代码要包含网关 ip addr show 看地址 ip -4 route 看V4网关 ip -6 route 看V6网关)
cat <<EOF> /mnt/etc/config/network
config interface 'loopback'
        option device 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config interface 'lan'
        option device 'eth0'
        option proto 'static'
        list ipaddr 'IPV4地址/24'
        option ipgw 'IPV4网关'
        list ip6addr 'IPV6地址/64'
        option ip6gw 'IPV6网关'
        list dns '2606:4700:4700::1111'
        list dns '2606:4700:4700::1001'
        list dns '1.1.1.1'
        list dns '1.0.0.1'
EOF

# 强制取消挂载
umount -l /mnt

# 取消加载镜像
kpartx -d openwrt-22.03.0-rc1-x86-64-generic-ext4-combined.img

# 让文件系统只读
echo 1 > /proc/sys/kernel/sysrq
echo u > /proc/sysrq-trigger

# DD
dd if=/tmp/openwrt-22.03.0-rc1-x86-64-generic-ext4-combined.img of=/dev/实际硬盘位置 bs=4M  status=progress

# 强制重启
echo 1 > /proc/sys/kernel/sysrq
echo b > /proc/sysrq-trigger

我尝试的镜像:

https://downloads.openwrt.org/releases/23.05.0/targets/x86/64/openwrt-23.05.0-x86-64-generic-ext4-combined-efi.img.gz

我根据上面教程安装的系统有问题

提示:

wrong efi loader signature

install xz-compressed data is corrupt

应该是什么文件损坏

我尝试精简脚本直接dd,不要帮镜像挂载,结果成功了:

# 创建内存临时文件夹
mount -t tmpfs tmpfs /tmp/
cd /tmp

# 下载OpenWrt x86_64镜像
wget https://downloads.openwrt.org/releases/23.05.2/targets/x86/64/openwrt-23.05.2-x86-64-generic-ext4-combined-efi.img.gz

# 解压
gzip -d openwrt-23.05.2-x86-64-generic-ext4-combined-efi.img.gz

# DD
dd if=/tmp/openwrt-23.05.2-x86-64-generic-ext4-combined-efi.img of=/dev/vda bs=4M  status=progress

# 强制重启
echo 1 > /proc/sys/kernel/sysrq
echo b > /proc/sysrq-trigger

我的虚拟主机支持vnc,我又通过vpc控制台去改了IP地址,如果想通过脚本自动配置的,应该要研究下挂载镜像修改镜像,应该是上面这个kpartx有毒。

手动编辑地址:

# vi /etc/config/network

config interface 'loopback'
        option device 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config interface 'wan'
    option ifname 'eth0'
    option proto 'static'
    option ipaddr '192.168.2.2'
    option netmask '255.255.255.0'
    option gateway '192.168.2.1'
    option dns '8.8.8.8 8.8.4.4'

重启网络,测速配置

/etc/init.d/network restart
ping google.com

发现还是无法通过外网地址来访问ssh和web地址:

配置防火墙:/etc/config/firewall

#vi /etc/config/firewall
# 增加配置
config rule
    option name 'Allow-WAN-SSH'
    option src 'wan'
    option dest_port '22'
    option target 'ACCEPT'
    option proto 'tcp'

config rule
    option name 'Allow-WAN-HTTP'
    option src 'wan'
    option dest_port '80'
    option target 'ACCEPT'
    option proto 'tcp'

config rule
    option name 'Allow-WAN-HTTPS'
    option src 'wan'
    option dest_port '443'
    option target 'ACCEPT'
    option proto 'tcp'

重启防火墙配置

/etc/init.d/firewall restart

参考资料:

https://hostloc.com/thread-1017280-1-1.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注