使用OpenSSH登录远程主机

  1. 常用的命令行登录远程主机:
$ ssh user@your_serve_ip
# 如:ssh [email protected]
# 如果有注册域名并解析至对应IP, 也可如下:
$ ssh user@domainname
# 提示输入密码
$ ssh [email protected]'s password:
# 输入正确密码即可登录远程主机
  1. 使用SSH密钥登录:
  • 生成密钥文件:
# 生成密钥对文件
# 要求你输入一个密钥文件名,如果直接enter,则可能使用默认的文件名:id_rsa覆盖掉旧的密钥文件(推荐输入)
$ ssk-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/Mark/.ssh/id_rsa): ubuntu_rsa
# 要求输入密码可为空(省事), 可输入
# 查看.pub文件内容,格式为:ssh-rsa开头,[email protected]结尾
$ cat xxx.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/yQUuXrjnuo7Y+yZGcGHZRogBPTyjwj39Qi/2MOEblYc+SYQKjDbSIgAjsK0nJOf6bQm0yozP/gb+5Ldp1GVUdhijDN9InSGvTS077E3U7juGW0gg7c8O7Nek8/wnUxv/fnHwrnVrfA0pUrVcZcLsTSAKfjuo775I3eXZ/XvYirDTgBTXlQyGplYW1oSedqQ0i7Xdki+SDPtLJB4mA90Ms6ndCuF9hwxtS8x+GpY8h56GU10KwHy6OFZpDngBIRTf7YzvMpfgmseCyVBT97vmFtvAO3rm2m0rJbYdvPT16HHXxqPCTJF3Y3j1EFSj5QxX1etOyr3nS4cdDRqNQsn9 [email protected]

  • 上传.pub内容到远程主机的~/.ssh/authorized_keys文件中:

上传方法一: 使用ssh-copy-id命令行

$ ssh-copy-d -i ~/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: " ~/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 
# 输入远程主机的密码确认
# 即显示添加成功
Number of key(s) added:        1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
# 直接登录即可
$ ssh '[email protected]'

# 进入远程主机:
$ chmod go-w ~/
$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/authorized_keys

上传方法二:手动复制

$ cat ~/.ssh/id_rsa.pub
# 将输出内容复制,格式为:ssh-rsa开头,[email protected]结尾
# 连接远程主机
$ mkdir -p ~/.ssh
$ echo <#pub_content#> >> ~/.ssh/authorized_keys
# 更新文件权限
$ chmod go-w ~/
$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/authorized_keys
# 直接登录即可
$ ssh '[email protected]'

你可能感兴趣的:(使用OpenSSH登录远程主机)