配置桥接网卡,使Proxmox VE(PVE)可以同时创建NAT虚拟机和独立IP虚拟机,同时可以使用脚本映射NAT虚拟机的端口到外网。
vim /etc/sysctl.conf
文件最后加入如下配置
net.ipv4.ip_forward=1
net.ipv4.conf.all.rp_filter=1
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.conf.default.forwarding=1
net.ipv4.conf.default.proxy_arp = 0
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv6.conf.eno1.autoconf=0
net.ipv6.conf.eno1.accept_ra=2
net.ipv6.conf.default.forwarding=1
net.ipv6.conf.all.forwarding=1
net.ipv6.conf.default.proxy_ndp=1
net.ipv6.conf.all.proxy_ndp=1
vim /etc/network/interfaces
新增一个vmbr172网桥,并设置流量转发
auto lo
iface lo inet loopbackiface enp40 inet manualauto vmbr0
iface vmbr0 inet staticaddress 192.168.0.103/24gateway 192.168.0.1bridge-ports enp40bridge-stp offbridge-fd 0#物理网卡配置一般不做改动,系统模板都是配置好的。
#为虚拟机新建一个虚拟网桥
#内网地址,虚拟机的网关
auto vmbr172
iface vmbr172 inet staticaddress 172.16.1.1netmask 255.255.255.0bridge-ports nonebridge-stp offbridge-fd 0post-up echo 1 > /proc/sys/net/ipv4/ip_forwardpost-up echo 1 > /proc/sys/net/ipv4/conf/eno1/proxy_arp#转发IPv4流量到虚拟机,使虚拟机与外网联通。post-up iptables -t nat -A POSTROUTING -s '172.16.1.0/16' -o vmbr0 -j MASQUERADEpost-down iptables -t nat -D POSTROUTING -s '172.16.1.0/16' -o vmbr0 -j MASQUERADE
重启
sudo service networking restart
此处使用的是ubuntu做dhcp服务器
参考地址
#新增
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 10022 -j DNAT --to-destination 10.10.10.100:22
#删除 (即把新增映射的-A改成-D)
iptables -t nat -D PREROUTING -p tcp -m tcp --dport 10022 -j DNAT --to-destination 10.10.10.100:22
#查看NAT规则,并显示行号
iptables -t nat --list --line-number
#删除指定行号的iptables规则
iptables -t nat -D POSTROUTING 10
IPv4是经过内网转发的,所以现在只可以访问外网,而外网却无法访问虚拟机。所以我们需要配置端口转发,将外部的访问转发到虚拟机。一般的做法是通过iptables进行转发。
端口映射可以使用iptables来完整,已经有大佬写了方便的脚本;
脚本名称 iptables.sh
#! /bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#ConfFile
iptablesconf='/root/iptables.config.sh'
function rootness(){if [[ $EUID -ne 0 ]]; thenecho "脚本需要以ROOT权限运行!"exit 1fi
}
function conf_list(){cat $iptablesconf
}
function conf_add(){if [ ! -f $iptablesconf ];thenecho "找不到配置文件!"exit 1fiecho "请输入虚拟机的内网IP"read -p "(Default: Exit):" confvmip[ -z "$confvmip" ] && exit 1echoecho "虚拟机内网IP = $confvmip"echowhile truedoecho "请输入虚拟机的端口:"read -p "(默认端口: 22):" confvmport[ -z "$confvmport" ] && confvmport="22"expr $confvmport + 0 &>/dev/nullif [ $? -eq 0 ]; thenif [ $confvmport -ge 1 ] && [ $confvmport -le 65535 ]; thenechoecho "虚拟机端口 = $confvmport"echobreakelseecho "输入错误,端口范围应为1-65535!"fielseecho "输入错误,端口范围应为1-65535!"fidoneechowhile truedoecho "请输入宿主机的端口"read -p "(默认端口: 8899):" natconfport[ -z "$natconfport" ] && natconfport="8899"expr $natconfport + 0 &>/dev/nullif [ $? -eq 0 ]; thenif [ $natconfport -ge 1 ] && [ $natconfport -le 65535 ]; thenechoecho "宿主机端口 = $natconfport"echobreakelseecho "输入错误,端口范围应为1-65535!"fielseecho "输入错误,端口范围应为1-65535!"fidoneecho "请输入转发协议:"read -p "(tcp 或者 udp ,回车默认操作: 退出):" conftype[ -z "$conftype" ] && exit 1echoecho "协议类型 = $conftype"echoiptablesshell="iptables -t nat -A PREROUTING -i vmbr0 -p $conftype --dport $natconfport -j DNAT --to-destination $confvmip:$confvmport"if [ `grep -c "$iptablesshell" $iptablesconf` != '0' ]; thenecho "配置已经存在"exit 1figet_char(){SAVEDSTTY=`stty -g`stty -echostty cbreakdd if=/dev/tty bs=1 count=1 2> /dev/nullstty -rawstty echostty $SAVEDSTTY}echoecho "回车继续,Ctrl+C退出脚本"char=`get_char`echo $iptablesshell >> $iptablesconfrunreturn=`$iptablesshell`echo $runreturnecho '配置添加成功'
}
function add_confs(){rootnessconf_add
}
function del_conf(){echowhile truedoecho "请输入宿主机的端口"read -p "(默认操作: 退出):" confserverport[ -z "$confserverport" ] && exit 1expr $confserverport + 0 &>/dev/nullif [ $? -eq 0 ]; thenif [ $confserverport -ge 1 ] && [ $confserverport -le 65535 ]; thenechoecho "宿主机端口 = $confserverport"echobreakelseecho "输入错误,端口范围应为1-65535!"fielseecho "输入错误,端口范围应为1-65535!"fidoneechoiptablesshelldel=`cat $iptablesconf | grep "dport $confserverport"`if [ ! -n "$iptablesshelldel" ]; thenecho "配置文件中没有该宿主机的端口"exit 1fiiptablesshelldelshell=`echo ${iptablesshelldel//-A/-D}`runreturn=`$iptablesshelldelshell`echo $runreturnsed -i "/$iptablesshelldel/d" $iptablesconfecho '配置删除成功'
}
function del_confs(){printf "你确定要删除配置吗?操作是不可逆的(y/n) "printf "\n"read -p "(默认: n):" answerif [ -z $answer ]; thenanswer="n"fiif [ "$answer" = "y" ]; thenrootnessdel_confelseecho "配置删除操作取消"fi
}
action=$1
case "$action" in
add)add_confs;;
list)conf_list;;
del)del_confs;;
*)echo "参数错误! [${action} ]"echo "用法: `basename $0` {add|list|del}";;
esac
脚本配置文件
名称 iptables.config.sh
#!/usr/bin/env bash
上传iptables.sh和iptables.config.sh 脚本到宿主机root目录,或者直接在root目录下创建
赋予iptables.config.sh可执行权限
xxx自行更改,为配置文件的路径
chmod +x /xxx/iptables.config.sh
在 iptables.sh目录下执行一下命令根据提示进行添加或删除端口映射
#添加端口映射
bash iptables.sh add
#删除端口映射
bash iptables.sh del
这样,访问服务器10022端口就会自动转发到虚拟机10.10.10.100的22端口上,实现了外网通过ssh登陆虚拟机的目的。但是,这样做有一个问题,ProxmoxVE自带防火墙,iptables开放22端口,相当于在防火墙上面开了一个洞,而且这个洞不受防火墙的控制,如果需要做安全防护,你还需要单独配置iptables,不是很方便。于是想到一个折中办法,用brook做端口转发,同时利用ProxmoxVE的防火墙进行统一控制。
获取brook,项目地址
#version 版本
wget -O /root/brook https://github.com/txthinking/brook/releases/download/{version}/brook/brook_linux_xxxwget -O /root/brook https://github.com/txthinking/brook/releases/download/v20221212/brook_linux_amd64
将brook移动到你想要放的地方
赋予执行权限
brook路径 需要自行修改
chmod +x /brook路径/brook
启动端口转发
#前台运行退出终端及结束转发
nohup /root/brook relay --from :10022 --to 10.10.10.100:22
#设置为后台运行
nohup ./brook relay --from :10022 --to 10.10.10.100:22 > output 2>&1 &
利用命令ps -ef|grep brook
来查看转发是否生效,同时在ProxmoxVE防火墙中开放或者关闭10022端口来控制外网对虚拟机的访问。
KVM虚拟机的配置是类似的,创建时选择桥接vmbr0,不论安装的是Linux系统还是windows系统,都需要手动输入IPv4和IPv地址,同时在主机配置IPv6信息和添加端口转发。
这样,一个同时开启了NAT和IPv6的虚拟机就创建好了,但是,还有个问题,IPv6的配置和端口转发是会随着主机重启而失效,我们需要在主机上启动开机自启服务,保证每次开机的时候,虚拟机器都能正常工作。
需要在PVE中开启开机自启服务,创建/etc/rc.local。
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.exit 0
赋予执行权限
chmod +x /etc/rc.local
启动服务
systemctl start rc-local
这样,我们只需要将需要执行的命令写在/etc/rc.local的exit 0前面,就可以在开机后自动执行。
Proxmox VE同时配置NAT共享IP和独立IP虚拟机
PVE服务器网络配置
单IP服务器利用ProxmoxVE建立IPv4-NAT和IPv6虚拟机
brook安装使用参考
上一篇:安全功能测试
下一篇:C语言tips-作用域和链接属性