您的位置:首页 > 其它

Ubuntu 16.04 IP分享服务器搭建

2017-07-21 12:22 399 查看
系统:Ubuntu 16.04

工具:iptables + isc-dhcp-server

服务器需有两块网卡:eth0 + eth1,一个接外网(eth0),一个接局域网(eth1)

-----------------------------------------------------------------------------配置DHCP---------------------------------------------------------------------------------------------------

sudo apt-get install  isc-dhcp-server

sudo vim /etc/default/isc-dhcp-server 将设置为INTERFACES="eth1"(此处填局域网卡)

sudo vim /etc/dhcp/dhcpd.conf设置内容参考如下:

ddns-update-style none;

# 这里按需填写,不行domain-name-servers就直接上8.8.8.8, 8.8.4.4
option domain-name "tongji.edu.cn";
option domain-name-servers 202.120.190.208, 202.120.190.108;

default-lease-time 600;
max-lease-time 7200;

# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;

# This is a very basic subnet declaration.
# 这里参考具体环境修改
subnet 192.168.3.0 netmask 255.255.255.0 {
range 192.168.3.10 192.168.3.100;
option routers 192.168.3.1;
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.3.255;
option domain-name-servers 202.120.190.208;
}
设置完执行sudo service isc-dhcp-server start 和 sudo service isc-dhcp-server enable

-----------------------------------------------------------------------------配置iptables---------------------------------------------------------------------------------------------------

iptables 系统自带不需自行安装

参考鸟哥教程:http://linux.vbird.org/linux_server/0250simple_firewall.php

编写一个ipshare.sh脚本,内容如下:

#!/bin/bash

# 請先輸入您的相關參數,不要輸入錯誤了!
EXTIF="eth0" # 這個是可以連上 Public IP 的網路介面
INIF="eth1" # 內部 LAN 的連接介面;若無則寫成 INIF=""
INNET="192.168.3.0/24" # 若無內部網域介面,請填寫成 INNET=""
export EXTIF INIF INNET

# 第一部份,針對本機的防火牆設定!##########################################
# 1. 先設定好核心的網路功能:
echo "1" > /proc/sys/net/ipv4/tcp_syncookies
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

# 2. 清除規則、設定預設政策及開放 lo 與相關的設定值
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin; export PATH
iptables -F
iptables -X
iptables -Z
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

# 3. 啟動額外的防火牆 script 模組
if [ -f /usr/local/virus/iptables/iptables.deny ]; then
sh /usr/local/virus/iptables/iptables.deny
fi
if [ -f /usr/local/virus/
4000
iptables/iptables.allow ]; then
sh /usr/local/virus/iptables/iptables.allow
fi
if [ -f /usr/local/virus/httpd-err/iptables.http ]; then
sh /usr/local/virus/httpd-err/iptables.http
fi

# 4. 允許某些類型的 ICMP 封包進入
AICMP="0 3 3/4 4 11 12 14 16 18"
for tyicmp in $AICMP
do
iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT
done

# 5. 允許某些服務的進入,請依照你自己的環境開啟
iptables -A INPUT -p TCP -i $EXTIF --dport 21 --sport 1024:65534 -j ACCEPT # FTP
iptables -A INPUT -p TCP -i $EXTIF --dport 22 --sport 1024:65534 -j ACCEPT # SSH
#iptables -A INPUT -p TCP -i $EXTIF --dport 25 --sport 1024:65534 -j ACCEPT # SMTP
iptables -A INPUT -p UDP -i $EXTIF --dport  53 --sport 1024:65534 -j ACCEPT # DNS
iptables -A INPUT -p TCP -i $EXTIF --dport  53 --sport 1024:65534 -j ACCEPT # DNS
iptables -A INPUT -p TCP -i $EXTIF --dport  80 --sport 1024:65534 -j ACCEPT # WWW
#iptables -A INPUT -p TCP -i $EXTIF --dport 110 --sport 1024:65534 -j ACCEPT # POP3
iptables -A INPUT -p TCP -i $EXTIF --dport 443 --sport 1024:65534 -j ACCEPT # HTTPS

 iptables -A INPUT -p TCP -i $EXTIF --dport 139 --sport 1024:65534 -j ACCEPT # SMB
 iptables -A INPUT -p TCP -i $EXTIF --dport 445 --sport 1024:65534 -j ACCEPT # SMB
 iptables -A INPUT -p UDP -i $EXTIF --dport 137 --sport 1024:65534 -j ACCEPT # SMB
 iptables -A INPUT -p UDP -i $EXTIF --dport 138 --sport 1024:65534 -j ACCEPT # SMB

# 6. 清除 NAT table 的規則吧!
  iptables -F -t nat
  iptables -X -t nat
  iptables -Z -t nat
  iptables -t nat -P PREROUTING  ACCEPT
  iptables -t nat -P POSTROUTING ACCEPT
  iptables -t nat -P OUTPUT      ACCEPT

# 7. 若有內部介面的存在 (雙網卡) 開放成為路由器,且為 IP 分享器!
  if [ "$INIF" != "" ]; then
    iptables -A INPUT -i $INIF -j ACCEPT
    echo "1" > /proc/sys/net/ipv4/ip_forward
    if [ "$INNET" != "" ]; then
        for innet in $INNET
        do
            iptables -t nat -A POSTROUTING -s $innet -o $EXTIF -j MASQUERADE
        done
    fi
  fi
配置完毕后sudo chmod +x ipshare.sh,然后执行sudo sh ipshare.sh

完毕~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: