1

自建Linux路由器--Debian篇

 2 years ago
source link: https://johnrosen1.com/2020/11/27/router/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

自建Linux路由器–Debian篇

之前许久之前写过一篇Ubuntu路由器的文章,但旧博客无了而我忘记了备份,就重新写一遍的罢。


对于路由器,其实能讲的并不多,网上许多人讨论的也往往不过是那么几个点罢了,比如 “为啥不用Openwrt?”,“Tp-link又不是不能用.jpg” , “没钱,买不起,打扰了”之类的。对此,我持保留意见,毕竟不是每个人都能去买台intel nuc当路由器使的,而且买得起的人里面,会玩的也确实不多。

那么,为什么我决定要自建Debian路由器呢?

  • 第一,相较于Openwrt,此方案对于不会安装Debian操作系统,Linux基本操作的人而言,基本就是一个不可能完成的任务,所以,有挑战性才好玩,不是吗?
  • 第二,Debian有着Openwrt不能比拟的自由度,你拥有机器的最高权限,理论上不受任何约束。
  • 第三,使用Debian就意味着,路由器不仅仅是一个路由器,而是一台服务器,任何Linux Server能安装的软体都能安装,极大地拓展了这台机器的玩法(当然前提是你知道怎么玩XD)。

安装Debian操作系统

前往Debian官网下载iso.

使用Rufus烧录安装介质(u盘).

设置首选开机项为上述安装介质.

接下来为关键一步! 如你需要pppoe拨号,请选择additional options –> expert模式 !

如不需要pppoe(比如为自动获取ip之类的),建议选择第一个安装选项。

接下来正常安装即可,如需pppoe拨号,请记得将所有ppp相关选项选中并建立连接


apt-get update
apt-get install sudo curl -y
sudo apt-get install bridge-utils -y

如显示 “包找不到”,请访问Debian apt source list generator修正你的apt source list.

获取网卡名称(找出eth0或者enp1s0这种物理网卡界面,后面会用到)

ip a

备份原有网络界面配置文件

cp -v /etc/network/{interfaces,interfaces.bak}

替换配置文件

sudo nano /etc/network/interfaces

配置文件 请勿照抄(示例为6网口,eth0为pppoe口),请自行修改!

# The loopback network interface
auto lo
iface lo inet loopback

# PPPoE connection
auto provider
iface provider inet ppp
pre-up /bin/ip link set eth0 up
provider provider

auto eth0

# Eth1 to Eth5 network switch #eth0等请替换成你自己的
allow-hotplug eth1
iface eth0 inet manual
pre-up ifconfig $IFACE up
pre-down ifconfig $IFACE down

allow-hotplug eth2
iface eth1 inet manual
pre-up ifconfig $IFACE up
pre-down ifconfig $IFACE down

allow-hotplug eth3
iface eth2 inet manual
pre-up ifconfig $IFACE up
pre-down ifconfig $IFACE down

allow-hotplug eth4
iface eth3 inet manual
pre-up ifconfig $IFACE up
pre-down ifconfig $IFACE down

allow-hotplug eth5
iface eth4 inet manual
pre-up ifconfig $IFACE up
pre-down ifconfig $IFACE down

# Setup an IP address for our bridge
auto br0
iface br0 inet static
bridge_ports eth1 eth2 eth3 eth4 eth5 #除拨号用的物理接口外都写上
address 192.168.1.253 #本机网络地址(局域网)
broadcast 192.169.1.255
netmask 255.255.255.0

重启network使配置生效

sudo service networking restart

验证配置生效

ip a

如看见br0则表示成功。


设置内核转发参数

sudo nano /etc/sysctl.conf

去掉以下两行的注释

net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1

顺手开启bbr,添加以下两行

net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
sudo sysctl -p

设置iptables转发参数

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE #自动获取ip请使用这个
### ppp0 ###
sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE #pppoe拨号请使用这个

安装iptables-persistent实现iptables开机自启。

sudo apt-get install iptables-persistent -y

全部选yes即可.


进阶配置指南

如果你完成了上述基础配置,那么路由器可以正常使用了,不过ip得手动配置,很麻烦,因此咱们需要一个DHCP服务器.

sudo apt-get install isc-dhcp-server -y
sudo nano /etc/dhcp/dhcpd.conf

根据你自己的网络情况配置即可。

重启服务使配置生效。

sudo systemctl restart isc-dhcp-server

这样局域网设备就能全自动获取ip等信息了,现在的路由器就实现了那些便宜货(tp-link这种)的功能。

什么?你想跟我讲wifi的事情?那么请你自己去买Ubut 之类的AP,我不想听,也不想管。

什么?你想跟我讲成本?你都来玩Debian路由器了,还差这么点AP钱?这好吗?这不好。


鉴于篇幅,我不打算继续讲更多功能了,其他可以实现的功能和普通的Linux server没有什么区别,请自己Google。

什么?你想让我写?不好意思,我咕了,再见 !


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK