- 论坛徽章:
- 0
|
我使用linux作为上网共享服务器稳定运行1年了,基本是不管它,效果还可以,现在把我的架设方式描叙一下,给大家一个参考
机器是塞扬400,128M内存,无键盘,无显示器,两个网卡,安装好redhat linux后,在开机bios中电源管理中,设置为来电后自动启动(经常停电,无办法).
设置ADSL按照通常的方法,使用adsl-setup,设置好后,使用adsl-start启动adsl,然后在本机上是否连上,一般是没有问题的.
设置机器启动后自动启动adsl
rc.local文件设置如下
- #!/bin/sh
- #
- # This script will be executed *after* all the other init scripts.
- # You can put your own initialization stuff in here if you don't
- # want to do the full Sys V style init stuff.
- touch /var/lock/subsys/local
- adsl-start #启动adsl
- /root/firewall/firewall.sh #运行防火墙脚本
复制代码
防火墙脚本基本是采取iptables一样,但是自己根据需求小小改了一下
全文如下
- #!/bin/sh
- #
- # rc.firewall - DHCP IP Firewall script for Linux 2.4.x and iptables
- #
- # Copyright (C) 2001 Oskar Andreasson <bluefluxATkoffeinDOTnet>
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; version 2 of the License.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program or from the site that you downloaded it
- # from; if not, write to the Free Software Foundation, Inc., 59 Temple
- # Place, Suite 330, Boston, MA 02111-1307 USA
- #
- ###########################################################################
- #
- # 1. Configuration options.
- #
- #
- # 1.1 Internet Configuration.
- #
- EXT_IP=`ifconfig|sed '/^eth/,/^ppp0/d'|sed '/^ *UP/,/$d/d'|cut -d: -f 2|cut -d ' ' -f 1`
- INET_IFACE="ppp0"
- #
- # 1.1.1 DHCP
- #
- #
- # Information pertaining to DHCP over the Internet, if needed.
- #
- # Set DHCP variable to no if you don't get IP from DHCP. If you get DHCP
- # over the Internet set this variable to yes, and set up the proper IP
- # address for the DHCP server in the DHCP_SERVER variable.
- #
- DHCP="no"
- #DHCP_SERVER="192.168.1.1"
- #
- # 1.1.2 PPPoE
- #
- # Configuration options pertaining to PPPoE.
- #
- # If you have problem with your PPPoE connection, such as large mails not
- # getting through while small mail get through properly etc, you may set
- # this option to "yes" which may fix the problem. This option will set a
- # rule in the PREROUTING chain of the mangle table which will clamp
- # (resize) all routed packets to PMTU (Path Maximum Transmit Unit).
- #
- # Note that it is better to set this up in the PPPoE package itself, since
- # the PPPoE configuration option will give less overhead.
- #
- PPPOE_PMTU="no"
- #
- # 1.2 Local Area Network configuration.
- #
- # your LAN's IP range and localhost IP. /24 means to only use the first 24
- # bits of the 32 bit IP address. the same as netmask 255.255.255.0
- #
- LAN_IP="192.168.1.1"
- LAN_IP_RANGE="192.168.1.0/24"
- LAN_BROADCAST_ADDRESS="192.168.1.255"
- LAN_IFACE="eth0"
- #
- # 1.3 DMZ Configuration.
- #
- #
- # 1.4 Localhost Configuration.
- #
- LO_IFACE="lo"
- LO_IP="127.0.0.1"
- #
- # 1.5 IPTables Configuration.
- #
- IPTABLES="/sbin/iptables"
- #
- # 1.6 Other Configuration.
- #
- ###########################################################################
- #
- # 2. Module loading.
- #
- #
- # Needed to initially load modules
- #
- /sbin/depmod -a
- #
- # 2.1 Required modules
- #
- /sbin/modprobe ip_conntrack
- /sbin/modprobe ip_tables
- /sbin/modprobe iptable_filter
- /sbin/modprobe iptable_mangle
- /sbin/modprobe iptable_nat
- /sbin/modprobe ipt_LOG
- /sbin/modprobe ipt_limit
- /sbin/modprobe ipt_MASQUERADE
- #
- # 2.2 Non-Required modules
- #
- #/sbin/modprobe ipt_owner
- #/sbin/modprobe ipt_REJECT
- /sbin/modprobe ip_conntrack_ftp
- /sbin/modprobe ip_conntrack_irc
- /sbin/modprobe ip_nat_ftp
- /sbin/modprobe ip_nat_irc
- ###########################################################################
- #
- # 3. /proc set up.
- #
- #
- # 3.1 Required proc configuration
- #
- echo "1" > /proc/sys/net/ipv4/ip_forward
- #
- # 3.2 Non-Required proc configuration
- #
- #echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
- #echo "1" > /proc/sys/net/ipv4/conf/all/proxy_arp
- #echo "1" > /proc/sys/net/ipv4/ip_dynaddr
- ###########################################################################
- #
- # 4. rules set up.
- #
- ######
- # 4.1 Filter table
- #
- #
- # 4.1.1 Set policies
- #
- $IPTABLES --flush
- $IPTABLES -t nat --flush
- $IPTABLES -t mangle --flush
- $IPTABLES -X
- $IPTABLES -P INPUT DROP
- $IPTABLES -P OUTPUT DROP
- $IPTABLES -P FORWARD DROP
- #
- # 4.1.2 Create userspecified chains
- #
- #
- # Create chain for bad tcp packets
- #
- $IPTABLES -N bad_tcp_packets
- #
- # Create separate chains for ICMP, TCP and UDP to traverse
- #
- $IPTABLES -N allowed
- $IPTABLES -N tcp_packets
- $IPTABLES -N udp_packets
- $IPTABLES -N icmp_packets
- #
- # 4.1.3 Create content in userspecified chains
- #
- #
- # bad_tcp_packets chain
- #
- $IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j LOG \
- --log-prefix "New not syn:"
- $IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP
- #
- # allowed chain
- #
- $IPTABLES -A allowed -p TCP --syn -j ACCEPT
- $IPTABLES -A allowed -p TCP -m state --state ESTABLISHED,RELATED -j ACCEPT
- $IPTABLES -A allowed -p TCP -j DROP
- #
- # TCP rules
- #
- #disable ftp port
- $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 21 -j allowed
- $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 20 -j allowed
- #this is port I use as ssh
- $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 9121 -j allowed
- #disable http port
- $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 80 -j allowed
- #disable pop3 port
- #$IPTABLES -A tcp_packets -p TCP -s 0/0 --dport pop3 -j allowed
- #$IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 113 -j allowed
- #this is smtp port
- #$IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 25 -j allowed
- #this is minadmin port
- $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 9122 -j allowed
- #this is named domain
- $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 53 -j allowed
- #
- # UDP ports
- #
- $IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 53 -j ACCEPT
- $IPTABLES -A udp_packets -p UDP -s 0/0 --dport 53 -j ACCEPT
- if [ $DHCP == "yes" ] ; then
- $IPTABLES -A udp_packets -p UDP -s $DHCP_SERVER --sport 67 \
- --dport 68 -j ACCEPT
- fi
- $IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 53 -j ACCEPT
- $IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 123 -j ACCEPT
- $IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 2074 -j ACCEPT
- $IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 4000 -j ACCEPT
- #
- # In Microsoft Networks you will be swamped by broadcasts. These lines
- # will prevent them from showing up in the logs.
- #
- $IPTABLES -A udp_packets -p UDP -i $INET_IFACE \
- --destination-port 135:139 -j DROP
- #
- # If we get DHCP requests from the Outside of our network, our logs will
- # be swamped as well. This rule will block them from getting logged.
- #
- $IPTABLES -A udp_packets -p UDP -i $INET_IFACE -d 255.255.255.255 \
- --destination-port 67:68 -j DROP
- #
- # ICMP rules
- #
- $IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j ACCEPT
- $IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT
- #
- # 4.1.4 INPUT chain
- #
- #
- # Bad TCP packets we don't want.
- #
- $IPTABLES -A INPUT -p tcp -j bad_tcp_packets
- #
- # Rules for special networks not part of the Internet
- #
- $IPTABLES -A INPUT -p ALL -i $LAN_IFACE -s $LAN_IP_RANGE -j ACCEPT
- $IPTABLES -A INPUT -p ALL -i $LO_IFACE -j ACCEPT
- $IPTABLES -A INPUT -p ALL -i $LAN_IFACE -d $LAN_BROADCAST_ADDRESS -j ACCEPT
- #
- # Special rule for DHCP requests from LAN, which are not caught properly
- # otherwise.
- #
- $IPTABLES -A INPUT -p UDP -i $LAN_IFACE --dport 67 --sport 68 -j ACCEPT
- #$IPTABLES -A INPUT -p UDP -i $LAN_IFACE2 --dport 67 --sport 68 -j ACCEPT
- $IPTABLES -A INPUT -p UDP -s 0.0.0.0 --sport 68 -d 255.255.255.255 --dport 67 -j ACCEPT
- #
- # Rules for incoming packets from the internet.
- #
- $IPTABLES -A INPUT -p ALL -i $INET_IFACE -m state --state ESTABLISHED,RELATED \
- -j ACCEPT
- $IPTABLES -A INPUT -p TCP -i $INET_IFACE -j tcp_packets
- $IPTABLES -A INPUT -p UDP -i $INET_IFACE -j udp_packets
- $IPTABLES -A INPUT -p ICMP -i $INET_IFACE -j icmp_packets
- #
- # If you have a Microsoft Network on the outside of your firewall, you may
- # also get flooded by Multicasts. We drop them so we do not get flooded by
- # logs
- #
- $IPTABLES -A INPUT -i $INET_IFACE -d 224.0.0.0/8 -j DROP
- #
- # Log weird packets that don't match the above.
- #
- $IPTABLES -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
- --log-level DEBUG --log-prefix "IPT INPUT packet died: "
- #
- # 4.1.5 FORWARD chain
- #
- #
- # Bad TCP packets we don't want
- #
- #list=`cat /root/firewall/blackdomain.txt`
- #for domain in $list
- #do
- #$IPTABLES -A FORWARD -d $domain -j DROP
- #done
- $IPTABLES -A FORWARD -p tcp -j bad_tcp_packets
- #
- # Accept the packets we actually want to forward
- #
- #a type user,100 packages/s
- list=`cat /root/firewall/a.txt`
- for ip in $list
- do
- $IPTABLES -A FORWARD -d $ip/32 -m state --state ESTABLISHED,RELATED -m limit --limit 100/s -j ACCEPT
- $IPTABLES -A FORWARD -d $ip/32 -j DROP
- done
- #b type user,40 packages/s
- list=`cat /root/firewall/b.txt`
- for ip in $list
- do
- $IPTABLES -A FORWARD -d $ip/32 -m state --state ESTABLISHED,RELATED -m limit --limit 60/s -j ACCEPT
- $IPTABLES -A FORWARD -d $ip/32 -j DROP
- done
- #c type user,20 packages/s
- list=`cat /root/firewall/c.txt`
- for ip in $list
- do
- $IPTABLES -A FORWARD -d $ip/32 -m state --state ESTABLISHED,RELATED -m limit --limit 20/s -j ACCEPT
- $IPTABLES -A FORWARD -d $ip/32 -j DROP
- done
- $IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
- #
- # Log weird packets that don't match the above.
- #
- $IPTABLES -A FORWARD -m limit --limit 3/minute --limit-burst 3 -j LOG \
- --log-level DEBUG --log-prefix "IPT FORWARD packet died: "
- #
- # 4.1.6 OUTPUT chain
- #
- #
- # Bad TCP packets we don't want.
- #
- $IPTABLES -A OUTPUT -p tcp -j bad_tcp_packets
- #
- # Special OUTPUT rules to decide which IP's to allow.
- #
- $IPTABLES -A OUTPUT -p ALL -s $LO_IP -j ACCEPT
- $IPTABLES -A OUTPUT -p ALL -s $LAN_IP -j ACCEPT
- $IPTABLES -A OUTPUT -p ALL -o $INET_IFACE -j ACCEPT
- #
- # Log weird packets that don't match the above.
- #
- $IPTABLES -A OUTPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
- --log-level DEBUG --log-prefix "IPT OUTPUT packet died: "
- ######
- # 4.2 nat table
- #
- #flush the nat tables first
- iptables --table nat --flush
- #
- # 4.2.1 Set policies
- #
- #
- # 4.2.2 Create user specified chains
- #
- #
- # 4.2.3 Create content in user specified chains
- #
- #
- # 4.2.4 PREROUTING chain
- #
- # $IPTABLES -A PREROUTING -t nat -p tcp --dport 80 -d $EXT_IP -i ppp0 -j DNAT --to 192.168.0.251:80
- # $IPTABLES -A FORWARD -p tcp --dport 80 -d 192.168.0.251 -i ppp0 -o eth0 -j ACCEPT
- # $IPTABLES -A PREROUTING -t nat -p tcp --dport 1600 -d $EXT_IP2 -i ppp1 -j DNAT --to 192.168.0.251:80
-
- # $IPTABLES -A FORWARD -p tcp --dport 1600 -d 192.168.0.251 -i ppp1 -o eth0 -j ACCEPT
- # $IPTABLES -t nat -A POSTROUTING -s 192.168.0.251 -j SNAT --to $EXT_IP
- #
- # 4.2.5 POSTROUTING chain
- #
- if [ $PPPOE_PMTU == "yes" ] ; then
- $IPTABLES -t nat -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN \
- -j TCPMSS --clamp-mss-to-pmtu
- fi
- #$IPTABLES -t nat -A POSTROUTING -o $INET_IFACE -j MASQUERADE
- list=`cat /root/firewall/a.txt`
- for ip in $list
- do
- iptables --table nat --append POSTROUTING -s $ip/32 --out-interface ppp0 -j MASQUERADE
- done
- list=`cat /root/firewall/b.txt`
- for ip in $list
- do
- iptables --table nat --append POSTROUTING -s $ip/32 --out-interface ppp0 -j MASQUERADE
- done
- list=`cat /root/firewall/c.txt`
- for ip in $list
- do
- iptables --table nat --append POSTROUTING -s $ip/32 --out-interface ppp0 -j MASQUERADE
- done
- #
- # 4.2.6 OUTPUT chain
- #
- ######
- # 4.3 mangle table
- #
- #
- # 4.3.1 Set policies
- #
- #
- # 4.3.2 Create user specified chains
- #
- #
- # 4.3.3 Create content in user specified chains
- #
- #
- # 4.3.4 PREROUTING chain
- #
- #
- # 4.3.5 INPUT chain
- #
- #
- # 4.3.6 FORWARD chain
- #
- #
- # 4.3.7 OUTPUT chain
- #
- #
- # 4.3.8 POSTROUTING chain
- #
- list=`cat /root/firewall/blackdomain.txt`
- for domain in $list
- do
- $IPTABLES -A FORWARD -d $domain -j DROP
- done
- $IPTABLES -A FORWARD -i $LAN_IFACE -j ACCEPT
- #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)
- [root@linux monitor]# cat monitorip.sh
- #!/bin/bash
- org_ip=`cat /root/monitor/ip.txt`
- now_ip=`ifconfig|sed '/^eth/,/^ppp0/d'|sed '/^ *UP/,/$d/d'|cut -d: -f 2|cut -d ' ' -f 1`
- #protect for case where we power off the adsl or the line is off
- space=""
- if [ "$space" = "$now_ip" ]
- then
- exit
- fi
- if [ $org_ip = $now_ip ]
- then
- echo "donot need update ip of ppp0" >> /dev/null
- else
- #put the new ip into file ip.txt
- ifconfig|sed '/^eth/,/^ppp0/d'|sed '/^ *UP/,/$d/d'|cut -d: -f 2|cut -d ' ' -f 1 > /root/monitor/ip.txt
- #update the dns
- /root/ez-ipupdate/ez-ipupdate -c /root/ez-ipupdate/qdns.conf
- #in order to make the system more stable,do it another 2 more times
- sleep 60
- /root/ez-ipupdate/ez-ipupdate -c /root/ez-ipupdate/qdns.conf
- sleep 60
- /root/ez-ipupdate/ez-ipupdate -c /root/ez-ipupdate/qdns.conf
- #flush the firewall
- /root/firewall/firewall.sh
- LOGDATE=`date`
- echo "$LOGDATE : IP CHANGE FROM $org_ip to $now_ip " >> /root/monitor/ipchange.log
- fi
复制代码
15分钟运行一次,如果ip改变了,则更新
防火墙脚本每15分钟运行一次,以免于防火墙死掉
每小时监视adsl看是否当掉
- [root@linux cron.hourly]# cat monitoradsl.sh
- #!/bin/sh
- device=`ifconfig|grep ppp0|cut -d ' ' -f 1`
- if [ "$device" = "ppp0" ]
- then
- exit
- else
- adsl-stop
- sleep 5
- adsl-stop
- sleep 5
- adsl-start
- fi
复制代码
每天重启一次adsl,脚本如下
- [root@linux cron.daily]# cat restartadsl.sh
- #!/bin/sh
- adsl-stop
- sleep 5
- adsl-stop
- sleep 5
- adsl-start
复制代码
基本就是这样了,由于水平有限,错误难免,欢迎大家指正!! |
|