目录
1. powershell 生成key:
2. 在服务器上安装公钥
3).为了确保连接成功,输入如下指令以保证以下文件权限正确:
3 开启 ssh 密钥登录
vscode 远程连接配置
python连接
python实现
1. powershell 生成key:
在命令行执行ssh-keygen
来创建密钥对,默认情况下,会生成一个私钥(id_rsa
)和一个公钥(id_rsa.pub
)
一般保存目录:
C:\Users\xxxx\.ssh
2. 在服务器上安装公钥
1).
拷贝id_rsa.pub中的公钥,并添加到authorized_keys中:
mkdir -p ~/.ssh
# {YOUR_PUB_KEY}是拷贝的公钥,以ssh-rsa开头
echo "{YOUR_PUB_KEY}" >> ~/.ssh/authorized_keys
2). 把 id_rsa.pub文件拷贝到服务器上,
执行覆盖:
cat id_rsa.pub > ~/.ssh/authorized_keys
执行追加:
cat id_rsa.pub >> ~/.ssh/authorized_keys
如此便完成了公钥的安装。
3).为了确保连接成功,输入如下指令以保证以下文件权限正确:
cd /root/.sshchmod 600 authorized_keys
chmod 700 ~/.ssh
方法2
chmod 600 /root/.ssh/authorized_keys
chmod 700 /root/.ssh
3 开启 ssh 密钥登录
vim /etc/ssh/sshd_config
将 PubkeyAuthentication 和 AuthorizedKeysFile 这两个选项的注释取消掉AuthorizedKeysFile 选项后面的路径指定了从哪里读取公钥文件
修改完毕重启远程服务器
vscode 远程连接配置
Host xxxxHostName xxxUser xxxPort xxxIdentityFile "C:\Users\xxx\.ssh\id_rsa"
原文链接:https://blog.csdn.net/TheKoi/article/details/129571955
python连接
ssh-keygen -t rsa -b 2048 -C "your_email@example.com"
import paramiko # 导入paramiko库# 创建SSHClient实例
ssh = paramiko.SSHClient()
# 自动添加未在known_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 通过密钥进行连接
try:ssh.connect(hostname='your_windows_ip', username='your_username', key_filename='path_to_your_private_key') # 连接到Windowsprint("连接成功!")# 执行命令stdin, stdout, stderr = ssh.exec_command('dir') # 执行Windows命令print(stdout.read().decode()) # 打印命令输出except Exception as e:print(f"连接失败: {e}") # 处理异常finally:ssh.close() # 关闭SSH连接
python实现
"""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/.
"""
#ssh-copy-id.py
import argparse, os
from subprocess import calldef winToPosix(win):"""Converts the specified windows path as a POSIX path in msysgit.Example:win: C:\\home\\userposix: /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="C:\\Users\\Administrator\\.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)
重启服务:sudo service ssh restart
查看日志:
cat /var/log/auth.log