有时候人不在公司,临时有事需要远程公司电脑处理事情。我们知道公司电脑是在内网中的的,在家里是不能直接访问到的。我们一般使用QQ等支持远程协助的工具,但这样需要有人帮你发起请求,也有免费的软件支持无人值守远程操作,但一般会限速,高峰期经常卡顿。公司正好有台阿里云服务器流量空闲的比较多正好可以用来装个wireguard服务,然后在把公司内网电脑和家里的电脑做为客户端连接上就可以互通 了。
公司阿里云服务器使用的centos 7系统.kernel版本:
# uname -r
3.10.0-1160.15.2.el7.x86_64
可以使用下面的脚步安装:
#!/bin/bash
#判断系统
if [ ! -e '/etc/redhat-release' ]; then
echo "仅支持centos7"
exit
fi
if [ -n "$(grep ' 6\.' /etc/redhat-release)" ] ;then
echo "仅支持centos7"
exit
fi
#更新内核
update_kernel(){
yum -y install epel-release curl
sed -i "0,/enabled=0/s//enabled=1/" /etc/yum.repos.d/epel.repo
yum remove -y kernel-devel
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
yum -y --enablerepo=elrepo-kernel install kernel-ml
sed -i "s/GRUB_DEFAULT=saved/GRUB_DEFAULT=0/" /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-ml-devel-4.19.1-1.el7.elrepo.x86_64.rpm
rpm -ivh kernel-ml-devel-4.19.1-1.el7.elrepo.x86_64.rpm
yum -y --enablerepo=elrepo-kernel install kernel-ml-devel
read -p "须要重启VPS,再次执行脚本选择安装wireguard,是否如今重启 ? [Y/n] :" yn
[ -z "${yn}" ] && yn="y"
if [[ $yn == [Yy] ]]; then
echo -e "VPS 重启中..."
reboot
fi
}
#生成随机端口
rand(){
min=$1
max=$(($2-$min+1))
num=$(cat /dev/urandom | head -n 10 | cksum | awk -F ' ' '{print $1}')
echo $(($num%$max+$min))
}
wireguard_update(){
yum update -y wireguard-dkms wireguard-tools
echo "更新完成"
}
wireguard_remove(){
wg-quick down wg0
yum remove -y wireguard-dkms wireguard-tools
rm -rf /etc/wireguard/
echo "卸载完成"
}
config_client(){
cat > /etc/wireguard/client.conf <<-EOF
[Interface]
PrivateKey = $c1
Address = 10.0.0.2/24
DNS = 114.114.114.114
MTU = 1420
[Peer]
PublicKey = $s2
Endpoint = $serverip:$port
AllowedIPs = 0.0.0.0/0, ::0/0
PersistentKeepalive = 25
EOF
}
#centos7安装wireguard
wireguard_install(){
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 dkms gcc-c++ gcc-gfortran glibc-headers glibc-devel libquadmath-devel libtool systemtap systemtap-devel
yum -y install wireguard-dkms wireguard-tools
yum -y install qrencode
mkdir /etc/wireguard
cd /etc/wireguard
wg genkey | tee sprivatekey | wg pubkey > spublickey
wg genkey | tee cprivatekey | wg pubkey > cpublickey
s1=$(cat sprivatekey)
s2=$(cat spublickey)
c1=$(cat cprivatekey)
c2=$(cat cpublickey)
serverip=$(curl ipv4.icanhazip.com)
port=$(rand 10000 60000)
eth=$(ls /sys/class/net | awk '/^e/{print}')
chmod 777 -R /etc/wireguard
systemctl stop firewalld
systemctl disable firewalld
yum install -y iptables-services
systemctl enable iptables
systemctl start iptables
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -F
service iptables save
service iptables restart
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
cat > /etc/wireguard/wg0.conf <<-EOF
[Interface]
PrivateKey = $s1
Address = 10.0.0.1/24
PostUp = echo 1 > /proc/sys/net/ipv4/ip_forward; iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o $eth -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o $eth -j MASQUERADE
ListenPort = $port
DNS = 114.114.114
MTU = 1420
[Peer]
PublicKey = $c2
AllowedIPs = 10.0.0.2/24
EOF
config_client
wg-quick up wg0
systemctl enable wg-quick@wg0
content=$(cat /etc/wireguard/client.conf)
echo "电脑端请下载client.conf,手机端可直接使用软件扫码"
echo "${content}" | qrencode -o - -t UTF8
}
add_user(){
echo -e "\033[37;41m给新用户起个名字,不能和已有用户重复\033[0m"
read -p "请输入用户名:" newname
cd /etc/wireguard/
cp client.conf $newname.conf
wg genkey | tee temprikey | wg pubkey > tempubkey
ipnum=$(grep Allowed /etc/wireguard/wg0.conf | tail -1 | awk -F '[ ./]' '{print $6}')
newnum=$((10#${ipnum}+1))
sed -i 's%^PrivateKey.*$%'"PrivateKey = $(cat temprikey)"'%' $newname.conf
sed -i 's%^Address.*$%'"Address = 10.0.0.$newnum\/24"'%' $newname.conf
cat >> /etc/wireguard/wg0.conf <<-EOF
[Peer]
PublicKey = $(cat tempubkey)
AllowedIPs = 10.0.0.$newnum/24
EOF
wg set wg0 peer $(cat tempubkey) allowed-ips 10.0.0.$newnum/32
echo -e "\033[37;41m添加完成,文件:/etc/wireguard/$newname.conf\033[0m"
rm -f temprikey tempubkey
}
#开始菜单
start_menu(){
clear
echo "========================="
echo " 介绍:适用于CentOS7"
echo " 做者:atrandys"
echo " 网站:www.atrandys.com"
echo " Youtube:atrandys"
echo "========================="
echo "1. 升级系统内核"
echo "2. 安装wireguard"
echo "3. 升级wireguard"
echo "4. 卸载wireguard"
echo "5. 显示客户端二维码"
echo "6. 增长用户"
echo "0. 退出脚本"
echo
read -p "请输入数字:" num
case "$num" in
1)
update_kernel
;;
2)
wireguard_install
;;
3)
wireguard_update
;;
4)
wireguard_remove
;;
5)
content=$(cat /etc/wireguard/client.conf)
echo "${content}" | qrencode -o - -t UTF8
;;
6)
add_user
;;
0)
exit 1
;;
*)
clear
echo "请输入正确数字"
sleep 5s
start_menu
;;
esac
}
start_menu
把上面的代码复制到脚步文件如install_wireguard.sh并为文件添加可执行权限。使用root用户或sudo执行脚本文件:
# ./install_wireguard.sh
=========================
介绍:适用于CentOS7
做者:atrandys
网站:www.atrandys.com
Youtube:atrandys
=========================
1. 升级系统内核
2. 安装wireguard
3. 升级wireguard
4. 卸载wireguard
5. 显示客户端二维码
6. 增长用户
0. 退出脚本
请输入数字:2
看到菜单2是安装wireguard的,输入2,就开始安装wireguard了。
脚本执行完成后,执行 systemctl status wg-quick@wg0检查是否安装成功:
# systemctl status wg-quick@wg0
● [email protected] - WireGuard via wg-quick(8) for wg0
Loaded: loaded (/usr/lib/systemd/system/[email protected]; enabled; vendor preset: disabled)
Active: active (exited) since Fri 2021-03-12 10:31:12 CST; 44s ago
Docs: man:wg-quick(8)
man:wg(8)
https://www.wireguard.com/
https://www.wireguard.com/quickstart/
https://git.zx2c4.com/wireguard-tools/about/src/man/wg-quick.8
https://git.zx2c4.com/wireguard-tools/about/src/man/wg.8
Process: 28364 ExecStart=/usr/bin/wg-quick up %i (code=exited, status=0/SUCCESS)
Main PID: 28364 (code=exited, status=0/SUCCESS)
看到active就代表安装成功了.
接下来就可以生成客户端配置文件了.
还是执行install_wireguard.sh,看到目录输入6,也就是增长用户选项:
=========================
介绍:适用于CentOS7
做者:atrandys
网站:www.atrandys.com
Youtube:atrandys
=========================
1. 升级系统内核
2. 安装wireguard
3. 升级wireguard
4. 卸载wireguard
5. 显示客户端二维码
6. 增长用户
0. 退出脚本
请输入数字:6
给新用户起个名字,不能和已有用户重复
请输入用户名:home
用户名可以自己取,不重复好分辩就好。输入用户名后记得敲回车。
执行成功后就可以在/etc/wirguard/ 目录下找到对应的客户端配置文件了,如home.conf。
再执行一次,生成company.conf文件。
分别把home.conf文件和company.conf文件复制到家里和公司的办公电脑。家里和公司的电脑是windows系统,直接下载对应的安装包安装就可以了,跟安装普通的软件一样.安装好后打开,导入客户端配置文件再点Activate就可以了:
可以在办公电脑上执行:
ping 10.0.0.1
如果能ping通就代表组网成功了.
接下来你就可以使用家里的电脑(10.0.0.3)远程访问公司的电脑(10.0.0.4)了。具体IP地址跟客户端添加顺序有关。你可以查看对应客户端配置文件确定其IP:
[Interface]
PrivateKey = aCfyRy96aMx/gLM+SRpnmYWUBGqr+9bdSLk2OyNK7k8=
Address = 10.0.0.3/24
DNS = 114.114.114.114
MTU = 1420[Peer]
.......
如果需要离线安装wirguard可以直接下载对应的rpm文件,也可以使用yumdownloader在可以联互联网的机器上下载好的rpm文件.
下载好后进入wirguard rpm文件所在目录执行:
rpm -ivh *.rpm --nodeps --force
如果系统先前没有安装iptables-service还需要进入相应rpm文件夹,执行上面的命令安装.
安装成功后修改上面的install_wireguard.sh脚本对应的安装函数:
#centos7安装wireguard
wireguard_install(){
# 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 dkms gcc-c++ gcc-gfortran glibc-headers glibc-devel libquadmath-devel libtool systemtap systemtap-devel
# yum -y install wireguard-dkms wireguard-tools
# yum -y install qrencode
# mkdir /etc/wireguard
cd /etc/wireguard
wg genkey | tee sprivatekey | wg pubkey > spublickey
wg genkey | tee cprivatekey | wg pubkey > cpublickey
s1=$(cat sprivatekey)
s2=$(cat spublickey)
c1=$(cat cprivatekey)
c2=$(cat cpublickey)
serverip=$(curl ipv4.icanhazip.com)
port=$(rand 10000 60000)
eth=$(ls /sys/class/net | awk '/^e/{print}')
chmod 777 -R /etc/wireguard
systemctl stop firewalld
systemctl disable firewalld
# yum install -y iptables-services
systemctl enable iptables
systemctl start iptables
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -F
service iptables save
service iptables restart
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
也就是注释掉了所有需要互联网的操作。然后再执行脚本,选择2:安装wireguard就可以了。添加客户端的方式跟先前在阿里云服务器上一样,不再赘述。
看到这里,都挺顺利的,还没体现强制离线安装。这样提到一次离线安装经历。系统版本也是Centos7,但内核版本是:3.10.0-862.与公司阿里云服务器内核版本基本一样,只是在版本号的最后有点小差别,但就是这么小的差别,安装就是不成功。按照上面的方式离线安装wirguard后,使用systemctl status wg-quick@wg0查看运行失败。使用wg-quick up wg0启动wireguard报:
#sudo wg-quick up wg0
[#] ip link add wg0 type wireguard
Unable to access interface: Protocol not supported
[#]ip link delete dev wg0
Cannot find device "wg0"
通过错误信息搜索问题大多是选择升级内核解决,但目前环境和时间不允许做升级内核操作。只得继续探索,尝试使用dkms build重新编译安装wireguard:
dkms build wirguard/1.0.20210219-1
竟然报错了,根据错误提示信息是socket.c文件中有错误,找到对应源码文件对应代码行。发现出差的函数跟ipv6有关,还好我们没用ipv6,直接注释掉出错的代码:
再次执行dkms build成功!然后在执行
dkms install wirguard/1.0.20210219-1
再次执行wg-quick up wg0启动wireguard就成功了!!!