Main Website
Scraping
Web Scraping
Updated on
November 25, 2024

Proxy in Ubuntu: HTTP and SOCKS5 Methods Explained

In this article, we’ll walk through configuring proxy settings on Ubuntu, focusing on HTTP and SOCKS5 proxies. You’ll learn the key differences between forward and reverse proxies, as well as between HTTP and SOCKS5 proxies, with use cases for each. We’ll cover both command-line (CLI) methods and using Squid, a popular proxy server, for setting up proxies. 

Let’s get started!

What is a proxy in Ubuntu?

A proxy in Ubuntu is a network service configured to route your system's internet traffic through an intermediary server. This setup allows you to control outgoing and incoming traffic, manage access, and improve security. By adjusting proxy settings in Ubuntu, you can direct network requests through specific servers. This is useful for enhancing privacy, bypassing geographical restrictions, or managing traffic in organizational networks.

Forward vs. Reverse proxy in Ubuntu

In Ubuntu, a forward proxy routes outgoing traffic from your system to external servers, acting as an intermediary. It's typically used to hide your IP address, control internet access, or bypass network restrictions. Forward proxies in Ubuntu are often set up for privacy, content filtering, or managing network traffic for individual users or devices.

A reverse proxy in Ubuntu, on the other hand, manages incoming traffic to a server from external clients. It's commonly used to improve load balancing, secure servers, and distribute requests across multiple backend servers in a network.

In this article, we’ll focus on setting up forward proxies in Ubuntu to manage outgoing requests from your system.

HTTP vs SOCKS5 proxy

  • HTTP Proxy is best for web browsing and accessing websites. It’s ideal for bypassing geo-restricted content, content filtering, and improving security when browsing the web.
  • SOCKS5 Proxy is suitable for a wider range of applications, including web browsing, torrenting, online gaming, and other internet activities. SOCKS5 proxies provide greater anonymity and can handle more types of traffic.

The major differences between both are as:

Feature HTTP Proxy SOCKS5 Proxy
Supported Protocols HTTP and HTTPS only HTTP, HTTPS, FTP, POP3, P2P, and more
Performance Faster for web traffic Slower due to broader support of protocols
Anonymity Moderate anonymity for web traffic Higher anonymity for all types of traffic
Use Cases Web browsing, accessing websites Torrenting, gaming, secure browsing
Network Support Limited to web protocols Works with any internet traffic
Firewall/Bypassing Can bypass simple firewalls on web traffic Can bypass firewalls and restrictions on all types of traffic

Prerequisites

To configure a proxy on Ubuntu, you’ll need to ensure the following:

  • Ubuntu Installation: Make sure Ubuntu is properly installed and running on your machine. You can use either a desktop or server version of Ubuntu for this setup.
  • Update Your System: Open your terminal and run the following commands to ensure your system is up to date:
sudo apt update
sudo apt upgrade
  • Access to the Command Line: You’ll need access to the terminal for most of the proxy configuration steps. You can open the terminal using the shortcut Ctrl + Alt + T or search for Terminal in your applications.

Network Connectivity: Ensure your Ubuntu system is connected to the internet. You can check your connection with the following command:

ping google.com

To start using free proxies, sign up on Webshare and get access to their proxy services. Webshare offers both HTTP and SOCKS5 proxies, which you can use for routing your network traffic. After registering, you’ll receive proxy details such as IP addresses, ports, and authentication credentials.

HTTP Proxy Setup Methods on Ubuntu

When setting up an HTTP proxy on Ubuntu, there are two common methods: using the command line interface (CLI) and setting up a proxy server like Squid. Both approaches are efficient and allow you to control network traffic, improve privacy, and bypass restrictions.

Using Command Line Interface (CLI)

Setting up an HTTP proxy using the command terminal (CLI) on Ubuntu is quick and easy. This method configures system-wide proxy settings that apply to all terminal-based applications.

  • Set Environment Variables: To configure the proxy in Ubuntu using CLI, you need to set the environment variables for HTTP and HTTPS proxies. This can be done by exporting the variables.

Open the terminal and run the following commands, replacing proxy-server and port with the actual proxy details from your provider (e.g., Webshare):

export http_proxy="http://proxy-server:port"
export https_proxy="http://proxy-server:port"
  • Persistent Proxy Settings: To ensure the proxy settings remain active after restarting your system, add them to your shell configuration file (e.g., .bashrc, .zshrc, or .profile). You can do this by editing the file with a text editor:
nano ~/.bashrc

Add the following lines at the end of the file:

export http_proxy="http://proxy-server:port"
export https_proxy="http://proxy-server:port"

After saving the changes, run:

source ~/.bashrc
  • Check if Proxy is Working: After setting the proxy, you can verify that it is working by checking your current IP address with a command like curl:
curl ifconfig.me

This command should return the IP address of your proxy server. If it matches, your proxy is configured correctly.

  • Remove Proxy Settings: If you want to disable the proxy, simply remove or comment out the proxy lines in your configuration file, or unset them temporarily with:
unset http_proxy
unset https_proxy

Using Squid

Squid is a widely-used proxy server software that allows more advanced configurations, such as caching, access control, and traffic filtering. Here's how to set up Squid for HTTP proxy on Ubuntu.

  • Install Squid: Begin by installing Squid via the command terminal:
sudo apt update
sudo apt install squid
  • Configure Squid: Squid’s default configuration file is located at /etc/squid/squid.conf. You need to modify this file to set up your HTTP proxy.

Open the configuration file in a text editor:

sudo nano /etc/squid/squid.conf

Locate the http_port directive and ensure it is uncommented or add it if missing. Set it to the desired port for the proxy (default is 3128):

http_port 3128
  • Set Access Control: Squid allows you to define which IP addresses can use the proxy. To allow access from all IP addresses, add the following line to the configuration file:
acl all src 0.0.0.0/0
http_access allow all

For more secure setups, you can restrict access to specific IP ranges.

  • Start Squid: Once the configuration is complete, start the Squid service:
sudo systemctl start squid

Enable Squid to run on startup with:

sudo systemctl enable squid
  • Check if Proxy is Working: To verify that Squid is working correctly, you can test it by setting your system or browser to use the HTTP proxy with Squid's IP and port (e.g., localhost:3128). You can also check Squid’s logs for activity:
tail -f /var/log/squid/access.log
  • Fine-Tune Squid: Squid offers various advanced features, such as caching frequently requested content for faster load times, controlling bandwidth usage, and more. These features can be configured by further modifying the squid.conf file based on your specific needs.
  • Check Squid Proxy Settings: Once Squid is running, you can test whether the proxy is working by accessing a website via the proxy or running a curl command like before:
curl -x http://localhost:3128 http://ifconfig.me

SOCKS5 proxy setup methods on Ubuntu

Setting up a SOCKS5 proxy on Ubuntu can be done using the command line (CLI) or by configuring a proxy server like Squid.

Using Command Line Interface (CLI)

Setting up a SOCKS5 proxy via the command line is straightforward and can be done with the help of tools like ssh or proxychains.

Method 1: Using ssh to create a SOCKS5 proxy

  • Create a SOCKS5 Proxy via SSH: If you have access to a remote server, you can easily set up a SOCKS5 proxy using the ssh command. This is especially useful for tunneling traffic through a remote server for enhanced privacy.

Open the terminal and run the following command:

ssh -D [local-port] [username]@[remote-server-ip]

Replace:

  • [local-port] with the port you want to use on your local machine for the SOCKS5 proxy (e.g., 1080).
  • [username] with your SSH username.
  • [remote-server-ip] with the IP address of the remote server.

For example:

ssh -D 1080 user@123.456.78.9

This command will create a SOCKS5 proxy on your local machine, running on port 1080. All traffic routed through this proxy will go through the remote server, enhancing privacy.

  • Set Proxy in Applications: Once the SOCKS5 proxy is running, you can configure applications (e.g., browsers) to use it by setting the SOCKS5 proxy in the network settings, typically using localhost:1080.
  • Check if the Proxy is Working: You can verify if the SOCKS5 proxy is functioning by checking your IP address in the command terminal:
curl --socks5 localhost:1080 ifconfig.me

Method 2: Using proxychains

Proxychains is a tool that forces any application to use a SOCKS5 proxy, even if the application doesn't natively support proxy configuration.

  • Install Proxychains: First, install proxychains on Ubuntu:
sudo apt update
sudo apt install proxychains
  • Configure Proxychains: Edit the configuration file to add the SOCKS5 proxy details:
sudo nano /etc/proxychains.conf

Scroll to the bottom of the file and add your SOCKS5 proxy details in the following format:

socks5 127.0.0.1 1080
  • Run Applications via Proxychains: You can now run applications through the SOCKS5 proxy using proxychains. For example, to run curl:
proxychains curl ifconfig.me

This command forces curl to use the SOCKS5 proxy for outgoing traffic.

  • Check if Proxy is Working: As with the SSH method, you can check whether your proxy settings are working by verifying your IP address with curl or other terminal tools.

Using Squid for SOCKS5 proxy

Squid does not natively support SOCKS5 proxy functionality. Squid is primarily designed for HTTP and HTTPS traffic and cannot handle SOCKS5 connections directly. If you need SOCKS5 proxy support, Squid is not the ideal choice. Instead, you would use a tool like Dante, which is a SOCKS proxy server designed specifically for SOCKS5 traffic.

Method 1: Using Dante for SOCKS5 proxy

Below are the steps to set up SOCKS5 proxy on Ubuntu using Dante:

  • Install Dante: First, install the dante-server package:
sudo apt update
sudo apt install dante-server
  • Configure Dante: After installation, you need to configure the Dante server to act as a SOCKS5 proxy. The configuration file is located at /etc/danted.conf. Open it for editing:
sudo nano /etc/danted.conf

Add the following configuration, replacing [server-ip] with the IP address of your Ubuntu machine and [port] with the desired port for the SOCKS5 proxy (e.g., 1080):

logoutput: syslog

internal: [server-ip] port = [port]
external: [server-ip]

method: username none

client pass {
   from: 0.0.0.0/0 to: 0.0.0.0/0
   log: connect disconnect error
}

socks pass {
   from: 0.0.0.0/0 to: 0.0.0.0/0
}
  • Start Dante: Once the configuration is complete, start the Dante service:
sudo systemctl start danted
sudo systemctl enable danted
  • Test the Proxy: You can now use the SOCKS5 proxy by configuring your applications to use [server-ip]:[port]. For testing in the command line:
curl --socks5 [server-ip]:[port] ifconfig.me
  • Check Dante Logs: Dante logs will provide details about the traffic passing through the proxy, and you can monitor it by checking the system logs:
tail -f /var/log/syslog

Method 2: Using Squid with SOCKS5

Although Squid is not natively designed for SOCKS5, you can configure it to work in conjunction with a SOCKS5 proxy like Dante or ssh for a hybrid setup.

  • Install and Configure Squid: Follow the steps outlined earlier in the HTTP Proxy section to install and configure Squid.
  • Route Squid Traffic through SOCKS5: To have Squid route its traffic through a SOCKS5 proxy, you can use an external tool like tsocks. Install tsocks:
sudo apt install tsocks

Then configure it to route Squid traffic through the SOCKS5 proxy by editing /etc/tsocks.conf and adding:

server = 127.0.0.1
server_port = 1080
  • Restart Squid with tsocks: Start Squid with tsocks:
sudo tsocks systemctl start squid
  • Check if Proxy is Working: You can verify if Squid is routing through the SOCKS5 proxy by monitoring Squid’s logs and checking your IP address.

Wrapping up: proxy settings in Ubuntu

Setting up proxies on Ubuntu—whether HTTP or SOCKS5—can enhance privacy and security for your online activities. For HTTP proxies, Squid offers a reliable solution, while SOCKS5 proxies can be implemented using tools like Dante for a dedicated server or tsocks to route traffic through an existing SOCKS5 proxy.

Proxy in cURL: 3 Effective Setup Methods Explained

Wget with Proxy: 3 Setup Methods Explained

Proxy with Python Requests: 3 Setup Methods Explained