SSH

概述

系统级配置文件路径/etc/ssh/sshd_config
用户级配置文件路径~/.ssh/config

配置

常用命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 登录远程主机
ssh user@host
# 如果本地用户名与远程用户名一致,登录时可以省略用户名
ssh host
# 默认端口是22,使用参数指定端口
ssh -p 2222 user@host
# 生成密钥
ssh-keygen -t rsa -C "comment"
# 将公钥拷贝到远程主机
ssh-copy-id user@host
ssh-copy-id -i ~/.ssh/id_rsa.pub user@host # 指定公钥文件
# 重启服务
service ssh restart

编辑~/.ssh/config

1
2
3
4
5
6
7
8
9
10
11
# github
Host GitHub
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
# gitlab
Host GitLab
HostName gitlab.com
Port 36000
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab

Host可用于HostName的别称,git clone git@github.com/aaa/bbb.git可替换为git clone git@GitHub/aaa/bbb.git
Port可省略设置端口号,ssh -p 36000 git@Github简写为ssh git@Github

默认情况下只会读取 id_rsa 文件,如果想将二个公钥 github、gitlab 一起读取需要使用命令:ssh-add ~/.ssh/id_rsa_github(gitlab 同理)可以使用ssh-add -l查看秘钥,在 Windows 中需要执行ssh-add ~/.ssh/id_rsa_gitlab命令。

在 Windows 中提示“Could not open a connection to your authentication agent.”,执行ssh-agent bash即可。

参考文献


SSH
https://laplac2.github.io/tools/ssh/
作者
Laplace
发布于
2020年1月1日
许可协议