123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- #!/bin/bash
- # WireGuard installer for Ubuntu 18.04 LTS, Debian 9 and CentOS 7.
- # This script will let you setup your own VPN server in no more than a minute, even if you haven't used WireGuard before.
- # It has been designed to be as unobtrusive and universal as possible.
- # wireguard-install
- wireguard_install(){
- if [ -e /etc/centos-release ]; then
- DISTRO="CentOS"
- elif [ -e /etc/debian_version ]; then
- DISTRO=$( lsb_release -is )
- else
- echo "Your distribution is not supported (yet)"
- exit
- fi
- if [ "$DISTRO" == "Ubuntu" ]; then
- apt update
- apt install software-properties-common -y
- echo .read | add-apt-repository ppa:wireguard/wireguard
- apt update
- apt install wireguard resolvconf qrencode -y
- elif [ "$DISTRO" == "Debian" ]; then
- echo "deb http://deb.debian.org/debian/ unstable main" > /etc/apt/sources.list.d/unstable.list
- printf 'Package: *\nPin: release a=unstable\nPin-Priority: 90\n' > /etc/apt/preferences.d/limit-unstable
- apt update
- apt install wireguard resolvconf qrencode -y
- elif [ "$DISTRO" == "CentOS" ]; then
- curl -Lo /etc/yum.repos.d/wireguard.repo https://copr.fedorainfracloud.org/coprs/jdoss/wireguard/repo/epel-7/jdoss-wireguard-epel-7.repo
- yum install -y epel-release
- yum install -y wireguard-dkms wireguard-tools
- yum -y install qrencode iptables-services
- systemctl stop firewalld && systemctl disable firewalld
- systemctl enable iptables && systemctl start iptables
- iptables -F && service iptables save && service iptables restart
- fi
- mkdir -p /etc/wireguard
- cd /etc/wireguard
- wg genkey | tee sprivatekey | wg pubkey > spublickey
- wg genkey | tee cprivatekey | wg pubkey > cpublickey
- chmod 777 -R /etc/wireguard
- systemctl enable wg-quick@wg0
- }
- if [ ! -f '/usr/bin/wg' ]; then
- wireguard_install
- fi
- # WireGuard VPN Multi-user Configuration Script, Support IPV6
- #############################################################
- let port=$RANDOM/2+9999
- mtu=1420
- ip_list=(2 5 8 178 186 118 158 198 168 9)
- ipv6_range="fd08:620c:4df0:65eb::"
- # Get WireGuard Management Command : bash wgmtu
- wget -O ~/wgmtu https://raw.githubusercontent.com/hongwenjun/vps_setup/english/wgmtu.sh
- # 定义文字颜色
- Green="\033[32m" && Red="\033[31m" && GreenBG="\033[42;37m" && RedBG="\033[41;37m"
- Font="\033[0m" && Yellow="\033[0;33m" && SkyBlue="\033[0;36m"
- echo_SkyBlue(){
- echo -e "${SkyBlue}$1${Font}"
- }
- echo_Yellow(){
- echo -e "${Yellow}$1${Font}"
- }
- echo_GreenBG(){
- echo -e "${GreenBG}$1${Font}"
- }
- echo_RedBG(){
- echo -e "${RedBG}$1${Font}"
- }
- #############################################################
- if [[ $# > 0 ]]; then
- num="$1"
- if [[ ${num} -ge 100 ]] && [[ ${num} -le 60000 ]]; then
- port=$num
- fi
- fi
- host=$(hostname -s)
- # 获得服务器ip,自动获取
- if [ ! -f '/usr/bin/curl' ]; then
- apt update && apt install -y curl
- fi
- if [ ! -e '/var/ip_addr' ]; then
- echo -n $(curl -4 ip.sb) > /var/ip_addr
- fi
- serverip=$(cat /var/ip_addr)
- #############################################################
- # 打开ip4/ipv6防火墙转发功能
- sysctl_config() {
- sed -i '/net.ipv4.ip_forward/d' /etc/sysctl.conf
- sed -i '/net.ipv6.conf.all.forwarding/d' /etc/sysctl.conf
- sed -i '/net.ipv6.conf.default.accept_ra/d' /etc/sysctl.conf
- echo 1 > /proc/sys/net/ipv4/ip_forward
- echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
- echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.conf
- echo "net.ipv6.conf.default.accept_ra=2" >> /etc/sysctl.conf
- sysctl -p >/dev/null 2>&1
- }
- sysctl_config
- # wg配置文件目录 /etc/wireguard
- mkdir -p /etc/wireguard
- chmod 777 -R /etc/wireguard
- cd /etc/wireguard
- # 然后开始生成 密匙对(公匙+私匙)。
- wg genkey | tee sprivatekey | wg pubkey > spublickey
- wg genkey | tee cprivatekey | wg pubkey > cpublickey
- # 生成服务端配置文件
- cat <<EOF >wg0.conf
- [Interface]
- PrivateKey = $(cat sprivatekey)
- Address = 10.0.0.1/24, ${ipv6_range}1/64
- PostUp = iptables -I FORWARD -i wg0 -j ACCEPT; iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT; ip6tables -I FORWARD -i wg0 -j ACCEPT; ip6tables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
- PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
- ListenPort = $port
- DNS = 8.8.8.8, 2001:4860:4860::8888
- MTU = $mtu
- [Peer]
- PublicKey = $(cat cpublickey)
- AllowedIPs = 10.0.0.188/32, ${ipv6_range}188
- EOF
- # 生成简洁的客户端配置
- cat <<EOF >client.conf
- [Interface]
- PrivateKey = $(cat cprivatekey)
- Address = 10.0.0.188/24, ${ipv6_range}188/64
- DNS = 8.8.8.8, 2001:4860:4860::8888
- # MTU = $mtu
- # PreUp = start .\route\routes-up.bat
- # PostDown = start .\route\routes-down.bat
- [Peer]
- PublicKey = $(cat spublickey)
- Endpoint = $serverip:$port
- AllowedIPs = 0.0.0.0/0, ::0/0
- PersistentKeepalive = 25
- EOF
- # 添加 2-9 号多用户配置
- for i in {2..9}
- do
- ip=10.0.0.${ip_list[$i]}
- ip6=${ipv6_range}${ip_list[$i]}
- wg genkey | tee cprivatekey | wg pubkey > cpublickey
- cat <<EOF >>wg0.conf
- [Peer]
- PublicKey = $(cat cpublickey)
- AllowedIPs = $ip/32, $ip6
- EOF
- cat <<EOF >wg_${host}_$i.conf
- [Interface]
- PrivateKey = $(cat cprivatekey)
- Address = $ip/24, $ip6/64
- DNS = 8.8.8.8, 2001:4860:4860::8888
- [Peer]
- PublicKey = $(cat spublickey)
- Endpoint = $serverip:$port
- AllowedIPs = 0.0.0.0/0, ::0/0
- PersistentKeepalive = 25
- EOF
- cat /etc/wireguard/wg_${host}_$i.conf | qrencode -o wg_${host}_$i.png
- done
- # restart WG server
- wg-quick down wg0
- wg-quick up wg0
- wg
- next() {
- printf "# %-70s\n" "-" | sed 's/\s/-/g'
- }
- echo
- echo_SkyBlue ":: Windows Client configuration, Please copy the conf text."
- cat /etc/wireguard/client.conf && next
- cat /etc/wireguard/wg_${host}_2.conf && next
- cat /etc/wireguard/wg_${host}_3.conf && next
- echo_RedBG " One-Step Automated Install WireGuard Script For Debian_9 Ubuntu Centos_7 "
- echo_GreenBG " Open Source Project: https://github.com/hongwenjun/vps_setup "
- echo_Yellow ":: WireGuard Management Command."
- echo_SkyBlue "Usage: ${GreenBG} bash wgmtu ${SkyBlue} [ setup | remove | vps | bench | -U ] "
- echo_SkyBlue " [ v2ray | vnstat | log | trace | -h ] "
|