Captain's Geek-Island Captain's Geek-Island
首页
生活如斯乎
架构师的路
  • 分类
  • 标签
  • 归档
沉洋官网 (opens new window)

SleepyOcean

走,找新大陆去
首页
生活如斯乎
架构师的路
  • 分类
  • 标签
  • 归档
沉洋官网 (opens new window)
  • 计算机基础

  • 并发专题

  • 性能调优专题

  • 工具专题

  • 源码框架专题

  • 设计模式

  • 分布式专题

  • 实战专题

  • 技术杂文

  • 云原生专题

  • 大数据分析专题

  • 前端专题

  • 运维专题

    • Linux基础 - 基础知识
    • Linux基础 - 命令手册
    • Windows 命令行配置代理
      • Windows命令
        • 磁盘管理
    • 自动运维 - 自动化脚本
    • 自动运维 - 配置自动化
    • 系统安装 - Win10安装教程
    • 系统配置 - MacOS配置
    • 系统配置 - Linux配置
    • 系统配置 - Android TV配置
    • 独门秘技 - 激活手册
  • 经验专题

  • 面试专题

  • 软实力专题

  • 架构师的路
  • 运维专题
SleepyOcean
2021-07-05

Windows 命令行配置代理

# 缘起

之前遇到在 Windows 下给终端(cmd,Git Bash,PowerShell)配置代理时,总是模模糊糊的就过去了,今天又折腾了一次,恰巧有时间记下来,不想要再次重复了。

其实命令很简单,跟在 Linux 下没什么区别。

set http_proxy=http://127.0.0.1:1080

set https_proxy=http://127.0.0.1:1080

set http_proxy_user=user
set http_proxy_pass=pass

set https_proxy_user=user
set https_proxy_pass=pass

# 恢复
set http_proxy=

set https_proxy=

# Ubuntu 下命令为 export
# export http_proxy=http://127.0.0.1:1080
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

就是两条命令,前两条。

# 要点

  1. 一定要加 http://,直接写域名或者 IP 不行。
  2. http 和 https 都要设置。

然后如果想验证是否成功配置了代理的话,用 ping 命令是不可以的

# ping 还是不行的原因

ping的协议不是https,也不是https,是ICMP协议。

# 验证方式

curl -vv http://www.google.com,用这条命令来验证,如果返回如下结果表示代理设置成功。

curl-google

这里还有一个坑是,cmd,Git Bash,PowerShell 设置的方式不同!!!有点精神分裂了。。。

  • cmd 中用 set http_proxy 设置

  • Git Bash 中用 export http_proxy 设置

  • PowerShell 中按照这样设置

    # NOTE: registry keys for IE 8, may vary for other versions
    $regPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'
    
    function Clear-Proxy
    {
        Set-ItemProperty -Path $regPath -Name ProxyEnable -Value 0
        Set-ItemProperty -Path $regPath -Name ProxyServer -Value ''
        Set-ItemProperty -Path $regPath -Name ProxyOverride -Value ''
    
        [Environment]::SetEnvironmentVariable('http_proxy', $null, 'User')
        [Environment]::SetEnvironmentVariable('https_proxy', $null, 'User')
    }
    
    function Set-Proxy
    {
        $proxy = 'http://example.com'
    
        Set-ItemProperty -Path $regPath -Name ProxyEnable -Value 1
        Set-ItemProperty -Path $regPath -Name ProxyServer -Value $proxy
        Set-ItemProperty -Path $regPath -Name ProxyOverride -Value '<local>'
    
        [Environment]::SetEnvironmentVariable('http_proxy', $proxy, 'User')
        [Environment]::SetEnvironmentVariable('https_proxy', $proxy, 'User')
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24

纠结于应该用 set 还是 export 还有一个判断方法是,敲一下这两个命令,如果返回一个长长的列表,就表示应该用这个命令,反之,如果返回找不到这个命令,就不应该用这个命令。

# 总结

这次应该是搞清楚了 Windows 下如何给 Terminal 设置代理,花了一个多小时的时间,感觉很值!

# 参考链接:

  • 命令行配置代理服务器 (opens new window)
  • windows终端命令行下如何使用代理? (opens new window)
  • windows(64位)下使用curl命令 (opens new window)
  • ICMP协议与ping原理 (opens new window)
  • ping (opens new window)
  • PowerShell Set-Proxy, Clear-proxy (opens new window)

# Windows命令

# 磁盘管理

# 输入以下命令进入diskpart管理工具
> diskpart


# 列出所有磁盘
DISKPART> list disk
  磁盘 ###  状态           大小     可用     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  磁盘 0    联机             1863 GB  1024 KB        *
  磁盘 1    联机             1863 GB  1024 KB        *
  磁盘 2    联机              953 GB      0 B
  磁盘 3    联机              465 GB  2048 KB        *
  磁盘 4    联机              953 GB      0 B
  磁盘 5    联机               59 GB  2561 MB        *

DISKPART> select disk 5

磁盘 5 现在是所选磁盘。

DISKPART> clean

DiskPart 成功地清除了磁盘。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#科学上网
上次更新: 2021/12/28, 07:12:00

← Linux基础 - 命令手册 自动运维 - 自动化脚本 →

新鲜出炉
01
记录 - 快速搭建自动化部署平台
04-13
02
Docker搭建各类Paas服务
03-01
03
系统配置 - Android TV配置
02-12
更多文章>
Copyright © 2019-2022 SleepyOcean | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式