免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1304 | 回复: 0
打印 上一主题 下一主题

[网络管理] linux下面架设ADSL服务器概叙 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-04 18:28 |只看该作者 |倒序浏览
我使用linux作为上网共享服务器稳定运行1年了,基本是不管它,效果还可以,现在把我的架设方式描叙一下,给大家一个参考
机器是塞扬400,128M内存,无键盘,无显示器,两个网卡,安装好redhat linux后,在开机bios中电源管理中,设置为来电后自动启动(经常停电,无办法).
设置ADSL按照通常的方法,使用adsl-setup,设置好后,使用adsl-start启动adsl,然后在本机上是否连上,一般是没有问题的.
设置机器启动后自动启动adsl

rc.local文件设置如下


  1. #!/bin/sh
  2. #
  3. # This script will be executed *after* all the other init scripts.
  4. # You can put your own initialization stuff in here if you don't
  5. # want to do the full Sys V style init stuff.

  6. touch /var/lock/subsys/local
  7. adsl-start #启动adsl
  8. /root/firewall/firewall.sh #运行防火墙脚本

复制代码


防火墙脚本基本是采取iptables一样,但是自己根据需求小小改了一下

全文如下


  1. #!/bin/sh
  2. #
  3. # rc.firewall - DHCP IP Firewall script for Linux 2.4.x and iptables
  4. #
  5. # Copyright (C) 2001  Oskar Andreasson <bluefluxATkoffeinDOTnet>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; version 2 of the License.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program or from the site that you downloaded it
  18. # from; if not, write to the Free Software Foundation, Inc., 59 Temple
  19. # Place, Suite 330, Boston, MA  02111-1307   USA
  20. #

  21. ###########################################################################
  22. #
  23. # 1. Configuration options.
  24. #

  25. #
  26. # 1.1 Internet Configuration.
  27. #
  28. EXT_IP=`ifconfig|sed '/^eth/,/^ppp0/d'|sed '/^ *UP/,/$d/d'|cut -d: -f 2|cut -d ' ' -f 1`
  29. INET_IFACE="ppp0"



  30. #
  31. # 1.1.1 DHCP
  32. #

  33. #
  34. # Information pertaining to DHCP over the Internet, if needed.
  35. #
  36. # Set DHCP variable to no if you don't get IP from DHCP. If you get DHCP
  37. # over the Internet set this variable to yes, and set up the proper IP
  38. # address for the DHCP server in the DHCP_SERVER variable.
  39. #

  40. DHCP="no"
  41. #DHCP_SERVER="192.168.1.1"

  42. #
  43. # 1.1.2 PPPoE
  44. #

  45. # Configuration options pertaining to PPPoE.
  46. #
  47. # If you have problem with your PPPoE connection, such as large mails not
  48. # getting through while small mail get through properly etc, you may set
  49. # this option to "yes" which may fix the problem. This option will set a
  50. # rule in the PREROUTING chain of the mangle table which will clamp
  51. # (resize) all routed packets to PMTU (Path Maximum Transmit Unit).
  52. #
  53. # Note that it is better to set this up in the PPPoE package itself, since
  54. # the PPPoE configuration option will give less overhead.
  55. #

  56. PPPOE_PMTU="no"

  57. #
  58. # 1.2 Local Area Network configuration.
  59. #
  60. # your LAN's IP range and localhost IP. /24 means to only use the first 24
  61. # bits of the 32 bit IP address. the same as netmask 255.255.255.0
  62. #

  63. LAN_IP="192.168.1.1"
  64. LAN_IP_RANGE="192.168.1.0/24"
  65. LAN_BROADCAST_ADDRESS="192.168.1.255"
  66. LAN_IFACE="eth0"


  67. #
  68. # 1.3 DMZ Configuration.
  69. #

  70. #
  71. # 1.4 Localhost Configuration.
  72. #

  73. LO_IFACE="lo"
  74. LO_IP="127.0.0.1"

  75. #
  76. # 1.5 IPTables Configuration.
  77. #

  78. IPTABLES="/sbin/iptables"

  79. #
  80. # 1.6 Other Configuration.
  81. #

  82. ###########################################################################
  83. #
  84. # 2. Module loading.
  85. #

  86. #
  87. # Needed to initially load modules
  88. #

  89. /sbin/depmod -a

  90. #
  91. # 2.1 Required modules
  92. #

  93. /sbin/modprobe ip_conntrack
  94. /sbin/modprobe ip_tables
  95. /sbin/modprobe iptable_filter
  96. /sbin/modprobe iptable_mangle
  97. /sbin/modprobe iptable_nat
  98. /sbin/modprobe ipt_LOG
  99. /sbin/modprobe ipt_limit
  100. /sbin/modprobe ipt_MASQUERADE

  101. #
  102. # 2.2 Non-Required modules
  103. #

  104. #/sbin/modprobe ipt_owner
  105. #/sbin/modprobe ipt_REJECT
  106. /sbin/modprobe ip_conntrack_ftp
  107. /sbin/modprobe ip_conntrack_irc
  108. /sbin/modprobe ip_nat_ftp
  109. /sbin/modprobe ip_nat_irc

  110. ###########################################################################
  111. #
  112. # 3. /proc set up.
  113. #

  114. #
  115. # 3.1 Required proc configuration
  116. #

  117. echo "1" > /proc/sys/net/ipv4/ip_forward

  118. #
  119. # 3.2 Non-Required proc configuration
  120. #

  121. #echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
  122. #echo "1" > /proc/sys/net/ipv4/conf/all/proxy_arp
  123. #echo "1" > /proc/sys/net/ipv4/ip_dynaddr

  124. ###########################################################################
  125. #
  126. # 4. rules set up.
  127. #

  128. ######
  129. # 4.1 Filter table
  130. #

  131. #
  132. # 4.1.1 Set policies
  133. #
  134. $IPTABLES  --flush
  135. $IPTABLES -t nat --flush
  136. $IPTABLES -t mangle --flush
  137. $IPTABLES -X
  138. $IPTABLES -P INPUT DROP
  139. $IPTABLES -P OUTPUT DROP
  140. $IPTABLES -P FORWARD DROP

  141. #
  142. # 4.1.2 Create userspecified chains
  143. #

  144. #
  145. # Create chain for bad tcp packets
  146. #

  147. $IPTABLES -N bad_tcp_packets

  148. #
  149. # Create separate chains for ICMP, TCP and UDP to traverse
  150. #

  151. $IPTABLES -N allowed
  152. $IPTABLES -N tcp_packets
  153. $IPTABLES -N udp_packets
  154. $IPTABLES -N icmp_packets

  155. #
  156. # 4.1.3 Create content in userspecified chains
  157. #

  158. #
  159. # bad_tcp_packets chain
  160. #

  161. $IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j LOG \
  162. --log-prefix "New not syn:"
  163. $IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP

  164. #
  165. # allowed chain
  166. #

  167. $IPTABLES -A allowed -p TCP --syn -j ACCEPT
  168. $IPTABLES -A allowed -p TCP -m state --state ESTABLISHED,RELATED -j ACCEPT
  169. $IPTABLES -A allowed -p TCP -j DROP

  170. #
  171. # TCP rules
  172. #

  173. #disable ftp port
  174. $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 21 -j allowed
  175. $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 20 -j allowed

  176. #this is port I use as ssh

  177. $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 9121 -j allowed
  178. #disable http port
  179. $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 80 -j allowed

  180. #disable pop3 port
  181. #$IPTABLES -A tcp_packets -p TCP -s 0/0 --dport pop3 -j allowed


  182. #$IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 113 -j allowed

  183. #this is smtp port
  184. #$IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 25 -j allowed

  185. #this is minadmin port
  186. $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 9122 -j allowed

  187. #this is named domain
  188. $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 53 -j allowed

  189. #
  190. # UDP ports
  191. #

  192. $IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 53 -j ACCEPT
  193. $IPTABLES -A udp_packets -p UDP -s 0/0 --dport 53 -j ACCEPT
  194. if [ $DHCP == "yes" ] ; then
  195. $IPTABLES -A udp_packets -p UDP -s $DHCP_SERVER --sport 67 \
  196. --dport 68 -j ACCEPT
  197. fi

  198. $IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 53 -j ACCEPT
  199. $IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 123 -j ACCEPT
  200. $IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 2074 -j ACCEPT
  201. $IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 4000 -j ACCEPT

  202. #
  203. # In Microsoft Networks you will be swamped by broadcasts. These lines
  204. # will prevent them from showing up in the logs.
  205. #

  206. $IPTABLES -A udp_packets -p UDP -i $INET_IFACE \
  207. --destination-port 135:139 -j DROP

  208. #
  209. # If we get DHCP requests from the Outside of our network, our logs will
  210. # be swamped as well. This rule will block them from getting logged.
  211. #

  212. $IPTABLES -A udp_packets -p UDP -i $INET_IFACE -d 255.255.255.255 \
  213. --destination-port 67:68 -j DROP

  214. #
  215. # ICMP rules
  216. #

  217. $IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j ACCEPT
  218. $IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT

  219. #
  220. # 4.1.4 INPUT chain
  221. #

  222. #
  223. # Bad TCP packets we don't want.
  224. #

  225. $IPTABLES -A INPUT -p tcp -j bad_tcp_packets

  226. #
  227. # Rules for special networks not part of the Internet
  228. #


  229. $IPTABLES -A INPUT -p ALL -i $LAN_IFACE -s $LAN_IP_RANGE -j ACCEPT
  230. $IPTABLES -A INPUT -p ALL -i $LO_IFACE -j ACCEPT
  231. $IPTABLES -A INPUT -p ALL -i $LAN_IFACE -d $LAN_BROADCAST_ADDRESS -j ACCEPT

  232. #
  233. # Special rule for DHCP requests from LAN, which are not caught properly
  234. # otherwise.
  235. #

  236. $IPTABLES -A INPUT -p UDP -i $LAN_IFACE --dport 67 --sport 68 -j ACCEPT
  237. #$IPTABLES -A INPUT -p UDP -i $LAN_IFACE2 --dport 67 --sport 68 -j ACCEPT
  238. $IPTABLES -A INPUT -p UDP -s 0.0.0.0 --sport 68 -d 255.255.255.255 --dport 67  -j ACCEPT

  239. #
  240. # Rules for incoming packets from the internet.
  241. #

  242. $IPTABLES -A INPUT -p ALL -i $INET_IFACE -m state --state ESTABLISHED,RELATED \
  243. -j ACCEPT
  244. $IPTABLES -A INPUT -p TCP -i $INET_IFACE -j tcp_packets
  245. $IPTABLES -A INPUT -p UDP -i $INET_IFACE -j udp_packets
  246. $IPTABLES -A INPUT -p ICMP -i $INET_IFACE -j icmp_packets

  247. #
  248. # If you have a Microsoft Network on the outside of your firewall, you may
  249. # also get flooded by Multicasts. We drop them so we do not get flooded by
  250. # logs
  251. #

  252. $IPTABLES -A INPUT -i $INET_IFACE -d 224.0.0.0/8 -j DROP

  253. #
  254. # Log weird packets that don't match the above.
  255. #

  256. $IPTABLES -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
  257. --log-level DEBUG --log-prefix "IPT INPUT packet died: "

  258. #
  259. # 4.1.5 FORWARD chain
  260. #

  261. #
  262. # Bad TCP packets we don't want
  263. #

  264. #list=`cat /root/firewall/blackdomain.txt`
  265. #for domain in $list
  266. #do
  267. #$IPTABLES -A FORWARD  -d $domain -j DROP
  268. #done

  269. $IPTABLES -A FORWARD -p tcp -j bad_tcp_packets

  270. #
  271. # Accept the packets we actually want to forward
  272. #

  273. #a type user,100 packages/s
  274. list=`cat /root/firewall/a.txt`
  275. for ip in $list
  276. do
  277. $IPTABLES -A FORWARD  -d $ip/32 -m state --state ESTABLISHED,RELATED -m limit --limit 100/s -j ACCEPT
  278. $IPTABLES -A FORWARD  -d $ip/32 -j DROP
  279. done

  280. #b type user,40 packages/s
  281. list=`cat /root/firewall/b.txt`
  282. for ip in $list
  283. do
  284. $IPTABLES -A FORWARD  -d $ip/32 -m state --state ESTABLISHED,RELATED -m limit --limit 60/s -j ACCEPT
  285. $IPTABLES -A FORWARD  -d $ip/32 -j DROP
  286. done

  287. #c type user,20 packages/s
  288. list=`cat /root/firewall/c.txt`
  289. for ip in $list
  290. do
  291. $IPTABLES -A FORWARD  -d $ip/32 -m state --state ESTABLISHED,RELATED -m limit --limit 20/s -j ACCEPT
  292. $IPTABLES -A FORWARD  -d $ip/32 -j DROP
  293. done


  294. $IPTABLES -A FORWARD  -m state --state ESTABLISHED,RELATED -j ACCEPT



  295. #
  296. # Log weird packets that don't match the above.
  297. #

  298. $IPTABLES -A FORWARD -m limit --limit 3/minute --limit-burst 3 -j LOG \
  299. --log-level DEBUG --log-prefix "IPT FORWARD packet died: "

  300. #
  301. # 4.1.6 OUTPUT chain
  302. #

  303. #
  304. # Bad TCP packets we don't want.
  305. #

  306. $IPTABLES -A OUTPUT -p tcp -j bad_tcp_packets

  307. #
  308. # Special OUTPUT rules to decide which IP's to allow.
  309. #

  310. $IPTABLES -A OUTPUT -p ALL -s $LO_IP -j ACCEPT
  311. $IPTABLES -A OUTPUT -p ALL -s $LAN_IP -j ACCEPT
  312. $IPTABLES -A OUTPUT -p ALL -o $INET_IFACE -j ACCEPT

  313. #
  314. # Log weird packets that don't match the above.
  315. #

  316. $IPTABLES -A OUTPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
  317. --log-level DEBUG --log-prefix "IPT OUTPUT packet died: "

  318. ######
  319. # 4.2 nat table
  320. #
  321. #flush the nat tables first

  322. iptables --table nat --flush
  323. #
  324. # 4.2.1 Set policies
  325. #

  326. #
  327. # 4.2.2 Create user specified chains
  328. #

  329. #
  330. # 4.2.3 Create content in user specified chains
  331. #

  332. #
  333. # 4.2.4 PREROUTING chain
  334. #

  335. #       $IPTABLES -A PREROUTING -t nat -p tcp --dport 80 -d $EXT_IP -i ppp0 -j DNAT --to 192.168.0.251:80

  336. #       $IPTABLES -A FORWARD -p tcp --dport 80 -d 192.168.0.251  -i ppp0 -o eth0 -j ACCEPT

  337. #        $IPTABLES -A PREROUTING -t nat -p tcp --dport 1600 -d $EXT_IP2 -i ppp1 -j DNAT --to 192.168.0.251:80
  338.                                                                                 
  339. #        $IPTABLES -A FORWARD -p tcp --dport 1600 -d 192.168.0.251  -i ppp1 -o eth0 -j ACCEPT


  340. # $IPTABLES -t nat -A POSTROUTING -s 192.168.0.251  -j SNAT --to $EXT_IP

  341. #
  342. # 4.2.5 POSTROUTING chain
  343. #

  344. if [ $PPPOE_PMTU == "yes" ] ; then
  345. $IPTABLES -t nat -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN \
  346. -j TCPMSS --clamp-mss-to-pmtu
  347. fi
  348. #$IPTABLES -t nat -A POSTROUTING -o $INET_IFACE -j MASQUERADE

  349. list=`cat /root/firewall/a.txt`
  350. for ip in $list
  351. do
  352. iptables --table nat --append POSTROUTING -s $ip/32 --out-interface ppp0 -j MASQUERADE
  353. done

  354. list=`cat /root/firewall/b.txt`
  355. for ip in $list
  356. do
  357. iptables --table nat --append POSTROUTING -s $ip/32 --out-interface ppp0 -j MASQUERADE
  358. done

  359. list=`cat /root/firewall/c.txt`
  360. for ip in $list
  361. do
  362. iptables --table nat --append POSTROUTING -s $ip/32 --out-interface ppp0 -j MASQUERADE
  363. done


  364. #
  365. # 4.2.6 OUTPUT chain
  366. #

  367. ######
  368. # 4.3 mangle table
  369. #

  370. #
  371. # 4.3.1 Set policies
  372. #

  373. #
  374. # 4.3.2 Create user specified chains
  375. #

  376. #
  377. # 4.3.3 Create content in user specified chains
  378. #

  379. #
  380. # 4.3.4 PREROUTING chain
  381. #

  382. #
  383. # 4.3.5 INPUT chain
  384. #

  385. #
  386. # 4.3.6 FORWARD chain
  387. #

  388. #
  389. # 4.3.7 OUTPUT chain
  390. #

  391. #
  392. # 4.3.8 POSTROUTING chain
  393. #

  394. list=`cat /root/firewall/blackdomain.txt`
  395. for domain in $list
  396. do
  397. $IPTABLES -A FORWARD  -d $domain -j DROP
  398. done
  399. $IPTABLES -A FORWARD -i $LAN_IFACE -j ACCEPT
  400. #update: Jan,25,2005,huzhiwen ,we use bash to realize the firewall,so we donot need the perl ,it is too heavy to use
复制代码



其中对于带宽分配有几种模式
a
b
c
每种模式的带宽不一样


对于不想用户使用的黑网站放在
/root/firewall/blackdomain.txt中,使用名字即可

由于还使用了动态域名(感谢www.3322.org)


  1. [root@linux monitor]# cat monitorip.sh
  2. #!/bin/bash
  3. org_ip=`cat /root/monitor/ip.txt`
  4. now_ip=`ifconfig|sed '/^eth/,/^ppp0/d'|sed '/^ *UP/,/$d/d'|cut -d: -f 2|cut -d ' ' -f 1`
  5. #protect for case where we power off the adsl or the line is off
  6. space=""
  7. if [ "$space" = "$now_ip" ]
  8. then
  9. exit
  10. fi

  11. if [ $org_ip = $now_ip ]
  12. then
  13. echo "donot need update ip of ppp0" >> /dev/null
  14. else
  15. #put the new ip into file ip.txt
  16. ifconfig|sed '/^eth/,/^ppp0/d'|sed '/^ *UP/,/$d/d'|cut -d: -f 2|cut -d ' ' -f 1 > /root/monitor/ip.txt

  17. #update the dns
  18. /root/ez-ipupdate/ez-ipupdate -c /root/ez-ipupdate/qdns.conf
  19. #in order to make the system more stable,do it another 2 more times
  20. sleep 60
  21. /root/ez-ipupdate/ez-ipupdate -c /root/ez-ipupdate/qdns.conf
  22. sleep 60
  23. /root/ez-ipupdate/ez-ipupdate -c /root/ez-ipupdate/qdns.conf

  24. #flush the firewall
  25. /root/firewall/firewall.sh
  26. LOGDATE=`date`
  27. echo "$LOGDATE : IP CHANGE FROM $org_ip to $now_ip " >> /root/monitor/ipchange.log
  28. fi

复制代码


15分钟运行一次,如果ip改变了,则更新


防火墙脚本每15分钟运行一次,以免于防火墙死掉

每小时监视adsl看是否当掉

  1. [root@linux cron.hourly]# cat monitoradsl.sh
  2. #!/bin/sh
  3. device=`ifconfig|grep ppp0|cut -d ' ' -f 1`
  4. if [ "$device" = "ppp0" ]
  5. then
  6.   exit
  7. else
  8.   adsl-stop
  9.   sleep 5
  10.   adsl-stop
  11.   sleep 5
  12.   adsl-start
  13. fi
复制代码

每天重启一次adsl,脚本如下


  1. [root@linux cron.daily]# cat restartadsl.sh
  2. #!/bin/sh
  3. adsl-stop
  4. sleep 5
  5. adsl-stop
  6. sleep 5
  7. adsl-start

复制代码


基本就是这样了,由于水平有限,错误难免,欢迎大家指正!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP