资讯专栏INFORMATION COLUMN

Windows系统使用cmder-ssh免密码登录linux服务器

JayChen / 1865人阅读

摘要:将公钥上传到服务器使用脚本完成操作将以上代码保存到本地,命名为,然后执行,其中为登陆用户名,为随后会提示输入远程服务器密码,密码正确则自动登陆服务器并把公钥文件复制到服务器。再次尝试登陆服务器会发现已经不需要密码了。

Linux服务器每次登陆或者scp复制文件时都需要繁琐的输入密码过程,而使用SSH Key来实现SSH无密码登录不仅免去了繁琐的密码输入步骤,也为Linux服务器增加了又一道安全防线(可以禁用掉ssh-root密码登录).

很多文章介绍ssh无密码登录方式都有多个步骤,其实远不必这么麻烦,接下来我们以windows系统cmder为例完成ssh无密码登录设置,要求下载的cmder为完整版。

SSH密钥和公钥是否存在?

首先看C:Users{用户名}目录下有没有.ssh目录,并且目录中是否已经存在id_rsa.pub文件,如果已经有该文件,请跳到步骤3,请不要轻易删除该文件,除非你知道该文件被覆盖/删除意味着什么。

生成SSH公钥和密钥文件

打开cmder,执行:ssh-keygen -t rsa,按Enter键,输入一个密码,然后再次输入同样的密码,密码至少要20位长度,随后就会在.ssh文件夹生成相对应的公私钥文件。

将SSH公钥上传到Linux服务器

使用脚本完成操作

"""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:homeuser
    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 ~.sshidrsa.pub", default=os.environ["HOME"]+".sshid_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)

将以上python代码保存到本地,命名为ssh-copy-id.py,然后cmder执行python ssh-copy-id root@xx.xx.xx.xx,其中root为登陆用户名,xx.xx.xx.xx为IP
随后会提示输入远程服务器密码,密码正确则自动登陆服务器并把公钥文件复制到Linux服务器。再次尝试登陆服务器会发现已经不需要密码了。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/42503.html

相关文章

  • 务器运维基础指南

    摘要:服务器运维基础指南作为一个开发人员,对于服务器的操作不可避免。最常用的操作就是部署应用到服务器,及在生产,测试环境通过查看日志排查问题。终端会提示你输入密码,此时输入密码,密码正确就可以登录服务器。 服务器运维基础指南 作为一个开发人员,对于服务器的操作不可避免。最常用的操作就是部署应用到服务器,及在生产,测试环境通过查看日志排查问题。 一般服务器都是linux操作系统并且是无图形界面...

    lufficc 评论0 收藏0
  • 阿里云技术教程系列-ECS远程连接 Linux 实例

    摘要:内容提示阿里云服务器入门教程步骤远程连接实例根据您本地的操作系统,您可以从等操作系统连接实例。根据提示,分别输入您的云服务器实例的用户名和密码。内容提示:阿里云ECS服务器入门教程:步骤 3 远程连接 Linux 实例 根据您本地的操作系统,您可以从 Windows、Linux、Mac OS X 等操作系统连接 Linux 实例。本文介绍常用的连接服务器方式。更全面详细的连接实例方式介绍,请...

    yedf 评论0 收藏0
  • 阿里云学生务器操作攻略(超详细全套完整图文教程!菜鸟必看!)

    摘要:第一步如何以最低价格快速买到阿里云服务器如果我是学生,我如何通过买到价值的服务答用购买云服务器是阿里云学生专属活动。除阿里云外,大多数云服务器公司例如西部等同配置云服务器的价格都在左右。例如云服务器地址等。第一步:如何以最低价格快速买到阿里云服务器?1.1 如果我是学生,我如何通过¥9.9买到价值¥117的服务?答:用¥9.9购买云服务器ECS是阿里云学生专属活动。首先,你需要在阿里云官网注...

    FuisonDesign 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<