Skip to content

Instantly share code, notes, and snippets.

@fearblackcat
Last active October 28, 2024 15:45
Show Gist options
  • Save fearblackcat/850c6e027d5a03017c44daaa6a7ffc30 to your computer and use it in GitHub Desktop.
Save fearblackcat/850c6e027d5a03017c44daaa6a7ffc30 to your computer and use it in GitHub Desktop.
Set proxy for terminal on mac

Shadowsocks Proxy

apt-get install python-pip
pip install shadowsocks

sudo ssserver -p 443 -k password -m aes-256-cfb --user nobody -d start

Client-side setup

  • Install Shadowsocks Client, e.g. run brew cask install shadowsocksx in Mac OSX.
  • SwitchSharp

Only config SOCKS Host to 127.0.0.1:1080 and select SOCKS v5

SOCKS 5 Through SSH Tunnel

# `-N`: do not execute commands
# `-D`: bind 1080 port and forward 1080 port to 22 port
# `-i`: use pre-shared key `hello.pem`
# `-p`: specify port used to connect to remote server
ssh -ND 1080 -i ~/.ssh/hello.pem <username>@<your-remote-server-ip> -p 22

Reference

Socks 5 in Terminal

Using Proxychains-ng to socksify your command

  • Install proxychains-ng by running brew install proxychains-ng.
  • Config /usr/local/Cellar/proxychains-ng/4.7/etc/proxychains.conf, modify the following parameters.
[ProxyList]
socks5 127.0.0.1 1080
  • Add proxychains4 to the front of every command, e.g. proxychains4 curl ipecho.net/plain

Also works for Application like Google Chrome while Safari does not work, try proxychains4 /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome . But this might slow down Chrome.

You can also use dsocks for mac(which does not work for me), tsocks for linux.

Using Unix Proxy Environment Variable

Add this script to ~/.bash_profile.

export http_proxy=socks5://127.0.0.1:1080
export https_proxy=socks5://127.0.0.1:1080

Config curl to use SOCKS5(Also works for homebrew)

Add the following line to ~/.curlrc. Since homebrew use curl to download the package, this will also enable homebrew to use SOCKS5.

socks5 = "socks5://127.0.0.1:1080"

There are no environment variables for SOCKS5 proxy servers in unix, so in order to use SOCKS5 in other utilities, check the man pages for existing tools to see if they have a configuration option for a SOCKS5 proxy and whether they have a configuration file that the configuration can be added to.

Config git to use SOCKS5

  • For https:// and http://(e.g. http://github.com, https://github.com), run the following script.
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'
  • For git://(e.g. git://github.com), run git config --global core.gitproxy 'socks5://127.0.0.1:1080'
  • For ssh(e.g. git@github.com,ssh://git@github.com), add ProxyCommand nc -x localhost:1080 %h %p to ~/.ssh/config file.

git config --global is stored in ~/.gitconfig while local config settings is in ./.git/config. To remove a configuration, e.g. run git config --global --unset core.gitproxy.

Reference

Tools

curl --socks5-hostname 127.0.0.1:1080 http://wtfismyip.com/json
@xujiazhe
Copy link

thank you!

you could put proxychains config file under
$HOME/.proxychains/proxychains.conf

@uqix
Copy link

uqix commented Nov 24, 2019

Thanks, this saved my day.

@PakhomovAlexander
Copy link

Thank you!

@mjzarrin
Copy link

mjzarrin commented Jun 6, 2020

Nice collection.
Thank you!

@Tokubara
Copy link

Thank you!

@gaunthan
Copy link

gaunthan commented Jul 2, 2022

Thanks! :D

@mezantrop
Copy link

You can try my TS-Warp to socksify traffic as well. Testing and feedback appreciated a lot.

@gaunthan
Copy link

gaunthan commented Jul 3, 2022 via email

@mbaneshi
Copy link

very useful, thank you

@gaunthan
Copy link

gaunthan commented Mar 25, 2023 via email

@keizure
Copy link

keizure commented Sep 27, 2023

thanks a lot

@gaunthan
Copy link

gaunthan commented Sep 27, 2023 via email

@tongyul
Copy link

tongyul commented Jun 27, 2024

There's an issue where recent versions of Homebrew run curl with the --disable flag which prevents the .curlrc config file from loading. The configuration file alone wouldn't be sufficient. I still need to check if Homebrew code also strips environment variables before running curl. FYI.
近期Homebrew版本有用--disable来避免curl使用.curlrc配置文件的问题。只更改配置文件是不够的。我还没查Homebrew代码在调用curl之前是否会去掉环境变量。请知悉。

@gaunthan
Copy link

gaunthan commented Jun 27, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment