how to write the config file for wireguard under linux for 2 connection one as client one as server
To configure WireGuard for both client and server connections on Linux, you need to create two separate configuration files—one for each connection.
- Server Configuration:
Create a file named
wg0.conf(or any name you prefer) for the server configuration.
sudo nano /etc/wireguard/wg0.conf
Add the following content to the wg0.conf file for the server connection:
[Interface]
PrivateKey = <server_private_key>
Address = <server_ip>/24
ListenPort = <server_listen_port>
[Peer]
PublicKey = <client_public_key>
AllowedIPs = <client_ip>/32
Replace <server_private_key> with the server's private key, <server_ip> with the server's IP address, and <server_listen_port> with the port number on which the server listens. Also, replace <client_public_key> with the client's public key and <client_ip> with the client's IP address.
- Client Configuration:
Create a file named
wg-client.conf(or any name you prefer) for the client configuration.
sudo nano /etc/wireguard/wg-client.conf
Add the following content to the wg-client.conf file for the client connection:
[Interface]
PrivateKey = <client_private_key>
Address = <client_ip>/24
[Peer]
PublicKey = <server_public_key>
Endpoint = <server_ip>:<server_listen_port>
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
Replace <client_private_key> with the client's private key, <client_ip> with the client's IP address, <server_public_key> with the server's public key, <server_ip> with the server's IP address, and <server_listen_port> with the server's listen port.
- Set file permissions: Make sure the configuration files have the correct permissions for WireGuard to read and use them.
sudo chmod 600 /etc/wireguard/wg0.conf
sudo chmod 600 /etc/wireguard/wg-client.conf
- Enable IP forwarding:
To enable IP forwarding, edit the
/etc/sysctl.conffile and uncomment the linenet.ipv4.ip_forward=1.
sudo nano /etc/sysctl.conf
Save the file and then run the following command to apply the changes:
sudo sysctl -p
- Start the WireGuard service: Start the WireGuard service to establish the connections.
sudo systemctl start wg-quick@wg0
To enable the service to start at boot, run the following command:
sudo systemctl enable wg-quick@wg0
That's it! You have now configured both the server and client connections for WireGuard on Linux. Make sure to replace the placeholder values with the actual keys, IP addresses, and ports specific to your setup
原文地址: http://www.cveoy.top/t/topic/h164 著作权归作者所有。请勿转载和采集!