One place for hosting & domains

      Server

      How To Set Up an OpenVPN Server on Debian 10


      A previous version of this tutorial was written by Justin Ellingwood

      Introduction

      Want to access the Internet safely and securely from your smartphone or laptop when connected to an untrusted network such as the WiFi of a hotel or coffee shop? A Virtual Private Network (VPN) allows you to traverse untrusted networks privately and securely as if you were on a private network. The traffic emerges from the VPN server and continues its journey to the destination.

      When combined with HTTPS connections, this setup allows you to secure your wireless logins and transactions. You can circumvent geographical restrictions and censorship, and shield your location and any unencrypted HTTP traffic from the untrusted network.

      OpenVPN is a full-featured, open-source Secure Socket Layer (SSL) VPN solution that accommodates a wide range of configurations. In this tutorial, you will set up an OpenVPN server on a Debian 10 server and then configure access to it from Windows, macOS, iOS and/or Android. This tutorial will keep the installation and configuration steps as simple as possible for each of these setups.

      Note: If you plan to set up an OpenVPN server on a DigitalOcean Droplet, be aware that we, like many hosting providers, charge for bandwidth overages. For this reason, please be mindful of how much traffic your server is handling.

      See this page for more info.

      Prerequisites

      To complete this tutorial, you will need access to a Debian 10 server to host your OpenVPN service. You will need to configure a non-root user with sudo privileges before you start this guide. You can follow our Debian 10 initial server setup guide to set up a user with appropriate permissions. The linked tutorial will also set up a firewall, which is assumed to be in place throughout this guide.

      Additionally, you will need a separate machine to serve as your certificate authority (CA). While it’s technically possible to use your OpenVPN server or your local machine as your CA, this is not recommended as it opens up your VPN to some security vulnerabilities. Per the official OpenVPN documentation, you should place your CA on a standalone machine that’s dedicated to importing and signing certificate requests. For this reason, this guide assumes that your CA is on a separate Debian 10 server that also has a non-root user with sudo privileges and a basic firewall.

      Please note that if you disable password authentication while configuring these servers, you may run into difficulties when transferring files between them later on in this guide. To resolve this issue, you could re-enable password authentication on each server. Alternatively, you could generate an SSH keypair for each server, then add the OpenVPN server’s public SSH key to the CA machine’s authorized_keys file and vice versa. See How to Set Up SSH Keys on Debian 10 for instructions on how to perform either of these solutions.

      When you have these prerequisites in place, you can move on to Step 1 of this tutorial.

      Step 1 — Installing OpenVPN and EasyRSA

      To start off, update your VPN server’s package index and install OpenVPN. OpenVPN is available in Debian’s default repositories, so you can use apt for the installation:

      • sudo apt update
      • sudo apt install openvpn

      OpenVPN is a TLS/SSL VPN. This means that it utilizes certificates in order to encrypt traffic between the server and clients. To issue trusted certificates, you will set up your own simple certificate authority (CA). To do this, we will download the latest version of EasyRSA, which we will use to build our CA public key infrastructure (PKI), from the project’s official GitHub repository.

      As mentioned in the prerequisites, we will build the CA on a standalone server. The reason for this approach is that, if an attacker were able to infiltrate your server, they would be able to access your CA private key and use it to sign new certificates, giving them access to your VPN. Accordingly, managing the CA from a standalone machine helps to prevent unauthorized users from accessing your VPN. Note, as well, that it’s recommended that you keep the CA server turned off when not being used to sign keys as a further precautionary measure.

      To begin building the CA and PKI infrastructure, use wget to download the latest version of EasyRSA on both your CA machine and your OpenVPN server. To get the latest version, go to the Releases page on the official EasyRSA GitHub project, copy the download link for the file ending in .tgz, and then paste it into the following command:

      • wget -P ~/ https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.6/EasyRSA-unix-v3.0.6.tgz

      Then extract the tarball:

      • cd ~
      • tar xvf EasyRSA-unix-v3.0.6.tgz

      You have successfully installed all the required software on your server and CA machine. Continue on to configure the variables used by EasyRSA and to set up a CA directory, from which you will generate the keys and certificates needed for your server and clients to access the VPN.

      Step 2 — Configuring the EasyRSA Variables and Building the CA

      EasyRSA comes installed with a configuration file which you can edit to define a number of variables for your CA.

      On your CA machine, navigate to the EasyRSA directory:

      Inside this directory is a file named vars.example. Make a copy of this file, and name the copy vars without a file extension:

      Open this new file using your preferred text editor:

      Find the settings that set field defaults for new certificates. It will look something like this:

      ~/EasyRSA-v3.0.6/vars

      . . .
      
      #set_var EASYRSA_REQ_COUNTRY    "US"
      #set_var EASYRSA_REQ_PROVINCE   "California"
      #set_var EASYRSA_REQ_CITY       "San Francisco"
      #set_var EASYRSA_REQ_ORG        "Copyleft Certificate Co"
      #set_var EASYRSA_REQ_EMAIL      "[email protected]"
      #set_var EASYRSA_REQ_OU         "My Organizational Unit"
      
      . . .
      

      Uncomment these lines and update the highlighted values to whatever you'd prefer, but do not leave them blank:

      ~/EasyRSA-v3.0.6/vars

      . . .
      
      set_var EASYRSA_REQ_COUNTRY    "US"
      set_var EASYRSA_REQ_PROVINCE   "NewYork"
      set_var EASYRSA_REQ_CITY       "New York City"
      set_var EASYRSA_REQ_ORG        "DigitalOcean"
      set_var EASYRSA_REQ_EMAIL      "[email protected]"
      set_var EASYRSA_REQ_OU         "Community"
      
      . . .
      

      When you are finished, save and close the file.

      Within the EasyRSA directory is a script called easyrsa which is called to perform a variety of tasks involved with building and managing the CA. Run this script with the init-pki option to initiate the public key infrastructure on the CA server:

      Output

      . . . init-pki complete; you may now create a CA or requests. Your newly created PKI dir is: /home/sammy/EasyRSA-v3.0.6/pki

      After this, call the easyrsa script again, following it with the build-ca option. This will build the CA and create two important files — ca.crt and ca.key — which make up the public and private sides of an SSL certificate.

      • ca.crt is the CA’s public certificate file which, in the context of OpenVPN, the server and the client use to inform one another that they are part of the same web of trust and not someone performing a man-in-the-middle attack. For this reason, your server and all of your clients will need a copy of the ca.crt file.
      • ca.key is the private key which the CA machine uses to sign keys and certificates for servers and clients. If an attacker gains access to your CA and, in turn, your ca.key file, they will be able to sign certificate requests and gain access to your VPN, impeding its security. This is why your ca.key file should only be on your CA machine and that, ideally, your CA machine should be kept offline when not signing certificate requests as an extra security measure.

      If you don’t want to be prompted for a password every time you interact with your CA, you can run the build-ca command with the nopass option, like this:

      • ./easyrsa build-ca nopass

      In the output, you’ll be asked to confirm the common name for your CA:

      Output

      . . . Common Name (eg: your user, host, or server name) [Easy-RSA CA]:

      The common name is the name used to refer to this machine in the context of the certificate authority. You can enter any string of characters for the CA’s common name but, for simplicity’s sake, press ENTER to accept the default name.

      With that, your CA is in place and it’s ready to start signing certificate requests.

      Step 3 — Creating the Server Certificate, Key, and Encryption Files

      Now that you have a CA ready to go, you can generate a private key and certificate request from your server and then transfer the request over to your CA to be signed, creating the required certificate. You’re also free to create some additional files used during the encryption process.

      Start by navigating to the EasyRSA directory on your OpenVPN server:

      From there, run the easyrsa script with the init-pki option. Although you already ran this command on the CA machine, it’s necessary to run it here because your server and CA will have separate PKI directories:

      Then call the easyrsa script again, this time with the gen-req option followed by a common name for the machine. Again, this could be anything you like but it can be helpful to make it something descriptive. Throughout this tutorial, the OpenVPN server’s common name will simply be “server”. Be sure to include the nopass option as well. Failing to do so will password-protect the request file which could lead to permissions issues later on:

      Note: If you choose a name other than “server” here, you will have to adjust some of the instructions below. For instance, when copying the generated files to the /etc/openvpn directory, you will have to substitute the correct names. You will also have to modify the /etc/openvpn/server.conf file later to point to the correct .crt and .key files.

      • ./easyrsa gen-req server nopass

      This will create a private key for the server and a certificate request file called server.req. Copy the server key to the /etc/openvpn/ directory:

      • sudo cp ~/EasyRSA-v3.0.6/pki/private/server.key /etc/openvpn/

      Using a secure method (like SCP, in our example below), transfer the server.req file to your CA machine:

      • scp ~/EasyRSA-v3.0.6/pki/reqs/server.req sammy@your_CA_ip:/tmp

      Next, on your CA machine, navigate to the EasyRSA directory:

      Using the easyrsa script again, import the server.req file, following the file path with its common name:

      • ./easyrsa import-req /tmp/server.req server

      Then sign the request by running the easyrsa script with the sign-req option, followed by the request type and the common name. The request type can either be client or server, so for the OpenVPN server’s certificate request, be sure to use the server request type:

      • ./easyrsa sign-req server server

      In the output, you’ll be asked to verify that the request comes from a trusted source. Type yes then press ENTER to confirm this:

      You are about to sign the following certificate.
      Please check over the details shown below for accuracy. Note that this request
      has not been cryptographically verified. Please be sure it came from a trusted
      source or that you have verified the request checksum with the sender.
      
      Request subject, to be signed as a server certificate for 1080 days:
      
      subject=
          commonName                = server
      
      
      Type the word 'yes' to continue, or any other input to abort.
        Confirm request details: yes
      

      If you encrypted your CA key, you’ll be prompted for your password at this point.

      Next, transfer the signed certificate back to your VPN server using a secure method:

      • scp pki/issued/server.crt sammy@your_server_ip:/tmp

      Before logging out of your CA machine, transfer the ca.crt file to your server as well:

      • scp pki/ca.crt sammy@your_server_ip:/tmp

      Next, log back into your OpenVPN server and copy the server.crt and ca.crt files into your /etc/openvpn/ directory:

      • sudo cp /tmp/{server.crt,ca.crt} /etc/openvpn/

      Then navigate to your EasyRSA directory:

      From there, create a strong Diffie-Hellman key to use during key exchange by typing:

      This may take a few minutes to complete. Once it does, generate an HMAC signature to strengthen the server's TLS integrity verification capabilities:

      • sudo openvpn --genkey --secret ta.key

      When the command finishes, copy the two new files to your /etc/openvpn/ directory:

      • sudo cp ~/EasyRSA-v3.0.6/ta.key /etc/openvpn/
      • sudo cp ~/EasyRSA-v3.0.6/pki/dh.pem /etc/openvpn/

      With that, all the certificate and key files needed by your server have been generated. You’re ready to create the corresponding certificates and keys which your client machine will use to access your OpenVPN server.

      Step 4 — Generating a Client Certificate and Key Pair

      Although you can generate a private key and certificate request on your client machine and then send it to the CA to be signed, this guide outlines a process for generating the certificate request on the server. The benefit of this is that we can create a script which will automatically generate client configuration files that contain all of the required keys and certificates. This lets you avoid having to transfer keys, certificates, and configuration files to clients and streamlines the process of joining the VPN.

      We will generate a single client key and certificate pair for this guide. If you have more than one client, you can repeat this process for each one. Please note, though, that you will need to pass a unique name value to the script for every client. Throughout this tutorial, the first certificate/key pair is referred to as client1.

      Get started by creating a directory structure within your home directory to store the client certificate and key files:

      • mkdir -p ~/client-configs/keys

      Since you will store your clients’ certificate/key pairs and configuration files in this directory, you should lock down its permissions now as a security measure:

      • chmod -R 700 ~/client-configs

      Next, navigate back to the EasyRSA directory and run the easyrsa script with the gen-req and nopass options, along with the common name for the client:

      • cd ~/EasyRSA-v3.0.6/
      • ./easyrsa gen-req client1 nopass

      Press ENTER to confirm the common name. Then, copy the client1.key file to the /client-configs/keys/ directory you created earlier:

      • cp pki/private/client1.key ~/client-configs/keys/

      Next, transfer the client1.req file to your CA machine using a secure method:

      • scp pki/reqs/client1.req sammy@your_CA_ip:/tmp

      Log in to your CA machine, navigate to the EasyRSA directory, and import the certificate request:

      • ssh sammy@your_CA_ip
      • cd EasyRSA-v3.0.6/
      • ./easyrsa import-req /tmp/client1.req client1

      Then sign the request as you did for the server in the previous step. This time, though, be sure to specify the client request type:

      • ./easyrsa sign-req client client1

      At the prompt, enter yes to confirm that you intend to sign the certificate request and that it came from a trusted source:

      Output

      Type the word 'yes' to continue, or any other input to abort. Confirm request details: yes

      Again, if you encrypted your CA key, you’ll be prompted for your password here.

      This will create a client certificate file named client1.crt. Transfer this file back to the server:

      • scp pki/issued/client1.crt sammy@your_server_ip:/tmp

      SSH back into your OpenVPN server and copy the client certificate to the /client-configs/keys/ directory:

      • cp /tmp/client1.crt ~/client-configs/keys/

      Next, copy the ca.crt and ta.key files to the /client-configs/keys/ directory as well:

      • sudo cp ~/EasyRSA-v3.0.6/ta.key ~/client-configs/keys/
      • sudo cp /etc/openvpn/ca.crt ~/client-configs/keys/

      With that, your server and client’s certificates and keys have all been generated and are stored in the appropriate directories on your server. There are still a few actions that need to be performed with these files, but those will come in a later step. For now, you can move on to configuring OpenVPN on your server.

      Step 5 — Configuring the OpenVPN Service

      Now that both your client and server’s certificates and keys have been generated, you can begin configuring the OpenVPN service to use these credentials.

      Start by copying a sample OpenVPN configuration file into the configuration directory and then extract it in order to use it as a basis for your setup:

      • sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
      • sudo gzip -d /etc/openvpn/server.conf.gz

      Open the server configuration file in your preferred text editor:

      • sudo nano /etc/openvpn/server.conf

      Find the HMAC section by looking for the tls-auth directive. This line should already be uncommented, but if isn’t then remove the ";" to uncomment it. Below this line, add the key-direction parameter, set to "0":

      /etc/openvpn/server.conf

      tls-auth ta.key 0 # This file is secret
      key-direction 0
      

      Next, find the section on cryptographic ciphers by looking for the commented out cipher lines. The AES-256-CBC cipher offers a good level of encryption and is well supported. Again, this line should already be uncommented, but if it isn’t then just remove the ";" preceding it:

      /etc/openvpn/server.conf

      cipher AES-256-CBC
      

      Below this, add an auth directive to select the HMAC message digest algorithm. For this, SHA256 is a good choice:

      /etc/openvpn/server.conf

      auth SHA256
      

      Next, find the line containing a dh directive which defines the Diffie-Hellman parameters. Because of some recent changes made to EasyRSA, the filename for the Diffie-Hellman key may be different than what is listed in the example server configuration file. If necessary, change the file name listed here by removing the 2048 so it aligns with the key you generated in the previous step:

      /etc/openvpn/server.conf

      dh dh.pem
      

      Finally, find the user and group settings and remove the ";" at the beginning of each to uncomment these lines:

      /etc/openvpn/server.conf

      user nobody
      group nogroup
      

      The changes you’ve made to the sample server.conf file up to this point are necessary in order for OpenVPN to function. The changes outlined below are optional, though they too are needed for many common use cases.

      (Optional) Push DNS Changes to Redirect All Traffic Through the VPN

      The settings above will create the VPN connection between the two machines, but will not force any connections to use the tunnel. If you wish to use the VPN to route all of your traffic, you will likely want to push the DNS settings to the client computers.

      There are a few directives in the server.conf file which you must change in order to enable this functionality. Find the redirect-gateway section and remove the semicolon ";" from the beginning of the redirect-gateway line to uncomment it:

      /etc/openvpn/server.conf

      push "redirect-gateway def1 bypass-dhcp"
      

      Just below this, find the dhcp-option section. Again, remove the ";" from in front of both of the lines to uncomment them:

      /etc/openvpn/server.conf

      push "dhcp-option DNS 208.67.222.222"
      push "dhcp-option DNS 208.67.220.220"
      

      This will assist clients in reconfiguring their DNS settings to use the VPN tunnel for as the default gateway.

      (Optional) Adjust the Port and Protocol

      By default, the OpenVPN server uses port 1194 and the UDP protocol to accept client connections. If you need to use a different port because of restrictive network environments that your clients might be in, you can change the port option. If you are not hosting web content on your OpenVPN server, port 443 is a popular choice since it is usually allowed through firewall rules.

      /etc/openvpn/server.conf

      # Optional!
      port 443
      

      Oftentimes, the protocol is restricted to that port as well. If so, change proto from UDP to TCP:

      /etc/openvpn/server.conf

      # Optional!
      proto tcp
      

      If you do switch the protocol to TCP, you will need to change the explicit-exit-notify directive’s value from 1 to 0, as this directive is only used by UDP. Failing to do so while using TCP will cause errors when you start the OpenVPN service:

      /etc/openvpn/server.conf

      # Optional!
      explicit-exit-notify 0
      

      If you have no need to use a different port and protocol, it is best to leave these two settings as their defaults.

      (Optional) Point to Non-Default Credentials

      If you selected a different name during the ./build-key-server command earlier, modify the cert and key lines that you see to point to the appropriate .crt and .key files. If you used the default name, “server”, this is already set correctly:

      /etc/openvpn/server.conf

      cert server.crt
      key server.key
      

      When you are finished, save and close the file.

      After going through and making whatever changes to your server’s OpenVPN configuration are required for your specific use case, you can begin making some changes to your server’s networking.

      Step 6 — Adjusting the Server Networking Configuration

      There are some aspects of the server’s networking configuration that need to be tweaked so that OpenVPN can correctly route traffic through the VPN. The first of these is IP forwarding, a method for determining where IP traffic should be routed. This is essential to the VPN functionality that your server will provide.

      Adjust your server’s default IP forwarding setting by modifying the /etc/sysctl.conf file:

      • sudo nano /etc/sysctl.conf

      Inside, look for the commented line that sets net.ipv4.ip_forward. Remove the "#" character from the beginning of the line to uncomment this setting:

      /etc/sysctl.conf

      net.ipv4.ip_forward=1
      

      Save and close the file when you are finished.

      To read the file and adjust the values for the current session, type:

      Output

      net.ipv4.ip_forward = 1

      If you followed the Debian 10 initial server setup guide listed in the prerequisites, you should have a UFW firewall in place. Regardless of whether you use the firewall to block unwanted traffic (which you almost always should do), for this guide you need a firewall to manipulate some of the traffic coming into the server. Some of the firewall rules must be modified to enable masquerading, an iptables concept that provides on-the-fly dynamic network address translation (NAT) to correctly route client connections.

      Before opening the firewall configuration file to add the masquerading rules, you must first find the public network interface of your machine. To do this, type:

      Your public interface is the string found within this command’s output that follows the word "dev". For example, this result shows the interface named wlp11s0, which is highlighted below:

      Output

      default via 203.0.113.1 dev eth0 proto static

      When you have the interface associated with your default route, open the /etc/ufw/before.rules file to add the relevant configuration:

      • sudo nano /etc/ufw/before.rules

      UFW rules are typically added using the ufw command. Rules listed in the before.rules file, though, are read and put into place before the conventional UFW rules are loaded. Towards the top of the file, add the highlighted lines below. This will set the default policy for the POSTROUTING chain in the nat table and masquerade any traffic coming from the VPN. Remember to replace wlp11s0 in the -A POSTROUTING line below with the interface you found in the above command:

      /etc/ufw/before.rules

      #
      # rules.before
      #
      # Rules that should be run before the ufw command line added rules. Custom
      # rules should be added to one of these chains:
      #   ufw-before-input
      #   ufw-before-output
      #   ufw-before-forward
      #
      
      # START OPENVPN RULES
      # NAT table rules
      *nat
      :POSTROUTING ACCEPT [0:0] 
      # Allow traffic from OpenVPN client to eth0 (change to the interface you discovered!)
      -A POSTROUTING -s 10.8.0.0/8 -o eth0 -j MASQUERADE
      COMMIT
      # END OPENVPN RULES
      
      # Don't delete these required lines, otherwise there will be errors
      *filter
      . . .
      

      Save and close the file when you are finished.

      Next, you need to tell UFW to allow forwarded packets by default as well. To do this, open the /etc/default/ufw file:

      • sudo nano /etc/default/ufw

      Inside, find the DEFAULT_FORWARD_POLICY directive and change the value from DROP to ACCEPT:

      /etc/default/ufw

      DEFAULT_FORWARD_POLICY="ACCEPT"
      

      Save and close the file when you are finished.

      Next, adjust the firewall itself to allow traffic to OpenVPN. If you did not change the port and protocol in the /etc/openvpn/server.conf file, you will need to open up UDP traffic to port 1194. If you modified the port and/or protocol, substitute the values you selected here.

      Also, in case you didn't add the SSH port when completing the prerequisite tutorial, add it here as well:

      • sudo ufw allow 1194/udp
      • sudo ufw allow OpenSSH

      After adding those rules, disable and re-enable UFW to restart it and load the changes from all of the files you've modified:

      • sudo ufw disable
      • sudo ufw enable

      Your server is now configured to correctly handle OpenVPN traffic.

      Step 7 — Starting and Enabling the OpenVPN Service

      You're finally ready to start the OpenVPN service on your server. This is done using the systemd utility systemctl.

      Start the OpenVPN server by specifying your configuration file name as an instance variable after the systemd unit file name. The configuration file for your server is called /etc/openvpn/server.conf, so add @server to end of your unit file when calling it:

      • sudo systemctl start openvpn@server

      Double-check that the service has started successfully by typing:

      • sudo systemctl status openvpn@server

      If everything went well, your output will look something like this:

      Output

      [email protected] - OpenVPN connection to server Loaded: loaded (/lib/systemd/system/[email protected]; disabled; vendor preset: enabled) Active: active (running) since Wed 2019-07-17 03:39:24 UTC; 29s ago Docs: man:openvpn(8) https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage https://community.openvpn.net/openvpn/wiki/HOWTO Main PID: 3371 (openvpn) Status: "Initialization Sequence Completed" Tasks: 1 (limit: 3587) Memory: 1.2M CGroup: /system.slice/system-openvpn.slice/[email protected] └─3371 /usr/sbin/openvpn --daemon ovpn-server --status /run/openvpn/server.status 10 --cd /etc/openvpn --config /etc/openvpn/server.conf --writepid /run/openvpn/

      You can also check that the OpenVPN tun0 interface is available by typing:

      This will output a configured interface:

      Output

      3: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 100 link/none inet 10.8.0.1 peer 10.8.0.2/32 scope global tun0 valid_lft forever preferred_lft forever inet6 fe80::dd60:3a78:b0ca:1659/64 scope link stable-privacy valid_lft forever preferred_lft forever

      After starting the service, enable it so that it starts automatically at boot:

      • sudo systemctl enable openvpn@server

      Your OpenVPN service is now up and running. Before you can start using it, though, you must first create a configuration file for the client machine. This tutorial already went over how to create certificate/key pairs for clients, and in the next step we will demonstrate how to create an infrastructure that will generate client configuration files easily.

      Step 8 — Creating the Client Configuration Infrastructure

      Creating configuration files for OpenVPN clients can be somewhat involved, as every client must have its own config and each must align with the settings outlined in the server’s configuration file. Rather than writing a single configuration file that can only be used on one client, this step outlines a process for building a client configuration infrastructure which you can use to generate config files on-the-fly. You will first create a “base” configuration file then build a script which will allow you to generate unique client config files, certificates, and keys as needed.

      Get started by creating a new directory where you will store client configuration files within the client-configs directory you created earlier:

      • mkdir -p ~/client-configs/files

      Next, copy an example client configuration file into the client-configs directory to use as your base configuration:

      • cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/client-configs/base.conf

      Open this new file in your text editor:

      • nano ~/client-configs/base.conf

      Inside, locate the remote directive. This points the client to your OpenVPN server address — the public IP address of your OpenVPN server. If you decided to change the port that the OpenVPN server is listening on, you will also need to change 1194 to the port you selected:

      ~/client-configs/base.conf

      . . .
      # The hostname/IP and port of the server.
      # You can have multiple remote entries
      # to load balance between the servers.
      remote your_server_ip 1194
      . . .
      

      Be sure that the protocol matches the value you are using in the server configuration:

      ~/client-configs/base.conf

      proto udp
      

      Next, uncomment the user and group directives by removing the ";" at the beginning of each line:

      ~/client-configs/base.conf

      # Downgrade privileges after initialization (non-Windows only)
      user nobody
      group nogroup
      

      Find the directives that set the ca, cert, and key. Comment out these directives since you will add the certs and keys within the file itself shortly:

      ~/client-configs/base.conf

      # SSL/TLS parms.
      # See the server config file for more
      # description.  It's best to use
      # a separate .crt/.key file pair
      # for each client.  A single ca
      # file can be used for all clients.
      #ca ca.crt
      #cert client.crt
      #key client.key
      

      Similarly, comment out the tls-auth directive, as you will add ta.key directly into the client configuration file:

      ~/client-configs/base.conf

      # If a tls-auth key is used on the server
      # then every client must also have the key.
      #tls-auth ta.key 1
      

      Mirror the cipher and auth settings that you set in the /etc/openvpn/server.conf file:

      ~/client-configs/base.conf

      cipher AES-256-CBC
      auth SHA256
      

      Next, add the key-direction directive somewhere in the file. You must set this to "1" for the VPN to function correctly on the client machine:

      ~/client-configs/base.conf

      key-direction 1
      

      Finally, add a few commented out lines. Although you can include these directives in every client configuration file, you only need to enable them for Linux clients that ship with an /etc/openvpn/update-resolv-conf file. This script uses the resolvconf utility to update DNS information for Linux clients.

      ~/client-configs/base.conf

      # script-security 2
      # up /etc/openvpn/update-resolv-conf
      # down /etc/openvpn/update-resolv-conf
      

      If your client is running Linux and has an /etc/openvpn/update-resolv-conf file, uncomment these lines from the client’s configuration file after it has been generated.

      Save and close the file when you are finished.

      Next, create a simple script that will compile your base configuration with the relevant certificate, key, and encryption files and then place the generated configuration in the ~/client-configs/files directory. Open a new file called make_config.sh within the ~/client-configs directory:

      • nano ~/client-configs/make_config.sh

      Inside, add the following content, making sure to change sammy to that of your server’s non-root user account:

      ~/client-configs/make_config.sh

      #!/bin/bash
      
      # First argument: Client identifier
      
      KEY_DIR=/home/sammy/client-configs/keys
      OUTPUT_DIR=/home/sammy/client-configs/files
      BASE_CONFIG=/home/sammy/client-configs/base.conf
      
      cat ${BASE_CONFIG} 
          <(echo -e '<ca>') 
          ${KEY_DIR}/ca.crt 
          <(echo -e '</ca>n<cert>') 
          ${KEY_DIR}/${1}.crt 
          <(echo -e '</cert>n<key>') 
          ${KEY_DIR}/${1}.key 
          <(echo -e '</key>n<tls-auth>') 
          ${KEY_DIR}/ta.key 
          <(echo -e '</tls-auth>') 
          > ${OUTPUT_DIR}/${1}.ovpn
      

      Save and close the file when you are finished.

      Before moving on, be sure to mark this file as executable by typing:

      • chmod 700 ~/client-configs/make_config.sh

      This script will make a copy of the base.conf file you made, collect all the certificate and key files you’ve created for your client, extract their contents, append them to the copy of the base configuration file, and export all of this content into a new client configuration file. This means that, rather than having to manage the client’s configuration, certificate, and key files separately, all the required information is stored in one place. The benefit of this is that if you ever need to add a client in the future, you can just run this script to quickly create the config file and ensure that all the important information is stored in a single, easy-to-access location.

      Please note that any time you add a new client, you will need to generate new keys and certificates for it before you can run this script and generate its configuration file. You will get some practice using this script in the next step.

      Step 9 — Generating Client Configurations

      If you followed along with the guide, you created a client certificate and key named client1.crt and client1.key, respectively, in Step 4. You can generate a config file for these credentials by moving into your ~/client-configs directory and running the script you made at the end of the previous step:

      • cd ~/client-configs
      • sudo ./make_config.sh client1

      This will create a file named client1.ovpn in your ~/client-configs/files directory:

      • ls ~/client-configs/files

      Output

      client1.ovpn

      You need to transfer this file to the device you plan to use as the client. For instance, this could be your local computer or a mobile device.

      While the exact applications used to accomplish this transfer will depend on your device's operating system and your personal preferences, a dependable and secure method is to use SFTP (SSH file transfer protocol) or SCP (Secure Copy) on the backend. This will transport your client's VPN authentication files over an encrypted connection.

      Here is an example SFTP command using the client1.ovpn example which you can run from your local computer (macOS or Linux). It places the .ovpn file in your home directory:

      • sftp sammy@your_server_ip:client-configs/files/client1.ovpn ~/

      Here are several tools and tutorials for securely transferring files from the server to a local computer:

      Step 10 — Installing the Client Configuration

      This section covers how to install a client VPN profile on Windows, macOS, Linux, iOS, and Android. None of these client instructions are dependent on one another, so feel free to skip to whichever is applicable to your device.

      The OpenVPN connection will have the same name as whatever you called the .ovpn file. In regards to this tutorial, this means that the connection is named client1.ovpn, aligning with the first client file you generated.

      Windows

      Installing

      Download the OpenVPN client application for Windows from OpenVPN's Downloads page. Choose the appropriate installer version for your version of Windows.

      Note: OpenVPN needs administrative privileges to install.

      After installing OpenVPN, copy the .ovpn file to:

      C:Program FilesOpenVPNconfig
      

      When you launch OpenVPN, it will automatically see the profile and make it available.

      You must run OpenVPN as an administrator each time it's used, even by administrative accounts. To do this without having to right-click and select Run as administrator every time you use the VPN, you must preset this from an administrative account. This also means that standard users will need to enter the administrator's password to use OpenVPN. On the other hand, standard users can't properly connect to the server unless the OpenVPN application on the client has admin rights, so the elevated privileges are necessary.

      To set the OpenVPN application to always run as an administrator, right-click on its shortcut icon and go to Properties. At the bottom of the Compatibility tab, click the button to Change settings for all users. In the new window, check Run this program as an administrator.

      Connecting

      Each time you launch the OpenVPN GUI, Windows will ask if you want to allow the program to make changes to your computer. Click Yes. Launching the OpenVPN client application only puts the applet in the system tray so that you can connect and disconnect the VPN as needed; it does not actually make the VPN connection.

      Once OpenVPN is started, initiate a connection by going into the system tray applet and right-clicking on the OpenVPN applet icon. This opens the context menu. Select client1 at the top of the menu (that's your client1.ovpn profile) and choose Connect.

      A status window will open showing the log output while the connection is established, and a message will show once the client is connected.

      Disconnect from the VPN the same way: Go into the system tray applet, right-click the OpenVPN applet icon, select the client profile and click Disconnect.

      macOS

      Installing

      Tunnelblick is a free, open source OpenVPN client for macOS. You can download the latest disk image from the Tunnelblick Downloads page. Double-click the downloaded .dmg file and follow the prompts to install.

      Towards the end of the installation process, Tunnelblick will ask if you have any configuration files. Answer I have configuration files and let Tunnelblick finish. Open a Finder window and double-click client1.ovpn. Tunnelblick will install the client profile. Administrative privileges are required.

      Connecting

      Launch Tunnelblick by double-clicking the Tunnelblick icon in the Applications folder. Once Tunnelblick has been launched, there will be a Tunnelblick icon in the menu bar at the top right of the screen for controlling connections. Click on the icon, and then the Connect client1 menu item to initiate the VPN connection.

      Linux

      Installing

      If you are using Linux, there are a variety of tools that you can use depending on your distribution. Your desktop environment or window manager might also include connection utilities.

      The most universal way of connecting, however, is to just use the OpenVPN software.

      On Debian, you can install it just as you did on the server by typing:

      • sudo apt update
      • sudo apt install openvpn

      On CentOS you can enable the EPEL repositories and then install it by typing:

      • sudo yum install epel-release
      • sudo yum install openvpn

      Configuring

      Check to see if your distribution includes an /etc/openvpn/update-resolv-conf script:

      Output

      update-resolv-conf

      Next, edit the OpenVPN client configuration file you transfered:

      If you were able to find an update-resolv-conf file, uncomment the three lines you added to adjust the DNS settings:

      client1.ovpn

      script-security 2
      up /etc/openvpn/update-resolv-conf
      down /etc/openvpn/update-resolv-conf
      

      If you are using CentOS, change the group directive from nogroup to nobody to match the distribution's available groups:

      client1.ovpn

      group nobody
      

      Save and close the file.

      Now, you can connect to the VPN by just pointing the openvpn command to the client configuration file:

      • sudo openvpn --config client1.ovpn

      This should connect you to your VPN.

      iOS

      Installing

      From the iTunes App Store, search for and install OpenVPN Connect, the official iOS OpenVPN client application. To transfer your iOS client configuration onto the device, connect it directly to a computer.

      The process of completing the transfer with iTunes is outlined here. Open iTunes on the computer and click on iPhone > apps. Scroll down to the bottom to the File Sharing section and click the OpenVPN app. The blank window to the right, OpenVPN Documents, is for sharing files. Drag the .ovpn file to the OpenVPN Documents window.

      iTunes showing the VPN profile ready to load on the iPhone

      Now launch the OpenVPN app on the iPhone. You will receive a notification that a new profile is ready to import. Tap the green plus sign to import it.

      The OpenVPN iOS app showing new profile ready to import

      Connecting

      OpenVPN is now ready to use with the new profile. Start the connection by sliding the Connect button to the On position. Disconnect by sliding the same button to Off.

      Note: The VPN switch under Settings cannot be used to connect to the VPN. If you try, you will receive a notice to only connect using the OpenVPN app.

      The OpenVPN iOS app connected to the VPN

      Android

      Installing

      Open the Google Play Store. Search for and install Android OpenVPN Connect, the official Android OpenVPN client application.

      You can transfer the .ovpn profile by connecting the Android device to your computer by USB and copying the file over. Alternatively, if you have an SD card reader, you can remove the device's SD card, copy the profile onto it and then insert the card back into the Android device.

      Start the OpenVPN app and tap the menu to import the profile.

      The OpenVPN Android app profile import menu selection

      Then navigate to the location of the saved profile (the screenshot uses /sdcard/Download/) and select the file. The app will make a note that the profile was imported.

      The OpenVPN Android app selecting VPN profile to import

      Connecting

      To connect, simply tap the Connect button. You'll be asked if you trust the OpenVPN application. Choose OK to initiate the connection. To disconnect from the VPN, go back to the OpenVPN app and choose Disconnect.

      The OpenVPN Android app ready to connect to the VPN

      Step 11 — Testing Your VPN Connection (Optional)

      Note: This method for testing your VPN connection will only work if you opted to route all your traffic through the VPN in Step 5.

      Once everything is installed, a simple check confirms everything is working properly. Without having a VPN connection enabled, open a browser and go to DNSLeakTest.

      The site will return the IP address assigned by your internet service provider and as you appear to the rest of the world. To check your DNS settings through the same website, click on Extended Test and it will tell you which DNS servers you are using.

      Now connect the OpenVPN client to your Droplet's VPN and refresh the browser. A completely different IP address (that of your VPN server) should now appear, and this is how you appear to the world. Again, DNSLeakTest's Extended Test will check your DNS settings and confirm you are now using the DNS resolvers pushed by your VPN.

      Step 12 — Revoking Client Certificates

      Occasionally, you may need to revoke a client certificate to prevent further access to the OpenVPN server.

      To do so, navigate to the EasyRSA directory on your CA machine:

      Next, run the easyrsa script with the revoke option, followed by the client name you wish to revoke:

      This will ask you to confirm the revocation by entering yes:

      Output

      Please confirm you wish to revoke the certificate with the following subject: subject= commonName = client2 Type the word 'yes' to continue, or any other input to abort. Continue with revocation: yes

      After confirming the action, the CA will fully revoke the client’s certificate. However, your OpenVPN server currently has no way to check whether any clients’ certificates have been revoked and the client will still have access to the VPN. To correct this, create a certificate revocation list (CRL) on your CA machine:

      This will generate a file called crl.pem. Securely transfer this file to your OpenVPN server:

      • scp ~/EasyRSA-v3.0.6/pki/crl.pem sammy@your_server_ip:/tmp

      On your OpenVPN server, copy this file into your /etc/openvpn/ directory:

      • sudo cp /tmp/crl.pem /etc/openvpn

      Next, open the OpenVPN server configuration file:

      • sudo nano /etc/openvpn/server.conf

      At the bottom of the file, add the crl-verify option, which will instruct the OpenVPN server to check the certificate revocation list that we've created each time a connection attempt is made:

      /etc/openvpn/server.conf

      crl-verify crl.pem
      

      Save and close the file.

      Finally, restart OpenVPN to implement the certificate revocation:

      • sudo systemctl restart openvpn@server

      The client should no longer be able to successfully connect to the server using the old credential.

      To revoke additional clients, follow this process:

      1. Revoke the certificate with the ./easyrsa revoke client_name command
      2. Generate a new CRL
      3. Transfer the new crl.pem file to your OpenVPN server and copy it to the /etc/openvpn directory to overwrite the old list.
      4. Restart the OpenVPN service.

      You can use this process to revoke any certificates that you've previously issued for your server.

      Conclusion

      You are now securely traversing the internet protecting your identity, location, and traffic from snoopers and censors.

      To configure more clients, you only need to follow steps 4 and 9-11 for each additional device. To revoke access to clients, just follow step 12.



      Source link

      How To Install the Apache Web Server on Debian 10


      Introduction

      The Apache HTTP server is the most widely-used web server in the world. It provides many powerful features including dynamically loadable modules, robust media support, and extensive integration with other popular software.

      In this guide, we’ll explain how to install an Apache web server on your Debian 10 server.

      Prerequisites

      Before you begin this guide, you should have a regular, non-root user with sudo privileges configured on your server. Additionally, you will need to enable a basic firewall to block non-essential ports. You can learn how to configure a regular user account and set up a firewall for your server by following our initial server setup guide for Debian 10.

      When you have an account available, log in as your non-root user to begin.

      Step 1 — Installing Apache

      Apache is available within Debian’s default software repositories, making it possible to install it using conventional package management tools.

      Let’s begin by updating the local package index to reflect the latest upstream changes:

      Then, install the apache2 package:

      After confirming the installation, apt will install Apache and all required dependencies.

      Step 2 — Adjusting the Firewall

      Before testing Apache, it's necessary to modify the firewall settings to allow outside access to the default web ports. Assuming that you followed the instructions in the prerequisites, you should have a UFW firewall configured to restrict access to your server.

      During installation, Apache registers itself with UFW to provide a few application profiles that can be used to enable or disable access to Apache through the firewall.

      List the ufw application profiles by typing:

      You will see a list of the application profiles:

      Output

      Available applications: AIM Bonjour CIFS . . . WWW WWW Cache WWW Full WWW Secure . . .

      The Apache profiles begin with WWW:

      • WWW: This profile opens only port 80 (normal, unencrypted web traffic)
      • WWW Cache: This profile opens only port 8080 (sometimes used for caching and web proxies)
      • WWW Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
      • WWW Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)

      It is recommended that you enable the most restrictive profile that will still allow the traffic you've configured. Since we haven't configured SSL for our server yet in this guide, we will only need to allow traffic on port 80:

      You can verify the change by typing:

      You should see HTTP traffic allowed in the displayed output:

      Output

      Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere WWW ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) WWW (v6) ALLOW Anywhere (v6)

      As you can see, the profile has been activated to allow access to the web server.

      Step 3 — Checking your Web Server

      At the end of the installation process, Debian 10 starts Apache. The web server should already be up and running.

      Check with the systemd init system to make sure the service is running by typing:

      • sudo systemctl status apache2

      Output

      ● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2019-07-19 15:33:01 UTC; 4min 13s ago Docs: https://httpd.apache.org/docs/2.4/ . . . Jul 19 15:33:01 debssh systemd[1]: Starting The Apache HTTP Server... Jul 19 15:33:01 debssh apachectl[2791]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive global Jul 19 15:33:01 debssh systemd[1]: Started The Apache HTTP Server.

      As you can see from this output, the service appears to have started successfully. However, the best way to test this is to request a page from Apache.

      You can access the default Apache landing page to confirm that the software is running properly through your IP address. If you do not know your server's IP address, you can get it a few different ways from the command line.

      Try typing this at your server's command prompt:

      You will get back a few addresses separated by spaces. You can try each in your web browser to see if they work.

      An alternative is using the curl tool, which should give you your public IP address as seen from another location on the internet.

      First, install curl using apt:

      Then, use curl to retrieve icanhazip.com using IPv4:

      When you have your server's IP address, enter it into your browser's address bar:

      http://your_server_ip
      

      You should see the default Debian 10 Apache web page:

      Apache default page

      This page indicates that Apache is working correctly. It also includes some basic information about important Apache files and directory locations.

      Step 4 — Managing the Apache Process

      Now that you have your web server up and running, let's go over some basic management commands.

      To stop your web server, type:

      • sudo systemctl stop apache2

      To start the web server when it is stopped, type:

      • sudo systemctl start apache2

      To stop and then start the service again, type:

      • sudo systemctl restart apache2

      If you are simply making configuration changes, Apache can often reload without dropping connections. To do this, use this command:

      • sudo systemctl reload apache2

      By default, Apache is configured to start automatically when the server boots. If this is not what you want, disable this behavior by typing:

      • sudo systemctl disable apache2

      To re-enable the service to start up at boot, type:

      • sudo systemctl enable apache2

      Apache should now start automatically when the server boots again.

      When using the Apache web server, you can use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server. In the following commands, replace your_domain with your own domain name. To learn more about setting up a domain name with DigitalOcean, see our Introduction to DigitalOcean DNS.

      Apache on Debian 10 has one server block enabled by default that is configured to serve documents from the /var/www/html directory. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html, let's create a directory structure within /var/www for our your_domain site, leaving /var/www/html in place as the default directory to be served if a client request doesn't match any other sites.

      Create the directory for your_domain as follows, using the -p flag to create any necessary parent directories:

      sudo mkdir -p /var/www/your_domain/html
      

      Next, assign ownership of the directory with the $USER environmental variable:

      • sudo chown -R $USER:$USER /var/www/your_domain/html

      The permissions of your web roots should be correct if you haven't modified your unmask value, but you can make sure by typing:

      • sudo chmod -R 755 /var/www/your_domain

      Next, create a sample index.html page using nano or your favorite editor:

      • nano /var/www/your_domain/html/index.html

      Inside, add the following sample HTML:

      /var/www/your_domain/html/index.html

      <html>
          <head>
              <title>Welcome to your_domain!</title>
          </head>
          <body>
              <h1>Success!  The your_domain virtual host is working!</h1>
          </body>
      </html>
      

      Save and close the file when you are finished.

      In order for Apache to serve this content, it's necessary to create a virtual host file with the correct directives. Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf directly, let's make a new one at /etc/apache2/sites-available/your_domain.conf:

      • sudo nano /etc/apache2/sites-available/your_domain.conf

      Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name:

      /etc/apache2/sites-available/your_domain.conf

      <VirtualHost *:80>
          ServerAdmin admin@your_email_domain
          ServerName your_domain
          ServerAlias www.your_domain
          DocumentRoot /var/www/your_domain/html
          ErrorLog ${APACHE_LOG_DIR}/error.log
          CustomLog ${APACHE_LOG_DIR}/access.log combined
      </VirtualHost>
      

      Notice that we've updated the DocumentRoot to our new directory and ServerAdmin to an email that the your_domain site administrator can access. We've also added two directives: ServerName, which establishes the base domain that should match for this virtual host definition, and ServerAlias, which defines further names that should match as if they were the base name.

      Save and close the file when you are finished.

      Let's enable the file with the a2ensite tool:

      • sudo a2ensite your_domain.conf

      Disable the default site defined in 000-default.conf:

      • sudo a2dissite 000-default.conf

      Next, let's test for configuration errors:

      • sudo apache2ctl configtest

      You should see the following output:

      Output

      AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message Syntax OK

      Restart Apache to implement your changes:

      • sudo systemctl restart apache2

      Apache should now be serving your domain name. You can test this by navigating to http://your_domain, where you should see something like this:

      Apache virtual host example

      Step 6 – Getting Familiar with Important Apache Files and Directories

      Now that you know how to manage the Apache service itself, you should take a few minutes to familiarize yourself with a few important directories and files.

      Content

      • /var/www/html: The actual web content, which by default only consists of the default Apache page you saw earlier, is served out of the /var/www/html directory. This can be changed by altering Apache configuration files.

      Server Configuration

      • /etc/apache2: The Apache configuration directory. All of the Apache configuration files reside here.
      • /etc/apache2/apache2.conf: The main Apache configuration file. This can be modified to make changes to the Apache global configuration. This file is responsible for loading many of the other files in the configuration directory.
      • /etc/apache2/ports.conf: This file specifies the ports that Apache will listen on. By default, Apache listens on port 80 and additionally listens on port 443 when a module providing SSL capabilities is enabled.
      • /etc/apache2/sites-available/: The directory where per-site virtual hosts can be stored. Apache will not use the configuration files found in this directory unless they are linked to the sites-enabled directory. Typically, all server block configuration is done in this directory, and then enabled by linking to the other directory with the a2ensite command.
      • /etc/apache2/sites-enabled/: The directory where enabled per-site virtual hosts are stored. Typically, these are created by linking to configuration files found in the sites-available directory with the a2ensite. Apache reads the configuration files and links found in this directory when it starts or reloads to compile a complete configuration.
      • /etc/apache2/conf-available/, /etc/apache2/conf-enabled/: These directories have the same relationship as the sites-available and sites-enabled directories, but are used to store configuration fragments that do not belong in a virtual host. Files in the conf-available directory can be enabled with the a2enconf command and disabled with the a2disconf command.
      • /etc/apache2/mods-available/, /etc/apache2/mods-enabled/: These directories contain the available and enabled modules, respectively. Files in ending in .load contain fragments to load specific modules, while files ending in .conf contain the configuration for those modules. Modules can be enabled and disabled using the a2enmod and a2dismod command.

      Server Logs

      • /var/log/apache2/access.log: By default, every request to your web server is recorded in this log file unless Apache is configured to do otherwise.
      • /var/log/apache2/error.log: By default, all errors are recorded in this file. The LogLevel directive in the Apache configuration specifies how much detail the error logs will contain.

      Conclusion

      Now that you have your web server installed, you have many options for the type of content you can serve and the technologies you can use to create a richer experience.

      If you'd like to build out a more complete application stack, you can look at this article on how to configure a LAMP stack on Debian 10.



      Source link

      Create a TCP and UDP Client and Server using Go


      Updated by Linode Contributed by Mihalis Tsoukalos

      Go is a compiled, statically typed programming language developed by Google. Many modern applications, including Docker, Kubernetes, and Terraform, are written in Go. Go packages allow developers to organize and reuse Go code in a simple and maintainable manner.

      In this guide, you will use the net package, which is a part of Go’s standard library, to create TCP and UDP servers and clients. This guide is meant to provide instructional examples to help you become more familiar with the Go programming language.

      Scope of this Guide

      Throughout this guide you will create the following:

      • A TCP server and client. The TCP server accepts incoming messages from a TCP client and responds with the current date and time.
      • A UDP server and client. The UDP server accepts incoming messages from a UDP client and responds with a random number.
      • A concurrent TCP server that accepts incoming messages from several TCP clients and responds with the number of clients currently connected to it.

      Before You Begin

      1. If you are not familiar with using Go packages, review the Getting Started with Go Packages guide.

      2. Install Go on your computer if it is not already installed. You can follow our guide How to Install Go on Ubuntu for installation steps.

        This guide requires Go version 1.8 or higher. It is considered good practice to have the latest version of Go installed. You can check your Go version by executing the following command:

        go version
        

      Note

      This guide is written for a non-root user. Depending on the TCP/IP port number that you use when running the TCP and UDP servers, you may need to prefix commands with sudo. If you are not familiar with the sudo command, see the Users and Groups guide.

      Protocol Definitions

      ProtocolDefinition
      TCP (Transmission Control Protocol)TCP’s principal characteristic is that it is a reliable protocol by design. If there is no proof of a packet’s delivery, TCP will resend the packet. Some of the tasks TCP packets can be used for are establishing connections, transferring data, sending acknowledgements, and closing connections.
      IP (Internet Protocol)The IP protocol adheres to the end-to-end principle, which places all network intelligence in the end nodes and not in the intermediary nodes. This design favors a reduction in network complexity over reliability. For this reason, the Internet Protocol does not guarantee a reliable delivery of packets over a network. Instead, IP works together with TCP to reliably deliver packets over a network.
      UDP (User Datagram Protocol):UDP provides a simpler implementation of the transport layer protocol that, while less reliable than TCP, is much faster. UDP does not provide error checking, correction or packet retransmission, which makes it very fast. When speed is more important than reliability, UDP is generally chosen over TCP. UDP is commonly used for online gaming, video chatting, and other real-time applications.

      The net Package

      Go’s net package provides a portable interface for network I/O, including TCP/IP, UDP, domain name resolution, and Unix domain sockets. You will use this package to create TCP and UDP servers and clients in this guide.

      net Package Functions

      Use the table below as a quick reference for some of the net package functions used throughout this guide. To view all types and functions included in the net package, see Golang’s official documentation.

      Note

      All versions of net.Dial() and net.Listen() return data types that implement the io.Reader and io.Writer interfaces. This means that you can use regular File I/O functions to send and receive data from a TCP/IP connections.
      TypeFunction
      type Listenerfunc Listen(network, address string) (Listener, error)

         • The network parameter defines the type of network to use and accepts values tcp, tcp4 (IPv4-only), tcp6 (IPv6-only), unix (Unix sockets), or unixpacket.

         • The address parameter defines the server address and port number that the server will listen on.

      type UDPConnfunc ListenUDP(network string, laddr *UDPAddr) (*UDPConn, error)

         • Used to create UDP servers.

         • The network parameter must be a UDP network name.

         • The laddr parameter defines the server address and port number that the server will listen on.

      func DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error)

         • Used to specify the kind of client you will create.

         • The network parameter must be a UDP network name.

         • The laddr is the listening address (server). If laddr is nil, a local address is automatically chosen.

         • raddr is the response address (client). If the IP field of raddr is nil or an unspecified IP address, the local system is assumed.

      type UDPAddrfunc ResolveUDPAddr(network, address string) (*UDPAddr, error)

         • This function returns the address of a UDP end point.

         • The network parameter must be a UDP network name.

         • The address parameter has the form host:port. The host must be a an IP address, or a host name that can be resolved to IP addresses.

      type TCPAddrfunc ResolveTCPAddr(network, address string) (*TCPAddr, error)

         • This function returns the address of a TCP end point.

         • The network parameter must be a TCP network name.

         • The address parameter has the form host:port. The host must be a an IP address, or a host name that can be resolved to IP addresses.

      type Connfunc Dial(network, address string) (Conn, error)

         • This function connects to the address on the named network.

         • The network parameter can be tcp, tcp4 (IPv4-only), tcp6 (IPv6-only), udp, udp4 (IPv4-only), udp6 (IPv6-only), ip, ip4 (IPv4-only), ip6 (IPv6-only), unix, unixgram and unixpacket.

         • When using TCP or UDP networks, the address parameter has the form host:port. The host must be a an IP address, or a host name that can be resolved to IP addresses.

      type TCPConnfunc DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error)

         • This function connects to the address on the TCP networks.

         • The network parameter must be a TCP network name.

         • The laddr is the listening address (server). If laddr is nil, a local address is automatically chosen.

         • raddr is the response address (client). If the IP field of raddr is nil or an unspecified IP address, the local system is assumed.

      Create a TCP Client and Server

      In this section, you will create a generic TCP client and server using Go. After creating the client and server, you will run them to test their connection with each other.

      Note

      Create the TCP Client

      The TCP client that you will create in this section will allow you to interact with any TCP server.

      1. In your current working directory, create a file named tcpC.go with the following content:

        ./tcpC.go
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        
        package main
        
        import (
                "bufio"
                "fmt"
                "net"
                "os"
                "strings"
        )
        
        func main() {
                arguments := os.Args
                if len(arguments) == 1 {
                        fmt.Println("Please provide host:port.")
                        return
                }
        
                CONNECT := arguments[1]
                c, err := net.Dial("tcp", CONNECT)
                if err != nil {
                        fmt.Println(err)
                        return
                }
        
                for {
                        reader := bufio.NewReader(os.Stdin)
                        fmt.Print(">> ")
                        text, _ := reader.ReadString('n')
                        fmt.Fprintf(c, text+"n")
        
                        message, _ := bufio.NewReader(c).ReadString('n')
                        fmt.Print("->: " + message)
                        if strings.TrimSpace(string(text)) == "STOP" {
                                fmt.Println("TCP client exiting...")
                                return
                        }
                }
        }
            
        • This file creates the main package, which declares the main() function. The function will use the imported packages to create a TCP client.
        • The main() function gathers command line arguments in the arguments variable and makes sure that a value for host:port was sent.
        • The CONNECT variable stores the value of arguments[1]to be used in the net.Dial() call.
        • A call to net.Dial() begins the implementation of the TCP client and will connect you to the desired TCP server. The second parameter of net.Dial() has two parts; the first is the hostname or the IP address of the TCP server and the second is the port number the TCP server listens on.
        • bufio.NewReader(os.Stdin) and ReadString() is used to read user input. Any user input is sent to the TCP server over the network using Fprintf().
        • bufio reader and the bufio.NewReader(c).ReadString('n') statement read the TCP server’s response. The error variable is ignored here for simplicity.
        • The entire for loop that is used to read user input will only terminate when you send the STOP command to the TCP server.

      Create the TCP Server

      You are now ready to create the TCP server. The TCP server will return the current date and time to the TCP client using a single network packet.

      1. In your current working directory, create a file named tcpS.go with the following content:

        ./tcpS.go
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        44
        45
        46
        47
        48
        49
        50
        
        package main
        
        import (
                "bufio"
                "fmt"
                "net"
                "os"
                "strings"
                "time"
        )
        
        func main() {
                arguments := os.Args
                if len(arguments) == 1 {
                        fmt.Println("Please provide port number")
                        return
                }
        
                PORT := ":" + arguments[1]
                l, err := net.Listen("tcp", PORT)
                if err != nil {
                        fmt.Println(err)
                        return
                }
                defer l.Close()
        
                c, err := l.Accept()
                if err != nil {
                        fmt.Println(err)
                        return
                }
        
                for {
                        netData, err := bufio.NewReader(c).ReadString('n')
                        if err != nil {
                                fmt.Println(err)
                                return
                        }
                        if strings.TrimSpace(string(netData)) == "STOP" {
                                fmt.Println("Exiting TCP server!")
                                return
                        }
        
                        fmt.Print("-> ", string(netData))
                        t := time.Now()
                        myTime := t.Format(time.RFC3339) + "n"
                        c.Write([]byte(myTime))
                }
        }
            
        • This file creates the main package, which declares the main() function. The function will use the imported packages to create a TCP server.
        • The main() function gathers command line arguments in the arguments variable and includes error handling.
        • The net.Listen() function makes the program a TCP server. This functions returns a Listener variable, which is a generic network listener for stream-oriented protocols.
        • It is only after a successful call to Accept() that the TCP server can begin to interact with TCP clients.
        • The current implementation of the TCP server can only serve the first TCP client that connects to it, because the Accept() call is outside of the for loop. In the Create a Concurrent TCP Server section of this guide, you will see a TCP server implementation that can serve multiple TCP clients using Goroutines.
        • The TCP server uses regular File I/O functions to interact with TCP clients. This interaction takes place inside the for loop. Similarly to the TCP client, when the TCP server receives the STOP command from the TCP client, it will terminate.

      Test the TCP Client and Server

      You can now test your TCP client and server. You will need to execute the TCP server first so that the TCP client has somewhere it can connect to.

      1. Run your TCP server. From the directory containing the tcpS.go file, run the following command:

        go run tcpS.go 1234
        

        The server will listen on port number 1234. You will not see any output as a result of this command.

      2. Open a second shell session to execute the TCP client and to interact with the TCP server. Run the following command:

        go run tcpC.go 127.0.0.1:1234
        

        Note

        If the TCP server is not running on the expected TCP port, you will get the following error message from tcpC.go:

        dial tcp [::1]:1234: connect: connection refused
        
      3. You will see a >> prompt waiting for you to enter some text. Type in Hello! to receive a response from the TCP server:

        Hello!
        

        You should see a similar output:

          
        >> Hello!
        ->: 2019-05-23T19:43:21+03:00
            
        
      4. Send the STOP command to exit the TCP client and server:

        STOP
        

        You should see a similar output in the client:

          
        >> STOP
        ->: TCP client exiting...
            
        

        The output on the TCP server side will resemble the following:

          
        -> Hello!
        Exiting TCP server!
            
        

      Note

      The TCP server waits before writing back to the TCP client, whereas the client writes to the TCP server first and then waits to receive an answer. This behavior is part of the protocol definition that governs a TCP or a UDP connection. In this example, you have implemented an unofficial protocol that is based on TCP.

      Create a UDP Client and Server

      In this section, you will create a UDP client and server. After creating the client and server, you will run them both to test their connection with each other. A UDP client can be generic and can communicate with multiple UDP servers. On the other hand, a UDP server cannot be completely generic, because it typically implements a specific functionality. In the case of our UDP server example, it will return random numbers to UDP clients that connect to it.

      Create the UDP Client

      The UDP client that you will create in this section will allow you to interact with any UDP server.

      1. In your current working directory, create a file named udpC.go with the following content:

        ./udpC.go
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        44
        45
        46
        47
        48
        49
        50
        51
        52
        53
        54
        
        package main
        
        import (
                "bufio"
                "fmt"
                "net"
                "os"
                "strings"
        )
        
        func main() {
                arguments := os.Args
                if len(arguments) == 1 {
                        fmt.Println("Please provide a host:port string")
                        return
                }
                CONNECT := arguments[1]
        
                s, err := net.ResolveUDPAddr("udp4", CONNECT)
                c, err := net.DialUDP("udp4", nil, s)
                if err != nil {
                        fmt.Println(err)
                        return
                }
        
                fmt.Printf("The UDP server is %sn", c.RemoteAddr().String())
                defer c.Close()
        
                for {
                        reader := bufio.NewReader(os.Stdin)
                        fmt.Print(">> ")
                        text, _ := reader.ReadString('n')
                        data := []byte(text + "n")
                        _, err = c.Write(data)
                        if strings.TrimSpace(string(data)) == "STOP" {
                                fmt.Println("Exiting UDP client!")
                                return
                        }
        
                        if err != nil {
                                fmt.Println(err)
                                return
                        }
        
                        buffer := make([]byte, 1024)
                        n, _, err := c.ReadFromUDP(buffer)
                        if err != nil {
                                fmt.Println(err)
                                return
                        }
                        fmt.Printf("Reply: %sn", string(buffer[0:n]))
                }
        }
              
        • This file creates the main package, which declares the main() function. The function will use the imported packages to create a UDP client.
        • The main() function gathers command line arguments in the arguments variable and includes error handling.
        • Regular File I/O functions are used by the UDP client to interact with the UDP server. The client will terminate when you send the STOP command to the UDP server. This is not part of the UDP protocol, but is used in the example to provide the client with a way to exit.
        • A UDP end point address is returned by the net.ResolveUDPAddr() function. The UDP end point is of type UDPAddr and contains IP and port information.
        • The connection to the UDP server is established with the use of the net.DialUDP() function.
        • bufio.NewReader(os.Stdin) and ReadString() is used to read user input.
        • The ReadFromUDP() function reads a packet from the server connection and will return if it encounters an error.

      Create the UDP Server

      You are now ready to create the UDP server. You will write the UDP server code to respond to any connected client with random numbers.

      1. In your current working directory, create a file named udps.go with the following content:

        ./udpS.go
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        44
        45
        46
        47
        48
        49
        50
        51
        52
        53
        54
        55
        56
        57
        58
        59
        
        package main
        
        import (
                "fmt"
                "math/rand"
                "net"
                "os"
                "strconv"
                "strings"
                "time"
        )
        
        func random(min, max int) int {
                return rand.Intn(max-min) + min
        }
        
        func main() {
                arguments := os.Args
                if len(arguments) == 1 {
                        fmt.Println("Please provide a port number!")
                        return
                }
                PORT := ":" + arguments[1]
        
                s, err := net.ResolveUDPAddr("udp4", PORT)
                if err != nil {
                        fmt.Println(err)
                        return
                }
        
                connection, err := net.ListenUDP("udp4", s)
                if err != nil {
                        fmt.Println(err)
                        return
                }
        
                defer connection.Close()
                buffer := make([]byte, 1024)
                rand.Seed(time.Now().Unix())
        
                for {
                        n, addr, err := connection.ReadFromUDP(buffer)
                        fmt.Print("-> ", string(buffer[0:n-1]))
        
                        if strings.TrimSpace(string(buffer[0:n])) == "STOP" {
                                fmt.Println("Exiting UDP server!")
                                return
                        }
        
                        data := []byte(strconv.Itoa(random(1, 1001)))
                        fmt.Printf("data: %sn", string(data))
                        _, err = connection.WriteToUDP(data, addr)
                        if err != nil {
                                fmt.Println(err)
                                return
                        }
                }
        }
            
        • This file creates the main package, which declares the main() function. The function will use the imported packages to create a UDP server.
        • The main() function gathers command line arguments in the arguments variable and includes error handling.
        • The net.ListenUDP() function tells the application to listen for incoming UDP connections, which are served inside the for loop. This is the function call that makes the program a UDP server.
        • The ReadFromUDP() and WriteToUDP() functions are used to read data from a UDP connection and write data to a UDP connection, respectively. A byte slice is stored in the data variable and used to write the desired data. The buffer variable also stores a byte slice and is used to read data.
        • Since UDP is a stateless protocol, each UDP client is served and then the connection closes automatically. The UDP server program will only exit when it receives the STOP keyword from a UDP client. Otherwise, the server program will continue to wait for more UDP connections from other clients.

      Test the UDP Client and Server

      You can now test your UDP client and server. You will need to execute the UDP server first so that the UDP client has somewhere it can connect to.

      1. Run your UDP server. From the directory containing the udpS.go file, run the following command:

        go run udpS.go 1234
        

        The server will listen on port number 1234. You will not see any output as a result of this command.

      2. Open a second shell session to execute the UDP client and to interact with the UDP server. Run the following command:

        go run udpC.go 127.0.0.1:1234
        
      3. You will see a >> prompt waiting for you to enter some text. Type in Hello! to receive a response from the UDP server:

        Hello!
        

        You should see a similar output:

          
        The UDP server is 127.0.0.1:1234
        >> Hello!
        Reply: 82
            
        
      4. Send the STOP command to exit the UDP client and server:

        You should see a similar output on the client side:

          
        >> STOP
        Exiting UDP client!
            
        

        The output on the UDP server side will be as follows:

          
        -> STOP
        Exiting UDP server!
            
        

      Create a Concurrent TCP Server

      This section demonstrates the implementation of a concurrent TCP server. The benefit of a concurrent TCP server is that it can serve multiple clients. In Go, this is accomplished by creating a separate Goroutine to serve each TCP client.

      The example TCP server keeps a running count of the number of TCP clients it has served so far. The counter increases by one each time a new TCP client connects to the TCP server. The current value of that counter is returned to each TCP client.

      1. In your current working directory, create a file named concTCP.go with the following content:

        ./concTCP.go
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        44
        45
        46
        47
        48
        49
        50
        51
        52
        53
        54
        55
        56
        57
        58
        59
        
        package main
        
        import (
                "bufio"
                "fmt"
                "net"
                "os"
                "strconv"
                "strings"
        )
        
        var count = 0
        
        func handleConnection(c net.Conn) {
                fmt.Print(".")
                for {
                        netData, err := bufio.NewReader(c).ReadString('n')
                        if err != nil {
                                fmt.Println(err)
                                return
                        }
        
                        temp := strings.TrimSpace(string(netData))
                        if temp == "STOP" {
                                break
                        }
                        fmt.Println(temp)
                        counter := strconv.Itoa(count) + "n"
                        c.Write([]byte(string(counter)))
                }
                c.Close()
        }
        
        func main() {
                arguments := os.Args
                if len(arguments) == 1 {
                        fmt.Println("Please provide a port number!")
                        return
                }
        
                PORT := ":" + arguments[1]
                l, err := net.Listen("tcp4", PORT)
                if err != nil {
                        fmt.Println(err)
                        return
                }
                defer l.Close()
        
                for {
                        c, err := l.Accept()
                        if err != nil {
                                fmt.Println(err)
                                return
                        }
                        go handleConnection(c)
                        count++
                }
        }
              
        • This file creates the main package, which declares the handleConnection() and main() functions.
        • The main() function will use the imported packages to create a concurrent TCP server. It gathers command line arguments in the arguments variable and includes error handling.
        • Each TCP client is served by a separate Goroutine that executes the handleConnection() function. This means that while a TCP client is served, the TCP server is free to interact with more TCP clients. TCP clients are connected using the Accept() function.
        • Although the Accept() function can be executed multiple times, the net.Listen() function needs to be executed only once. For this reason the net.Listen() function remains outside of the for loop.
        • The for loop in the main() function is endless because TCP/IP servers usually run nonstop. However, if the handleConnection() function receives the STOP message, the Goroutine that runs it will exit and the related TCP connection will close.

      Test the Concurrent TCP Server

      In this section, you will test the concurrent TCP server using the netcat command line utility.

      1. Run your concurrent TCP server. From the directory containing the concTCP.go file, run the following command:

        go run concTCP.go 1234
        

        The command creates a TCP server that listens on port number 1234. You can use any port number, however, ensure it is not already in use and that you have the required privileges. Reference the list of well-known TCP and UDP ports, if needed.

      2. Use netcat to establish a connection with the TCP server. By default, netcat will establish a TCP connection with a remote host on the specified port number.

        nc 127.0.0.1 1234
        
      3. After issuing the previous command, you will not see any change in your output. Type Hello! to send a packet to the TCP server:

        Hello!
        

        The TCP server will return the number of current client connections as its response. Since this is your first connection established with the TCP server, you should expect an output of 1.

          
        Hello!
        1
            
        

        If you’d like, you can open a new shell session and use netcat to establish a second connection with the TCP server by repeating Step 2. When you send the server a second Hello!, you should receive a response of 2 this time.

      4. You can also connect to the TCP server using the TCP client you created in the Create the TCP Client section of the guide. Ensure you are in the directory containing the tcpC.go file and issue the following command:

        go run tcpC.go 127.0.0.1:1234
        
      5. You will see a >> prompt waiting for you to enter some text. Type in Hello! to receive a response from the TCP server:

        Hello!
        

        You should see a similar output indicating 3 client connections:

          
        >> Hello!
        ->: 3
            
        
      6. Send the STOP command to exit the TCP client:

        You should see a similar output on the client:

          
        >> STOP
        ->: TCP client exiting...
              
        

        The output on the TCP server side will be as follows:

          
        .Hello!
        .Hello!
        .Hello!
              
        

        Note

        From the shell session running the TCP server, type CTRL-c to interrupt program execution and then, CTRL-D to close all client connections and to stop the TCP server.

      More Information

      You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

      Find answers, ask questions, and help others.

      This guide is published under a CC BY-ND 4.0 license.



      Source link