ssh-copy-id for windows

本文介绍在Windows环境下实现SSH公钥认证的方法,包括使用Python脚本和Shell命令两种方式。涉及cmder环境配置及常见问题解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备环境:cmder环境运行

1. python方式

参考:https://segmentfault.com/a/1190000016489772

执行命令样式:python copy_id_for_windows.py root@echo.dell

存在问题:需要输入两次密码,无法直接在cmder下运行,msysgit环境下未验证。

"""ssh-copy-id for Windows.

Example usage: python ssh-copy-id.py ceilfors@my-remote-machine

This script is dependent on msysgit by default as it requires scp and ssh.
For convenience you can also try that comes http://bliker.github.io/cmder/.
"""

import argparse, os
from subprocess import call

def winToPosix(win):
    """Converts the specified windows path as a POSIX path in msysgit.

    Example:
    win: C:\\home\\user
    posix: /c/home/user
    """
    posix = win.replace('\\', '/')
    return "/" + posix.replace(':', '', 1)

parser = argparse.ArgumentParser()
parser.add_argument("-i", "--identity_file", help="identity file, default to ~\\.ssh\\idrsa.pub", default=os.environ['HOME']+"\\.ssh\\id_rsa.pub")
parser.add_argument("-d", "--dry", help="run in the dry run mode and display the running commands.", action="store_true")
parser.add_argument("remote", metavar="user@machine")
args = parser.parse_args()

local_key = winToPosix(args.identity_file)
remote_key = "~/temp_id_rsa.pub"

# Copy the public key over to the remote temporarily
scp_command = "scp {} {}:{}".format(local_key, args.remote, remote_key)
print(scp_command)
if not args.dry:
    call(scp_command)

# Append the temporary copied public key to authorized_key file and then remove the temporary public key
ssh_command = ("ssh {} "
                 "mkdir ~/.ssh;"
                 "touch ~/.ssh/authorized_keys;"
                 "cat {} >> ~/.ssh/authorized_keys;"
                 "rm {};").format(args.remote, remote_key, remote_key)
print(ssh_command)
if not args.dry:
    call(ssh_command)

2. shell方式

参考:http://www.voidcn.com/article/p-ssegbhrp-btv.html

问题:提示文件*/id_rsa.pub不存在。应该是cmder的问题

ID_FILE=C:\Users\Administrator\.ssh\id_rsa.pub
#ID_FILE="/C/Users/Administrator/.ssh/id_rsa.pub"
GET_ID="cat ${ID_FILE}"
{ eval "$GET_ID" ; } | ssh ${1%:} "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys" || exit 1

 

### 在 Windows 上使用 `ssh-copy-id` 命令 由于 Windows 并未默认提供 `ssh-copy-id` 工具,因此需要采取其他方法来实现相同的功能。以下是几种可行的方法: #### 方法一: 使用 Git Bash 或 WSL (Windows Subsystem for Linux) 安装 Git Bash 或者启用并配置好 WSL 后,在这些环境中可以直接运行类 Unix 的命令。 对于 Git Bash 和 WSL 用户来说,只需打开对应的终端程序,并按照常规方式调用 `ssh-copy-id` 即可[^1]。 ```bash ssh-copy-id -i ~/.ssh/id_rsa.pub user@server_ip_address ``` #### 方法二: 手动复制公钥至远程主机 当无法直接利用 `ssh-copy-id` 时,可以通过手动操作完成同样的任务。具体步骤如下所示: 1. **生成 SSH 密钥对** 如果尚未创建过 SSH 秘钥,则需先执行此步。可以在 PowerShell 中通过 OpenSSH 客户端来进行这项工作。 ```powershell ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ``` 2. **获取本地用户的公钥** 将生成好的 `.pub` 文件中的内容读取出来准备粘贴给远端服务器。 ```powershell Get-Content "$env:USERPROFILE\.ssh\id_rsa.pub" ``` 3. **连接到目标机器并将公钥追加到授权密钥列表中** 登录到想要设置无密码访问的目标机器上,编辑或新建 `~/.ssh/authorized_keys` 文件,把之前获得的内容添加进去。 ```bash echo 'ssh-rsa AAAAB... your_email@example.com' >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys ``` 这种方法虽然稍微复杂一点,但是能够达到与 `ssh-copy-id` 类似的结果[^5]。 #### 方法三: 利用第三方工具模拟 `ssh-copy-id` 某些情况下也可以借助一些专门为此设计的小型应用程序或者脚本来简化流程。例如 PuTTY Suite 提供了一个名为 Pageant 的代理服务可以帮助管理私钥;而 WinSCP 这样的图形界面 SFTP 客户端也支持批量上传文件夹内的所有 .pub 文件作为已知主机的信任凭证[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值