Software & AppsOperating SystemLinux

How To Set a Proxy for Terminal in Linux

Ubuntu 2

In this article, we will explore how to set a proxy for the terminal in Linux. This can be especially useful if you are working in an environment that requires a proxy server for internet access. We’ll cover several methods, including setting environment variables, editing system configuration files, using proxychains, and using Tor as a proxy.

Setting Environment Variables

One of the simplest ways to set a proxy for the terminal in Linux is by setting environment variables. Here’s how to do it:

  1. Open your terminal.
  2. Use the export command to set the http_proxy and https_proxy variables. Replace proxyServerAddress and proxyPort with your proxy server’s address and port number, respectively:
    export http_proxy='http://proxyServerAddress:proxyPort'
    export https_proxy='https://proxyServerAddress:proxyPort'

The http_proxy and https_proxy environment variables are used by many command-line programs such as wget, curl and others to determine the proxy settings.

To disable the proxy, you can unset these variables:

unset http_proxy
unset https_proxy

Editing System Configuration Files

Another method to set a proxy for the terminal is by editing system configuration files. This method is more permanent, as it sets the proxy for commands like wget, ftp, lftp, and telnet in the terminal.

  1. Open your terminal and run the following command to edit the proxy.sh file:
    sudo -H gedit /etc/profile.d/proxy.sh
  2. Add the following lines to the file, replacing the placeholders with your proxy details:
    export http_proxy=http://username:password@proxyhost:port/
    export https_proxy=https://username:password@proxyhost:port/
    export ftp_proxy=http://username:password@proxyhost:port/
  3. Save the file and exit.

Using Proxychains

Proxychains is a tool that forces any TCP connection made by any given application to follow through proxy like TOR or any other SOCKS4, SOCKS5 or HTTP(S) proxy. It is a useful tool for those who want to anonymize their browsing and is also helpful for pentesters and programmers.

  1. Install proxychains by running the following command:
    sudo apt-get install proxychains
  2. Edit the configuration file using the following command:
    sudo vi /etc/proxychains.conf
  3. Modify the last line of the file to specify your proxy details.
  4. To use the proxy, prefix your commands with proxychains, e.g., proxychains ssh user@host.

Using Tor as a Proxy

Tor is free software and an open network that helps you defend against traffic analysis, a form of network surveillance that threatens personal freedom and privacy.

  1. Install Tor by running the following command:
    sudo apt-get install tor
  2. Start Tor by running the following command:
    tor
  3. To send terminal communications through Tor, you can use the torsocks command. For example:
    torsocks curl icanhazip.com

This will make the command use the Tor network for communication.

Conclusion

Setting a proxy for terminal in Linux can be achieved in several ways, depending on your specific needs and the tools you have at your disposal. Whether you need to set a proxy for a single session, make a permanent configuration change, or route your traffic through an anonymizing network, Linux provides the flexibility to accommodate your requirements.

How do I check if a proxy is already set in my terminal?

To check if a proxy is already set in your terminal, you can use the echo command to display the values of the http_proxy and https_proxy environment variables. Open your terminal and run the following commands:

echo $http_proxy
echo $https_proxy

If a proxy is set, the respective variable will display the proxy server address and port number. If nothing is displayed, it means no proxy is currently set.

How can I set a proxy for a specific command without permanently changing the system configuration?

You can set a proxy for a specific command without permanently changing the system configuration by prefixing the command with the environment variable assignments. For example, to use a proxy for a single curl command, you can run:

http_proxy='http://proxyServerAddress:proxyPort' https_proxy='https://proxyServerAddress:proxyPort' curl example.com

This will set the proxy only for the curl command and not affect other commands or the system configuration.

How can I configure a proxy that requires authentication?

To configure a proxy that requires authentication, you need to include your username and password in the proxy URL. For example:

export http_proxy='http://username:password@proxyServerAddress:proxyPort'
export https_proxy='https://username:password@proxyServerAddress:proxyPort'

Replace username and password with your actual credentials, and proxyServerAddress and proxyPort with the appropriate values.

How can I disable the proxy temporarily?

To disable the proxy temporarily, you can unset the http_proxy and https_proxy environment variables. Open your terminal and run the following commands:

unset http_proxy
unset https_proxy

This will remove the proxy settings for the current session.

Can I use a SOCKS proxy instead of an HTTP/HTTPS proxy?

Yes, you can use a SOCKS proxy instead of an HTTP/HTTPS proxy. However, not all command-line programs support SOCKS proxies natively. In such cases, you can use a tool like proxychains to force the application to use the SOCKS proxy. Refer to the "Using Proxychains" section in the article for more information.

Leave a Comment

Your email address will not be published. Required fields are marked *