暗中观察

CentOS 使用ssh密钥登录
1.添加用户useradd -d /home/sunjianhua sunjianhua 2.生成密钥su -...
扫描右侧二维码阅读全文
31
2019/07

CentOS 使用ssh密钥登录

1.添加用户

useradd   -d /home/sunjianhua sunjianhua

2.生成密钥

su -sunjianhua 
cd /home/sunjianhua
ssh-keygen -t rsa -b 2048
cd .ssh
cat id_rsa.pub > authorized_keys
mkdir sunjianhua
mv id_rsa*  ./sunjianhua
tar -zcvf   sunjianhua.tar.gz    sunjianhua
sz  sunjianhua.tar.gz
rm -rf  sunjianhua*
su root
chmod   600 authorized_keys

3.设置ssh并登录

# vim  /etc/ssh/sshd_config
在文件中变更一下内容:
Port 12345
UseDNS no
AddressFamily inet
PermitRootLogin no
SyslogFacility AUTHPRIV
PasswordAuthentication no
AllowUsers sunjianhua
# 重启sshd服务
service   sshd  restart  

然后登陆: sunjianhua/刚才下载的私钥
注:如果登录不了,需要在防火墙里面配置12345端口

4.免密登录到目标服务器
将你的公钥填充到一个远程机器上的authorized_keys文件中!
ssh-copy-id -i ~/.ssh/id_rsa.pub user@machine

5.设置多个免密登录配置

# vim .ssh/config 
Host test1
HostName 149.129.167.111
Port 22
User root
IdentitiesOnly yes

Host test2
HostName 149.129.172.222
Port 22
User root
IdentitiesOnly yes

Host test3
HostName 47.241.118.333
Port 8888
User root
IdentitiesOnly yes
  1. 配置git多个key
    Host host1

       HostName www.host2.com
       IdentityFile C:\Users\username\.ssh\id_rsa
       PreferredAuthentications publickey
       User userName

    Host host2

       HostName www.host2.com
       IdentityFile C:\Users\username\.ssh\id_rsa
       PreferredAuthentications publickey
       User userName

    Host host3

       HostName www.host3.com
       IdentityFile C:\Users\username\.ssh\id_rsa_company
       PreferredAuthentications publickey
       User userName

    tips: https://www.cnblogs.com/ayseeing/p/4445194.html

Last modification:September 7th, 2022 at 04:24 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment