Mathias revisou este gist . Ir para a revisão
1 file changed, 150 insertions
VPN_Setup_OpenVPN.md(arquivo criado)
@@ -0,0 +1,150 @@ | |||
1 | + | # Setting Up a Home Server: Static IP and Port Exposure with OpenVPN on Linux | |
2 | + | ||
3 | + | Have you ever wished to run your own email server from the comfort of your home but were held back by the need for a | |
4 | + | reliable static IP? There is a solution! Whether it's hosting a personal website, gaming server, or a | |
5 | + | specialized application, this guide will show you how to set up a static IP and port exposure using OpenVPN on Linux. | |
6 | + | ||
7 | + | > "What if I already have a static IP?" | |
8 | + | > This guide is intended for users that don't have a static IP address at home and want to expose a service on their | |
9 | + | > home network to the internet. If you already have a static IP address, you may not need this guide. | |
10 | + | ||
11 | + | ## Prerequisites | |
12 | + | - 🖥️ A hosted Linux server with a public IPv4 address | |
13 | + | - :computer: A linux server at your home (e.g. Raspberry Pi) | |
14 | + | - :thinking: A basic understanding of Linux and networking | |
15 | + | ||
16 | + | ## Overview | |
17 | + | ||
18 | + | The basic idea is to set up a VPN server on your hosted Linux server and a VPN client on your home Linux server. The | |
19 | + | VPN client will connect to the VPN server and expose the VPN server's IP address to the internet. This will allow you | |
20 | + | to access your home server from the internet using the VPN server's IP address. | |
21 | + | A nice side effect of this setup is that your actual home IP address will be hidden from the internet. | |
22 | + | ||
23 | + | ## 🖥️ Setting up the VPN Server | |
24 | + | ||
25 | + | This process is fairly straightforward. We will be using the [OpenVPN-Install](https://github.com/angristan/openvpn-install) | |
26 | + | script to set up the VPN server. This script will install and configure OpenVPN on your server. It will also generate | |
27 | + | a client configuration file that you will need to copy to your home server. | |
28 | + | ||
29 | + | 1. SSH into your hosted Linux server | |
30 | + | 2. Install curl (if not already installed) | |
31 | + | ``` bash | |
32 | + | sudo apt update && apt install curl -y | |
33 | + | ``` | |
34 | + | 3. Ensure you are in the `/root` directory: | |
35 | + | ``` bash | |
36 | + | cd /root | |
37 | + | ``` | |
38 | + | 4. Download the OpenVPN-Install script and make it executable: | |
39 | + | ``` bash | |
40 | + | curl -O https://raw.githubusercontent.com/angristan/openvpn-install/master/openvpn-install.sh | |
41 | + | chmod +x openvpn-install.sh | |
42 | + | sudo AUTO_INSTALL=y ./openvpn-install.sh | |
43 | + | ``` | |
44 | + | 5. Create a client configuration file by running the following command: | |
45 | + | ``` bash | |
46 | + | sudo ./openvpn-install.sh | |
47 | + | ``` | |
48 | + | Type `1` and press `Enter` to create a new client. You will be prompted to enter a name for the client. Enter a name | |
49 | + | of your choice and press `Enter`. Now type in `1` again and press `Enter` to confirm everything. The script will | |
50 | + | generate a client configuration file and save it to `/root/<client-name>.ovpn`. You will need to copy this file | |
51 | + | to your home server in the next step. | |
52 | + | ||
53 | + | ## :computer: Setting up the VPN Client | |
54 | + | ||
55 | + | 1. SSH into your home Linux server and install OpenVPN: | |
56 | + | ``` bash | |
57 | + | sudo apt update && apt install openvpn -y | |
58 | + | ``` | |
59 | + | 2. Copy the client configuration file from your hosted Linux server to your home Linux server. You can use `scp` to | |
60 | + | copy the file: | |
61 | + | ``` bash | |
62 | + | scp root@<hosted-server-ip>:/root/<client-name>.ovpn /etc/openvpn/client.conf | |
63 | + | ``` | |
64 | + | 3. Start the OpenVPN service: | |
65 | + | ``` bash | |
66 | + | sudo systemctl start openvpn@client | |
67 | + | ``` | |
68 | + | 4. Check the status of the OpenVPN service: | |
69 | + | ``` bash | |
70 | + | sudo systemctl status openvpn@client | |
71 | + | ``` | |
72 | + | 5. Enable the OpenVPN service to start on boot: | |
73 | + | ``` bash | |
74 | + | sudo systemctl enable openvpn@client | |
75 | + | ``` | |
76 | + | 6. Check your public IP address: | |
77 | + | ``` bash | |
78 | + | curl ifconfig.me | |
79 | + | ``` | |
80 | + | You should see the IP address of your hosted Linux server. This means that your home Linux server is now connected | |
81 | + | to your hosted Linux server via the VPN server. | |
82 | + | ||
83 | + | ## :twisted_rightwards_arrows: Setting up port forwarding | |
84 | + | ||
85 | + | Now that your home Linux server is connected to your hosted Linux server via the VPN server, you can expose any port | |
86 | + | on your home Linux server to the internet. This is done by setting up port forwarding on your hosted Linux server. | |
87 | + | ||
88 | + | First, you need to find out the IP address of your home Linux server. You can do this by running the following command | |
89 | + | on your hosted Linux server: | |
90 | + | ``` bash | |
91 | + | cat /etc/openvpn/ipp.txt | cut -d ',' -f 2 | |
92 | + | ``` | |
93 | + | ||
94 | + | This is the IP address of your home Linux server. You can now set up port forwarding on your hosted Linux server to | |
95 | + | expose any port on your home Linux server to the internet. | |
96 | + | ||
97 | + | To make the setup easier, we are going to set an environment variable with the IP address of your home Linux server: | |
98 | + | ``` bash | |
99 | + | export HOME_IP=<home-server-ip> # Replace <home-server-ip> with the IP address of your home Linux server | |
100 | + | export STATIC_IP=<static-ip> # Replace <static-ip> with the static IP address of your hosted Linux server | |
101 | + | ``` | |
102 | + | ||
103 | + | First, you need to enable IP forwarding on your hosted Linux server. Replace `eth0` with the name of your network | |
104 | + | interface: | |
105 | + | ``` bash | |
106 | + | sudo sysctl -w net.ipv4.ip_forward=1 | |
107 | + | sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE | |
108 | + | ``` | |
109 | + | ||
110 | + | You can use the following commands to set up port forwarding on your hosted Linux server: | |
111 | + | ||
112 | + | ### :globe_with_meridians: Exposing a TCP port | |
113 | + | ``` bash | |
114 | + | PORT=<port>; sudo iptables -t nat -d $STATIC_IP -A PREROUTING -p tcp --dport $PORT -j DNAT --to-destination $HOME_IP:$PORT | |
115 | + | ``` | |
116 | + | ||
117 | + | ### :globe_with_meridians: Exposing a UDP port | |
118 | + | ``` bash | |
119 | + | PORT=<port>; sudo iptables -t nat -d $STATIC_IP -A PREROUTING -p udp --dport $PORT -j DNAT --to-destination $HOME_IP:$PORT | |
120 | + | ``` | |
121 | + | ||
122 | + | ### :earth_africa: Exposing a range of TCP ports | |
123 | + | ``` bash | |
124 | + | START_PORT=<start-port> END_PORT=<end-port>; sudo iptables -t nat -d $STATIC_IP -A PREROUTING -p tcp --dport $START_PORT:$END_PORT -j DNAT --to-destination $HOME_IP:$START_PORT-$END_PORT | |
125 | + | ``` | |
126 | + | ||
127 | + | ### :earth_africa: Exposing a range of UDP ports | |
128 | + | ``` bash | |
129 | + | START_PORT=<start-port> END_PORT=<end-port>; sudo iptables -t nat -d $STATIC_IP -A PREROUTING -p udp --dport $START_PORT:$END_PORT -j DNAT --to-destination $HOME_IP:$START_PORT-$END_PORT | |
130 | + | ``` | |
131 | + | ||
132 | + | ### :information_source: Example: Exposing a web server on port 80 | |
133 | + | ``` bash | |
134 | + | PORT=80; sudo iptables -t nat -d $STATIC_IP -A PREROUTING -p tcp --dport $PORT -j DNAT --to-destination $HOME_IP:$PORT | |
135 | + | ``` | |
136 | + | ||
137 | + | ## :floppy_disk: Making the iptables rules persistent | |
138 | + | The iptables rules are not persistent by default. This means that if you reboot your hosted Linux server, the iptables | |
139 | + | rules will be lost. To make the iptables rules persistent, you need to install the `iptables-persistent` package: | |
140 | + | ``` bash | |
141 | + | sudo apt update && apt install iptables-persistent -y | |
142 | + | ``` | |
143 | + | ||
144 | + | During the installation, you will be asked if you want to save the current iptables rules. Type `yes` and press `Enter` | |
145 | + | to save the rules. The iptables rules will now be loaded automatically on boot. | |
146 | + | ||
147 | + | ## :tada: Conclusion | |
148 | + | ||
149 | + | Congratulations! You have successfully set up a static IP and port exposure using OpenVPN on Linux. You can now access | |
150 | + | any service on your home Linux server from the internet using the IP address of your hosted Linux server. |
Próximo
Anterior