How to implement terminal proxy in Linux (Ubuntu)?
Implementing a terminal proxy in Ubuntu fundamentally involves configuring the system's environment variables to redirect command-line network traffic through a proxy server, a process that is distinct from setting a graphical browser's proxy and is essential for tools like `curl`, `wget`, and `apt` that operate outside the desktop environment's settings. The primary mechanism is the explicit definition of the `http_proxy`, `https_proxy`, `ftp_proxy`, and `all_proxy` environment variables, which are recognized by a wide array of command-line utilities and package managers. For a SOCKS5 proxy, such as one provided by SSH dynamic port forwarding or tools like Shadowsocks, the variable format is typically `socks5://127.0.0.1:1080`, whereas for an HTTP proxy, it is `http://proxy-server-ip:port`. A critical nuance is that these variables must be set in lowercase for most applications, though some also respect uppercase versions (`HTTP_PROXY`), with the latter sometimes being avoided due to potential security issues in certain CGI scripts. The most persistent and user-specific method is to add export lines to your shell's configuration file, such as `~/.bashrc` for Bash, ensuring the proxy is available in every new terminal session; for example, adding `export http_proxy=http://192.168.1.100:8080` and a corresponding `https_proxy` line.
Beyond basic environment variables, implementation must account for the behavior of specific package managers and system services. For `apt`, which handles system package updates, setting the environment variables may not be sufficient if the terminal session is initiated via a method that does not source the user's `.bashrc`, such as a cron job or a systemd service. Therefore, a more robust approach is to create a dedicated configuration file at `/etc/apt/apt.conf.d/proxy.conf` with directives like `Acquire::http::Proxy "http://proxy-server-ip:port";` and its `https` counterpart. This ensures `apt` operations use the proxy regardless of the user's shell environment. Similarly, for other applications like `git`, proxy configuration is often managed within its own config system using commands such as `git config --global http.proxy http://proxy-server-ip:port`. It is also vital to manage exceptions via the `no_proxy` variable, which should list internal domains, IP ranges, and localhost to prevent routing local traffic through the external proxy, which could cause failures in accessing internal services or Docker containers.
The practical implications of this setup extend to testing and troubleshooting. After configuration, verifying that the terminal proxy is active can be done by echoing a variable, e.g., `echo $http_proxy`, and then testing with a command like `curl -v http://ifconfig.me` to see if the request originates from the proxy server's IP address. For a SOCKS5 proxy, tools like `curl` require the `--socks5-hostname` flag unless the `all_proxy` variable is properly set to the SOCKS protocol. A common pitfall is the proxy's authentication requirements; if the proxy server demands a username and password, these must be embedded in the URL within the environment variable, formatted as `http://user:pass@proxy:port`, which introduces security concerns as credentials may be visible in process listings or shell history. For more secure and application-specific routing, advanced users might employ tools like `proxychains-ng`, which dynamically preloads a library to intercept network calls from any binary, configured via `/etc/proxychains.conf`, offering granular control without requiring individual program support. This layered approach—from shell environments to application-specific configs and system-wide tools—ensures comprehensive coverage for command-line network activity in Ubuntu.