windows和macOS下ssh配置config免密登陆服务器笔记!

mac使用命令行登陆远程服务器:
ssh    服务器用户名@服务器IP 
例如:
dragonersli@lishilongdeMacBook-Air ~ % ssh [email protected] #连接远程服务器
[email protected]'s password: #输入远程服务器密码
Last login: Sat Dec 21 16:54:14 2019 from 221.12.22.34

Welcome to Alibaba Cloud Elastic Compute Service !

[root@paijinhua ~]# pwd
/root
[root@paijinhua ~]# exit #退出远程服务器
登出
Connection to 47.110.242.239 closed.
dragonersli@lishilongdeMacBook-Air ~ %



每次连接要输入ip和密码,记不住怎么办?可以使用公私钥方式免密登陆!
cd ~/.ssh/
vim config

Host #ssh连接别名
HostName #服务器IP
User #服务器用户名
#Port端口什么的可以参考文档配置
config配置如下:
Host paijinhua_host
HostName 47.110.242.239
User root


Host paijinhua_test
HostName 47.111.140.177
User root

复制本机公钥到要连接的服务器
cat id_rsa.pub #复制内容
cd ~/.ssh #远程服务器
vim authorized_keys #黏贴内容



更方便的操作:直接本机复制到远程服务器
ssh-copy-id可以将公钥传输到远程服务器,实现ssh免密码登录。mac 10.10系统下面默认没有ssh-copy-id命令,可以自己安装。

https://github.com/beautifulcode/ssh-copy-id-for-OSX


ssh-copy-id -i ~/.ssh/id_rsa.pub 服务器用户名@服务器IP地址
执行ssh-copy-id将公钥传输到指定的服务器。第一次需要密码,后续连接不需要密码
dragonersli@lishilongdeMacBook-Air .ssh % ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/dragonersli/.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.

dragonersli@lishilongdeMacBook-Air .ssh % pwd
/Users/dragonersli/.ssh
dragonersli@lishilongdeMacBook-Air .ssh % ssh paijinhua_host
Last login: Sat Dec 21 17:35:36 2019 from 221.12.22.34

Welcome to Alibaba Cloud Elastic Compute Service !

[root@paijinhua ~]# pwd
/root
[root@paijinhua ~]#

远程服务器查看

Welcome to Alibaba Cloud Elastic Compute Service !

[root@paijinhua ~]# cd ~/.ssh/
[root@paijinhua .ssh]# ls
authorized_keys
[root@paijinhua .ssh]# cat authorized_keys  #未执行ssh-copy-id
[root@paijinhua .ssh]# cat authorized_keys  #执行ssh-copy-id后,自动复制公钥
ssh-rsa xxxx... [email protected] 
[root@paijinhua .ssh]# 

window下cmd窗口运行linux命令:

windows登录ssh:下win + R输入cmd然后ssh 服务器账号@服务器IP 然后输入密码可连接远程服务器。
配置免密登录:进入C:\Users\Administrator\.ssh新建config配置同上。不过ssh-copy-id没安装。要手动到服务器打开authorized_keys添加本机的id_rsa.pub内容
image.png
windows和macOS下ssh配置config免密登陆服务器笔记!_第1张图片
image.png

有一款软件cmder,可以在windows上使用linux命令,带色彩和自动补全等

总结:

本机~/.ssh配置config,设置远程服务器登陆别名服务器ip用户名等,如果不执行ssh-copy-id则每次ssh 别名都要输入密码,执行ssh-copy-id -i ~/.ssh/id_rsa.pub 服务器用户名@服务器IP后则可以ssh 别名免密登陆远程服务器。

你可能感兴趣的:(windows和macOS下ssh配置config免密登陆服务器笔记!)