A Brief But Accurate Documentation, BSD Linux And Programming

 Docker

Sunday, March 5, 2023

Set Proxy for all Traffics System wide Ubuntu

First install Redsocks package to re-route all the traffics

sudo apt-get install redsocks


in the file /etc/redsocks.conf

edit the ip and port to exact under redsock menu


redsocks {

    local_ip = 127.0.0.1;

    local_port = 12345;


# have to change the ip and port to suit your needs 

    ip = example.com; 

    port = 7777;

    type = socks5;

      // known types: socks4, socks5, http-connect, http-relay


    // login = username;

    // password = password;

}


run the config

sudo redsocks -c /etc/redsocks.conf


if there was "bind: Address already in use" try to kill it

killall redsocks


to check if there the port is bound to local port 12345

netstat -tulpn

if command not found you can install it first

sudo apt install net-tools


Now change the iptables to the redsocks to rule them all through the proxy


sudo iptables -t nat -N REDSOCKS


sudo iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN

sudo iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN

sudo iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN

sudo iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN

sudo iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN

sudo iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN

sudo iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN

sudo iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN

    

sudo iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 12345

    

sudo iptables -t nat -A OUTPUT -p tcp --dport 443 -j REDSOCKS

sudo iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDSOCKS

    

sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDSOCKS

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDSOCKS



after running the above command all the traffics should be re-routed to the proxy that you configured including apt and any other GUIs applications


to reset the iptables

sudo iptables -F

sudo iptables -t nat -F

sudo iptables -t mangle -F

sudo iptables -X


No comments:

Post a Comment

Back to top