文章

文章列表

Linux

Linux代理(proxy)设置

Linux代理(proxy),设置环境变量。

Linux代理(proxy)设置

有关网络代理的环境变量

环境变量说明可选的取值
http_proxyhttp协议的网络连接使用该代理。ip:port
http://ip:port
socks://ip:port
socks4://ip:port
socks5://ip:port
https_proxyhttps协议的网络连接使用该代理。
ftp_proxyftp协议使用该代理。
all_proxy所有网络协议的网络连接都使用该代理;
all_proxy变量的优先级低于以上变量(http_proxy等)。
no_proxy无需代理的主机和域名,
支持通配符,
多个主机 / 域名之间使用逗号分隔。
localhost,10.*,*.ad.com,

proxy.sh

portport2是端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash

arg=$1


set_proxy() {
  default_all_port=1089
  default_http_port=8889
  default_all_protocol=socks5


  read -p "Please enter all_proxy port ($default_all_port by default):" all_port
  all_port=${all_port:-$default_all_port}

  read -p "Please enter all_proxy protocol, socks5 or http ($default_all_protocol by default):" all_protocol
  all_protocol=${all_protocol:-$default_all_protocol}

  read -p "Please enter http_proxy port ($default_http_port by default):" http_port
  http_port=${http_port:-$default_http_port}

  # 小写
  # export all_proxy=socks5://127.0.0.1:$all_port
  export all_proxy=$all_protocol://127.0.0.1:$all_port

  export http_proxy=http://127.0.0.1:$http_port
  export https_proxy=http://127.0.0.1:$http_port

  # 大写
  # export ALL_PROXY=socks5://127.0.0.1:$all_port
  export ALL_PROXY=$all_protocol://127.0.0.1:$all_port

  export HTTP_PROXY=http://127.0.0.1:$http_port
  export HTTPS_PROXY=http://127.0.0.1:$http_port
}

unset_proxy() {
  unset all_proxy
  unset http_proxy
  unset https_proxy

  unset ALL_PROXY
  unset HTTP_PROXY
  unset HTTPS_PROXY
}

help() {
  echo "    help:"
  echo "       set      set proxy"
  echo "       unset    unset proxy"
  echo "       proxy    check proxy"
  echo "       help     help information"
  echo
  echo -e "use \e[32m. proxy.sh\e[0m or \e[32msource proxy.sh\e[0m"
}

if [[ $arg = 'set' ]];then
  set_proxy
elif [[ $arg = 'unset' ]];then
  unset_proxy
elif [[ $arg = 'proxy' ]];then
  env |grep -i proxy
else
  help
fi

使用:

1
2
3
4
. proxy.sh help
. proxy.sh set
. proxy.sh unset
. proxy.sh proxy

参考文献 Linux设置网络代理

本文由作者按照 CC BY 4.0 进行授权