ssh配置

  • 多个账号配置ssh key
  • ssh免密码登录

多个账号配置ssh key

cd ~/.ssh
ssh-keygen -t rsa -C "youremail1@xxx.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/wu/.ssh/id_rsa): [ssh-key-name]

# ~/.ssh/下创建config文件
# ~/.ssh/config start
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    # 使用HostName的时候,指定使用~/.ssh/github的ssh密钥
    IdentityFile ~/.ssh/github

Host 192.168.110.220
    HostName 192.168.110.220
    PreferredAuthentications publickey
    Port 22
    IdentityFile ~/.ssh/192.168.110.220
# ~/.ssh/config end

ssh -T git@192.168.110.220 -p 22
# Welcome to GitLab, @chris!

ssh免密码登录

cd ~/.ssh
# 创建ssh密钥
ssh-keygen -t rsa -C "youremail1@xxx.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/wu/.ssh/id_rsa): [ssh-key-name]

# 复制指定ssh密钥到服务器,以后登录的时候就不需要输入密码了
ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 root@$SERVER

# 登录远端服务器
ssh root@$SERVER
cat ~/.ssh/authorized_keys
exit

# 测试跟服务器的连接
ssh -T root@$SERVER

非root用户,如何配置免密码登录

# 用非root用户登录到服务器,比如normal: ssh normal@$SERVER
sudo mkdir ~/.ssh
sudo chown $(whoami) ~/.ssh
sudo touch ~/.ssh/authorized_keys
sudo chown $(whoami)  ~/.ssh/authorized_keys
sudo chmod 600 ~/.ssh/authorized_keys
ls -l ~/.ssh/authorized_keys
# -rw------- 1 zsuser root 0 May 23 15:09 /home/zsuser/.ssh/authorized_keys

结尾