PVE 开启nat上网 以及端口转发 DHCP

coolsd 2021-6-9 119

只需要开启小鸡上网

auto vmbr1
iface vmbr1 inet static
        address 192.168.0.1/24
        bridge-ports none
        bridge-stp off
        bridge-fd 0
        post-up echo 1 > /proc/sys/net/ipv4/ip_forward
        post-up iptables -t nat -A POSTROUTING -s '192.168.0.0/24' -o vmbr0 -j MASQUERADE
        post-down iptables -t nat -D POSTROUTING -s '192.168.0.0/24' -o vmbr0 -j MASQUERADE

开启DHCP

apt-get update
apt-get -y install dnsmasq
cat >> /etc/network/interfaces <<'EOF'

auto vmbr1
iface vmbr1 inet static
address 192.168.0.1
netmask 255.255.255.0
bridge_ports none
bridge_stp off
bridge_fd 0
EOF
cat >> /etc/dnsmasq.conf <<'EOF'
interface=vmbr1
#dhcp-option=1,255.255.255.0 
dhcp-range=192.168.0.2,192.168.0.20,12h
dhcp-option=3,192.168.0.1
dhcp-option=option:dns-server,114.114.114.114,8.8.8.8
EOF
cat >> /etc/sysctl.conf <<'EOF'
net.ipv4.ip_forward = 1
EOF
sysctl -p
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE
cat > /etc/network/if-pre-up.d/iptables <<'EOF'
#!/bin/bash
/sbin/iptables-restore < /etc/iptables.up.rules
EOF
chmod +x /etc/network/if-pre-up.d/iptables
iptables-save > /etc/iptables.up.rules
service networking restart
service dnsmasq restar
最新回复 (3)
  • coolsd 2021-6-9
    引用 2

    NAT端口转发

    #! /bin/bash
    # chkconfig: 2345 55 25
    # Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
    # run 'update-rc.d -f router defaults', or use the appropriate command on your
    # distro. For CentOS/Redhat run: 'chkconfig --add nat'
    #open nat       
    echo 1 > /proc/sys/net/ipv4/ip_forward
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    NAME=iptables
    R_BIN=/usr/sbin/$NAME
    read -p "输入虚拟机网段(默认:192.168.0.0/24):" LAN
    if [ -z "${LAN}" ];then
            LAN=192.168.0.0/24
    fi
    read -p "输入外网IP(默认:142.202.243.2):" IPi
    if [ -z "${IPi}" ];then
            IPi=142.202.243.2
    fi
    read -p "输入外网端口(默认:en02):" WAN
    if [ -z "${WAN}" ];then
            WAN=eno2
    fi
    case "$1" in
        start)
            echo -n "Starting router.. "
            $R_BIN -t nat -A PREROUTING -d $IPi -p tcp --dport $2 -j DNAT --to-destination $3 
            echo " done"
            ;;
        stop)
            echo -n "Stoping router.. "
            $R_BIN -t nat -D PREROUTING -d $IPi -p tcp --dport $2 -j DNAT --to-destination $3 
            echo " done"
            ;;
        status)
            $R_BIN -t nat -nL
            ;;
        startr)
            echo -n "Starting router.. "
            if [ $2 ];then
               LAN=$2
            fi
            $R_BIN -t nat -A POSTROUTING -s $LAN -o eth0 -j MASQUERADE 
            echo " done"
            ;;
        stopr)
            echo -n "Stoping router.. "
            if [ $2 ];then 
               LAN=$2
            fi
            $R_BIN -t nat -D POSTROUTING -s $LAN -o eth0 -j MASQUERADE 
            echo " done"
            ;;
        *)
            echo "Usage: $0 {start <iport> <dip:dport>|stop <iport> <dip:dport>|status|startr <IPv4/CIDR>|stopr <IPv4/CIDR> }"
            exit 1
            ;;
    esac


  • coolsd 2021-7-22
    引用 3
    内建一个网桥做中转
    1. auto vmbr1
    2.     iface vmbr1 inet static
    3.             address 10.0.0.0.1
    4.             netmask 255.255.255.0
    5.             bridge_ports none
    6.             bridge_stp off
    7.             bridge_fd 0
    8.             post-up echo 1 > /proc/sys/net/ipv4/ip_forward
    9.             post-up bash /root/iptables.config.sh
    10.             post-up iptables -t nat -A POSTROUTING -s '10.0.0.0/24' -o vmbr0 -j MASQUERADE
    11.             post-down iptables -t nat -D POSTROUTING -s 10.0.0.0/24' -o vmbr0 -j MASQUERADE
    复制代码

    新建小鸡网卡桥接到vmbr1这个接口就行了
    端口映射直接iptables
    1. iptables -t nat -A PREROUTING -i vmbr0 -p <协议> --dport <母鸡端口> -j DNAT --to-destination <小鸡ip>:<小鸡端口>
    复制代码


    最后于 2021-7-22 被coolsd编辑 ,原因:
  • coolsd 2021-7-23
    引用 4
    iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 8444 -j DNAT --to-destination 192.168.0.12:8444
    iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 8447 -j DNAT --to-destination 192.168.0.12:8447
    iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 3389 -j DNAT --to-destination 192.168.0.12:3389
    iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 12221 -j DNAT --to-destination 192.168.0.12:12220
    iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 8888 -j DNAT --to-destination 192.168.0.12:8888
    iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 5217 -j DNAT --to-destination 192.168.0.12:5217
    iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 443 -j DNAT --to-destination 192.168.0.12:443
    iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to-destination 192.168.0.12:80



    最后于 2021-7-24 被coolsd编辑 ,原因:
返回
发新帖
本站文章均收录自网上,仅做参考用。 欢迎大家添砖加瓦,发表一些自己使用过程中遇到的问题以及解决方法。