Linux CentOS ssh 使用公钥 Public Key 免密登录

客户端

# 生成密钥对
# -N '' 为空密码(即不设置密码)
# -f 公钥存在地址
# -q 静默模式(不输出信息)
$ ssh-keygen -t rsa -b 2048 -N '' -f ~/.ssh/id_rsa -q

# 查看刚才生成的公钥内容
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7QMmAarXGxCAdwrtAoHC/AEFmHzO7qsVM4fuSc0zPwN5aSS62XdSYHOaoPJvZFfUGpMr+ZHakIrCeGQr85ucxMpvSZRbGPK6phdlolRIRcOhYdDDBmoRo3k9/Hr6LsIV4G6nUeXEeoSzCjIwMvDQJakL9LG+I/20ZSWr15Zv/gLFxTsCEZkcHMNUAHy8sLRZl0v9sjKft23Nti8oYacURss1jOaEE1Z2EgvveHHGbNgibl6yJWysoSlW61zZespvi34PdUhL0R9vsOai1Mesa8r8PCfGwZaEEMkc2GLfP4tpiNkN547992jfmAzHihW+CJmqh59+infZ3J6ZADzO1 [email protected]

服务器

# 添加登录用户 xchenhao
# -m 自动创建用户家目录
# -c 添加备注信息(在 /etc/passwd 中体现)
$ useradd -m xchenhao -c 'I am a test'

# 进入用户的家目录
$ cd /home/xchenhao

# 创建 .ssh 文件夹
$ mkdir .ssh/

# 设置文件夹权限
$ chmod 755 ./.ssh/

# 设置文件夹所属用户及用户组
$ chown xchenhao.xchenhao ./.ssh/

# 创建授权的公钥文件
$ touch ./.ssh/authorized_keys

# 将客户端所生成的公钥内容追加进该文件当中
$ echo 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7QMmAarXGxCAdwrtAoHC/AEFmHzO7qsVM4fuSc0zPwN5aSS62XdSYHOaoPJvZFfUGpMr+ZHakIrCeGQr85ucxMpvSZRbGPK6phdlolRIRcOhYdDDBmoRo3k9/Hr6LsIV4G6nUeXEeoSzCjIwMvDQJakL9LG+I/20ZSWr15Zv/gLFxTsCEZkcHMNUAHy8sLRZl0v9sjKft23Nti8oYacURss1jOaEE1Z2EgvveHHGbNgibl6yJWysoSlW61zZespvi34PdUhL0R9vsOai1Mesa8r8PCfGwZaEEMkc2GLfP4tpiNkN547992jfmAzHihW+CJmqh59+infZ3J6ZADzO1 [email protected]' >> ./.ssh/authorized_keys

# 设置文件权限
$ chmod 600 ./.ssh/authorized_keys

# 设置文件所属用户及用户组
$ chown xchenhao.xchenhao ./.ssh/authorized_keys

客户端

# ssh 连接服务器
# 输入 yes 确认继续
$ ssh [email protected]
The authenticity of host '192.168.1.16 (192.168.1.16)' can't be established.
ECDSA key fingerprint is SHA256:Hgfro0LJ2aVsMVfSdFGcD1q5VSRzPLuwkbnuP9pktyk.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.16' (ECDSA) to the list of known hosts.
# 连接成功
[xchenhao@localhost ~]$ whoami
xchenhao

其它

# 删除密码
$ passwd -d xchenhao

# 设置用户的密码
$ passwd xchenhao

# 删除用户
# -r 连同该用户的家目录一并删除
$ userdel -r xchenhao

参考

  • https://blog.csdn.net/gf_lvah/article/details/79941521 ssh-keygen 无回车生成公钥私钥对
  • https://www.cnblogs.com/seriawei/p/7774989.html Linux使用私钥公钥(Public key)登录 - 免密码登录
  • https://blog.csdn.net/a1010256340/article/details/75608037 使用ssh公钥实现免密码登录
  • https://www.cnblogs.com/zknublx/p/9360821.html linux命令useradd添加用户详解
  • https://idc.wanyunshuju.com/cym/84.html linux userdel命令详解删除用户账号

你可能感兴趣的:(运维,Linux)