Ubuntu代理网络配置
<blockquote><p>配置网络代理后,代理流量仍旧会被代理规则过滤,而不是全局代理。</p>
</blockquote>
<h2 id="终端">终端</h2>
<h3 id="临时配置">临时配置</h3>
<p>终端临时使用网络代理,关闭终端后,网络代理也将会关闭。</p>
<pre><code class="language-shell">export http_proxy="http://your-proxy-server:port"
export https_proxy="https://your-proxy-server:port"
# 代理所有协议app_proxy
export all_proxy="http://your-proxy-server:port"
# 若想使用sock5h进行连接。sock5h的优势是在远端进行DNS解析,更安全
export http_proxy="socks5h://your-proxy-server:port"
export https_proxy="socks5h://your-proxy-server:port"
export all_proxy="socks5h://your-proxy-server:port"
# 推荐
export http_proxy="http://your-proxy-server:port"
export https_proxy="https://your-proxy-server:port"
export all_proxy="socks5h://your-proxy-server:port"
</code></pre>
<h3 id="永久配置">永久配置</h3>
<pre><code class="language-shell"># 编辑 .bashrc
vim ~/.bashrc
# 末端添加
export http_proxy="http://your-proxy-server:port"
export https_proxy="https://your-proxy-server:port"
export all_proxy="http://your-proxy-server:port"
# sock5h版本
export http_proxy="socks5h://your-proxy-server:port"
export https_proxy="socks5h://your-proxy-server:port"
export all_proxy="socks5h://your-proxy-server:port"
# 我推荐的版本,混合使用。这样不影响一些只能通过http或者https协议的命令
export http_proxy="http://your-proxy-server:port"
export https_proxy="https://your-proxy-server:port"
export all_proxy="socks5h://your-proxy-server:port"
# 刷新.bashrc
source ~/.bashrc
</code></pre>
<h3 id="验证">验证</h3>
<p>若能输出配置的代理地址,则配置成功。也可通过<code>curl</code>命令查看网站信息。</p>
<p><strong>注意:</strong><code>ping</code>命令使用<code>ICMP</code>协议,大多数代理服务器不处理<code>ICMP</code>流量。</p>
<pre><code class="language-shell">echo $http_proxy
echo $https_proxy
echo $all_proxy
# 或者curl命令查看某国外网站信息。-I查看网站信息
curl -I XXX.com
</code></pre>
<h2 id="apt软件包管理工具">APT软件包管理工具</h2>
<p>部分软件仓库服务器位于国外,在国内可能连接超时,如Typora仓库、spotify仓库等。</p>
<p>如果终端配置了代理,可以使用<code>sudo -E apt update</code>临时继承代理环境变量。同样通过<code>curl</code>命令检测是否设置成功。</p>
<pre><code class="language-shell">sudo vim /etc/apt/apt.conf.d/95proxies
# 添加以下内容
Acquire::http::Proxy "http://your-proxy-server:port/";
Acquire::https::Proxy "https://your-proxy-server:port/";
# 文件传输,虽然目前一般都用http或者https
Acquire::ftp::Proxy "https://your-proxy-server:port/";
# socks5h版本。apt(UBuntu 18.04之后的版本)支持socks5,所以全用socks5也没问题
Acquire::http::Proxy "socks5h://your-proxy-server:port/";
Acquire::https::Proxy "socks5h://your-proxy-server:port/";
Acquire::ftp::Proxy "socks5h://your-proxy-server:port/";
</code></pre>
<h2 id="ssh">SSH</h2>
<p>因为咱们ssh连接的服务器一般都是国内的,所以不推荐修改。</p>
<ul>
<li>SOCKS代理:代理地址以<code>socks5://</code> 或者 <code>socks://</code> 开头。
<ul>
<li><code>-x</code>:表示后面是SOCKS代理服务器地址</li>
<li><code>-X 5</code>:表示SOCKS 5代理。如果是使用SOCKS 4,则改为<code>-X 4</code></li>
<li><code>%h</code>和<code>%p</code>:分别表示目标主机和端口。SSH 会自动将它们替换为代理服务器地址<code>your_proxy_address</code>和端口号<code>your_port</code></li>
</ul>
</li>
</ul>
<pre><code class="language-shell"># 修改ssh配置文件。如果没有,则创建
vim ~/.ssh/config
# 在文件内添加
Host example.com
Hostname example.com
ProxyCommand nc -x your_proxy_address your_port -X 5 %h %p
</code></pre>
<ul>
<li>HTTP代理:代理地址以<code>http://</code>开头。
<ul>
<li>借助工具<code>corkscrew</code>。<code>sudo apt install corkscrew</code></li>
</ul>
</li>
</ul>
<pre><code class="language-shell"># 修改ssh配置文件。如果没有,则创建
vim ~/.ssh/config
# 在文件内添加
Host example.com
Hostname example.com
ProxyCommand corkscrew your_proxy_address your_port %h %p
</code></pre><br><br>
来源:https://www.cnblogs.com/coder-shane/p/18410182
頁:
[1]