Linux系统git代理与pip换源
一、配置 git 代理
1.确认当前环境
Ubuntu/Debian: sudo apt install socat
如果安装失败,在终端依次运行以下命令,看看输出:
1 | whoami |
如果 whoami 输出 root:直接去掉 sudo 执行安装命令即可:
1 | apt install socat |
2.如果 apt 可用且你是 root
1 | apt-get update |
3.修改config文件
1 | PROXY_HOST=127.0.0.1 |
4.验证与 git clone
1 | chmod 600 ~/.ssh/config |
首次连接会提示:Are you sure you want to continue connecting (yes/no/[fingerprint])?,输入yes
5.检查是否已有 SSH key
1 | ls -l ~/.ssh/id_*.pub 2>/dev/null |
没输出(提示 No such file)→ 执行第6步生成新 key
6.生成 SSH key(如果没有的话)
1 | ssh-keygen -t ed25519 -C "你的邮箱@example.com" -f ~/.ssh/id_ed25519 -N "" |
参数说明:
-t ed25519:推荐的现代算法
-C:注释,一般写你的 GitHub 邮箱
-f:指定文件路径
-N “”:不设密码(方便后续无人值守 clone;如果想加密码可以去掉这项)
生成后会得到两个文件:
~/.ssh/id_ed25519(私钥,千万别外泄)
~/.ssh/id_ed25519.pub(公钥,要贴到 GitHub)
7.查看并复制公钥内容
1 | cat ~/.ssh/id_ed25519.pub |
输出的公钥内容类似:ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...你的邮箱@example.com
8.把公钥添加到 GitHub
浏览器打开 https://github.com/settings/keys
点击右上角 New SSH key
Title:随便写,比如 算力平台
Key type:保持 Authentication Key
Key:粘贴刚才复制的那一整行公钥
点 Add SSH key
9.测试链接
1 | ssh -T git@github.com |
看到:Hi your-username! You've successfully authenticated, but GitHub does not provide shell access.,配置成功
二、pip 换源
覆盖环境变量,强制改用清华源(不需要凭证)
在 ~/.bashrc 中覆盖:
1 | cat >> ~/.bashrc <<'EOF' |
验证覆盖是否生效
1 | pip config list |
现在 :env:.index-url 应该显示为 https://pypi.tuna.tsinghua.edu.cn/simple
装个小包验证速度和成功率:
1 | pip install requests -v 2>&1 | tail -20 |
在 -v 日志里看到类似:Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
