使用debian做路由网关

必要软件

今天利用debian做了一台路由网关, 并且能自动翻墙.

首先你需要一些东西: 一台已架设好openvpn/vpn的服务器, 一个已安装好debian系统的电脑或者虚拟机.

首先进入debian, 安装一些必要的软件

apt-get install openssl curl openvpn iptables bind9


bind9 nameserver is daemon

配置openvpn客户端

安装完成之后, 首先配置好openvpn客户端.

vim /etc/openvpn/client.conf

将你的openvpn地址以及相关key在client.conf中设定. 然后

openvpn /etc/openvpn/client.conf

测试连接是否正常. 如果可以正常访问twitter这些网站, 那进行下一步.

配置转发规则

创建如下一个sh文件, 保存为nat.sh

#!/bin/sh

LOCAL_NETWORK = "172.16.88.0/24" #你当前网络段地址

iptables -t -nat -A POSTROUTING -s $LOCAL_NETWORK -o tun0 -j MASQUERADE #将openvpn数据转发
iptables -t -nat -A POSTROUTING -s $LOCAL_NETWORK -o eth0 -j MASQUERADE

tun0, eth0根据自己机器上的配置进行修改.

创建localnetwork.sh

#!/bin/sh

# Local network
route add -net 172.16.88.0 netmask 255.255.255.0 gw 172.16.88.1

创建add_route.sh, 这个可以在google搜索到. openvpn自动翻墙脚本

#!/bin/sh

DEFAULT_GATEWAY='172.16.88.1'

#route list
route add -net 1.0.1.0 netmask 255.255.255.0 gw $DEFAULT_GATEWAY
#依次加入

创建完毕后, 依次执行net.sh, localnetwork.net和add_route.sh

然后检查

cat /proc/sys/net/ipv4/ip_forward

是否为1, 若不是请修改/etc/sysctl.conf, 然后执行sysctl -p

目前网关路由器已经设定完毕

最后一步

在网关路由服务器设定完毕后, 在自己的电脑上, 将网关以及dns都成debian网关路由器的ip地址就可以自动翻墙了. 🙂

网络路由器主要利用了netfilter/iptables的转发功能而创建出来的.

  1. hey your blog design is very nice, clean and fresh and with updated content, make people feel peace and I always like browsing your site.
    My website is Travel Tours.

  2. Great! thanks for the share!

Leave a Comment

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: