Linux命令 ssh-copy-id 使用方法(将ssh 公钥上传到目标服务器进行免密登录)

本文详细介绍如何使用ssh-copy-id命令实现SSH免密登录。通过示例步骤,讲解如何安装公钥到远程机器的authorized_keys文件中,以及如何验证免密登录是否成功。

ssh-copy-id 命令的 man 说明文档

说明文档比较简单,就不再赘述翻译了

SSH-COPY-ID(1)                                                  SSH-COPY-ID(1)

NAME
       ssh-copy-id - install your public key in a remote machine’s authorized_keys

SYNOPSIS
       ssh-copy-id [-i [identity_file]] [user@]machine

DESCRIPTION
       ssh-copy-id  is a script that uses ssh to log into a remote machine (presumably using a login password, so password authenti-
       cation should be enabled, unless you’ve done some clever use of multiple identities) It also changes the permissions  of  the
       remote  user’s  home,  ~/.ssh, and ~/.ssh/authorized_keys to remove group writability (which would otherwise prevent you from
       logging in, if the remote sshd has StrictModes set in its configuration).  If the -i option is given then the  identity  file
       (defaults  to  ~/.ssh/id_rsa.pub)  is used, regardless of whether there are any keys in your ssh-agent.  Otherwise, if this:
       ssh-add -L provides any output, it uses that in preference to the identity file.  If the -i option is used,  or  the  ssh-add
       produced no output, then it uses the contents of the identity file.  Once it has one or more fingerprints (by whatever means)
       it uses ssh to append them to ~/.ssh/authorized_keys on the remote machine (creating the file, and directory, if necessary)

SEE ALSO
       ssh(1), ssh-agent(1), sshd(8)

OpenSSH                        14 November 1999                 SSH-COPY-ID(1)

使用示例

[root@localhost .ssh]# ssh-copy-id -i id_rsa.pub root@93.xx.xx.xx -p 27073
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
The authenticity of host '[93.xx.xx.xx]:27073 ([93.xx.xx.xx]:27073)' can't be established.
RSA key fingerprint is SHA256:uTxnRGKSmlgPgUD8Qaerf7Hgun0V7PLc5kKoHKnj3JI.
RSA key fingerprint is MD5:6b:0d:59:9e:e0:c5:c2:95:99:2a:c0:a4:b1:4c:af:a0.
Are you sure you want to continue connecting (yes/no)? yes
/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
root@93.xx.xx.xx's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh -p '27073' 'root@93.xx.xx.xx'"
and check to make sure that only the key(s) you wanted were added.


登录测试

ssh -p ‘27073’ ‘root@93.xx.xx.xx’
通过上面命令,不用输入密码直接可以登录目标服务器

### 配置SSH免密登录方法 在缺少 `ssh-copy-id` 命令的情况下,可以通过手动方式将本地公钥复制到远程服务器的 `~/.ssh/authorized_keys` 文件中,从而实现SSH免密登录。以下是具体的操作方法和注意事项: #### 1. 确保SSH服务已安装并运行 如果目标Linux服务器上未安装SSH服务,则需要先安装并启动SSH服务。例如,在CentOS系统中,可以使用以下命令完成安装与启动: ```bash yum install openssh-server -y # 安装SSH服务[^1] service sshd restart # 重启SSH服务[^1] ``` #### 2. 生成本地SSH密钥对 在本地服务器上生成SSH密钥对(如果没有现成的密钥)。可以使用 `ssh-keygen` 工具生成RSA密钥对: ```bash ssh-keygen -t rsa # 生成RSA密钥对[^4] ``` 执行上述命令后,系统会提示输入保存密钥的文件路径(默认为 `~/.ssh/id_rsa`)以及设置密码短语(passphrase)。如果不需要密码短语,直接按回车跳过即可。 生成完成后,私钥将保存在 `~/.ssh/id_rsa`,公钥将保存在 `~/.ssh/id_rsa.pub`。 #### 3. 将公钥复制到远程服务器 由于缺少 `ssh-copy-id` 命令,因此需要手动将本地公钥内容追加到远程服务器的 `~/.ssh/authorized_keys` 文件中。操作步骤如下: 1. 使用 `cat` 命令查看本地公钥内容: ```bash cat ~/.ssh/id_rsa.pub ``` 将输出的公钥内容复制下来。 2. 登录到远程服务器,并确保远程服务器上存在 `~/.ssh` 目录。如果不存在,可以创建该目录并设置正确的权限: ```bash mkdir -p ~/.ssh # 创建.ssh目录 chmod 700 ~/.ssh # 设置.ssh目录权限[^3] ``` 3. 在远程服务器上创建或编辑 `~/.ssh/authorized_keys` 文件,并将本地公钥内容追加到其中: ```bash echo "公钥内容" >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys # 设置authorized_keys文件权限[^3] ``` #### 4. 测试免密登录 完成上述配置后,尝试从本地服务器通过SSH连接到远程服务器: ```bash ssh 用户名@远程服务器IP地址 ``` 如果一切配置正确,此时应该能够成功登录而无需输入密码。 --- ### 注意事项 - 如果仍然无法实现免密登录,请检查以下几点: - 确保远程服务器上的SSH服务已启用并允许密钥认证。 - 确保 `~/.ssh` 目录及其文件的权限设置正确。 - 如果之前尝试失败,可能需要删除旧的公钥和私钥重新生成,并重复上述配置过程。 - 如果本地和远程服务器的SSH版本不一致,可能会导致密钥格式兼容性问题。例如,高版本SSH生成的密钥可能无法被低版本SSH识别。这种情况下,可以在生成密钥时指定较低的加密算法(如 `-m PEM` 参数)[^4]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿雷由

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值