Created
September 12, 2021 07:26
-
-
Save ShashkovS/05b7e00b0fe091bf0d89adc268579db4 to your computer and use it in GitHub Desktop.
Install and config OpenVNP on CentOs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# openvpn | |
# Нужно знать IP своего вебсервера. Пусть это будет 123.123.123.123 (replace наше всё) | |
# И выбрать порт, на котором оно будет работать. Если vps пустой, то лучше всего порт 443 (это https). | |
# Если там уже есть вебсервер, то replace наше всё, мы будем использовать порт 12312 | |
# Разное стартовое, если сервер совсем «голый» | |
# yum -> dnf | |
yum upgrade | |
yum -y install epel-release | |
yum -y install dnf | |
dnf clean all | |
dnf update | |
# Разное для сборки | |
dnf -y groupinstall development | |
# nano + mc + vim + 7zip | |
dnf -y install nano mc vim p7zip | |
# Хорошо бы настроить вход по ssh-ключу, но это отдельно гуглите | |
# Ещё хорошо бы порт для ssh с 22 заменить на другой | |
# Это вот здесь: nano /etc/ssh/sshd_config | |
# Потом его в iptables нужно не забыть добавить | |
# Создаём пользователя | |
useradd openvpn --no-create-home --user-group --shell /bin/false | |
# Ставим пакеты и зависимости | |
yum install epel-release | |
yum install openvpn easy-rsa –y | |
# Теперь готовим разнообразные внутренние ключи | |
mkdir /var/log/openvpn/ | |
mkdir /etc/openvpn/ccd | |
cd ~ | |
wget https://github.com/OpenVPN/easy-rsa/archive/master.zip | |
unzip master.zip | |
cd ~/easy-rsa-master/easyrsa3 | |
cp ~/easy-rsa-master/easyrsa3/vars.example ~/easy-rsa-master/easyrsa3/vars | |
./easyrsa init-pki | |
./easyrsa build-ca nopass | |
./easyrsa gen-dh | |
./easyrsa gen-req vpn-server nopass | |
./easyrsa sign-req server vpn-server | |
mkdir -p /etc/openvpn/keys | |
yes | cp ~/easy-rsa-master/easyrsa3/pki/ca.crt /etc/openvpn/keys | |
yes | cp ~/easy-rsa-master/easyrsa3/pki/issued/vpn-server.crt /etc/openvpn/keys | |
yes | cp ~/easy-rsa-master/easyrsa3/pki/private/vpn-server.key /etc/openvpn/keys | |
yes | cp ~/easy-rsa-master/easyrsa3/pki/dh.pem /etc/openvpn/keys | |
cd /etc/openvpn/keys/ | |
openvpn --genkey --secret ta.key | |
# Заливаем конфиг | |
touch /etc/openvpn/server.conf | |
cat > /etc/openvpn/server.conf << EOF | |
port 443 | |
proto udp | |
dev tun0 | |
user openvpn | |
group openvpn | |
dh /etc/openvpn/keys/dh.pem | |
ca /etc/openvpn/keys/ca.crt | |
cert /etc/openvpn/keys/vpn-server.crt | |
key /etc/openvpn/keys/vpn-server.key | |
tls-auth /etc/openvpn/keys/ta.key 0 | |
# crl-verify /etc/openvpn/keys/crl.pem | |
script-security 2 | |
cipher BF-CBC | |
tls-server | |
comp-lzo | |
mute 10 | |
persist-key | |
persist-tun | |
max-clients 50 | |
keepalive 10 900 | |
client-config-dir /etc/openvpn/ccd | |
ifconfig-pool-persist /etc/openvpn/ccd/ipp.txt | |
server 10.15.0.0 255.255.255.0 | |
push "dhcp-option DNS 8.8.8.8" | |
push "dhcp-option DNS 8.8.4.4" | |
push "redirect-gateway def1" | |
status /var/log/openvpn/openvpn-status.log 1 | |
status-version 3 | |
log-append /var/log/openvpn/openvpn-server.log | |
verb 5 | |
EOF | |
# Теперь нужны iptable для настройки маршрутизации | |
# Не зевните здесь порт, который используется для подключения по ssh | |
ln -sf /usr/share/zoneinfo/Europe/Moscow /etc/localtime | |
dnf -y install iptables-services | |
systemctl mask firewalld | |
systemctl stop firewalld | |
systemctl enable iptables | |
systemctl start iptables | |
/usr/libexec/iptables/iptables.init save | |
echo "1" > /proc/sys/net/ipv4/ip_forward | |
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf | |
echo "" > /etc/sysconfig/iptables | |
systemctl restart iptables.service | |
iptables --flush | |
iptables -A INPUT -p UDP --dport 443 -j ACCEPT | |
iptables -A FORWARD -s 10.15.0.0/24 -j ACCEPT | |
iptables -A FORWARD -d 10.15.0.0/24 -j ACCEPT | |
iptables -t nat -A POSTROUTING -s 10.15.0.0/24 -j SNAT --to-source 123.123.123.123 | |
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT | |
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT | |
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT | |
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 12312 -j ACCEPT | |
iptables -A INPUT -p udp -m state --state NEW -m udp --dport 12312 -j ACCEPT | |
iptables-save > /etc/sysconfig/iptables | |
# Запускаем сервис | |
systemctl -f enable [email protected] | |
systemctl restart network.service | |
systemctl restart iptables.service | |
systemctl restart [email protected] | |
# Создаём скрипт для удобного создания ключей | |
cd ~/easy-rsa-master/easyrsa3/ | |
cat > ~/easy-rsa-master/easyrsa3/mk_client.sh << EOT | |
#!/bin/sh | |
# FILE: ~/easy-rsa-master/easyrsa3/mk_client.sh | |
CLIENT=$1 | |
if [ "$CLIENT" = "" ]; then | |
echo "missing client name" 1>&2 | |
exit | |
fi | |
cd ~/easy-rsa-master/easyrsa3 | |
./easyrsa gen-req ${CLIENT} nopass | |
./easyrsa sign-req client ${CLIENT} | |
#Клиенту передать эти файлы: | |
mkdir ${CLIENT} && cd ${CLIENT} | |
cp ~/easy-rsa-master/easyrsa3/pki/issued/${CLIENT}.crt ${CLIENT}.crt | |
cp ~/easy-rsa-master/easyrsa3/pki/private/${CLIENT}.key ${CLIENT}.key | |
cp ~/easy-rsa-master/easyrsa3/pki/ca.crt ca.crt | |
cp /etc/openvpn/keys/ta.key ta.key | |
cat > ${CLIENT}.ovpn << EOF | |
client | |
dev tun | |
proto udp | |
remote 3y6uhib5.shashkovs.ru 443 | |
tls-client | |
# ca "ca.crt" | |
# tls-auth "ta.key" 1 | |
# cert "${CLIENT}.crt" | |
# key "${CLIENT}.key" | |
key-direction 1 | |
remote-cert-tls server | |
comp-lzo | |
tun-mtu 1500 | |
mssfix 1450 | |
verb 3 | |
nobind | |
resolv-retry infinite | |
EOF | |
echo "<ca>" >> ${CLIENT}.ovpn | |
cat ~/easy-rsa-master/easyrsa3/pki/ca.crt | \ | |
grep -A 100 "BEGIN CERTIFICATE" | \ | |
grep -B 100 "END CERTIFICATE" >> ${CLIENT}.ovpn | |
echo "</ca>" >> ${CLIENT}.ovpn | |
echo "<tls-auth>" >> ${CLIENT}.ovpn | |
cat /etc/openvpn/keys/ta.key | \ | |
grep -A 100 "BEGIN OpenVPN Static key" | \ | |
grep -B 100 "END OpenVPN Static key" >> ${CLIENT}.ovpn | |
echo "</tls-auth>" >> ${CLIENT}.ovpn | |
echo "<cert>" >> ${CLIENT}.ovpn | |
cat ~/easy-rsa-master/easyrsa3/pki/issued/${CLIENT}.crt | \ | |
grep -A 100 "BEGIN CERTIFICATE" | \ | |
grep -B 100 "END CERTIFICATE" >> ${CLIENT}.ovpn | |
echo "</cert>" >> ${CLIENT}.ovpn | |
echo "<key>" >> ${CLIENT}.ovpn | |
cat ~/easy-rsa-master/easyrsa3/pki/private/${CLIENT}.key | \ | |
grep -A 100 "BEGIN PRIVATE KEY" | \ | |
grep -B 100 "END PRIVATE KEY" >> ${CLIENT}.ovpn | |
echo "</key>" >> ${CLIENT}.ovpn | |
EOT | |
chmod +x ~/easy-rsa-master/easyrsa3/mk_client.sh | |
# Готово! | |
# Теперь, чтобы создать новый ключ | |
cd ~/easy-rsa-master/easyrsa3/ | |
./mk_client.sh new_name | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment