- 使用ssh链接必须在同一局域网内,不在同一局域网需要使用内网穿透,这里介绍的是在同一局域网内。
一、ssh密钥设置
参考链接:https://www.cnblogs.com/ElEGenT/p/11844560.html
1.原理
密码方式的即时认证方式 。而公私钥 是在服务器保存一份已经通过认证的加密串,登录时通过这个加密串去认证。
公钥是可以传播的,私钥只能在自己的本地
公私钥的工作原理, 可以参考这篇文章:https://blog.csdn.net/csm201314/article/details/78453579
2.本地通过ssh免密码登录到服务器。(本地win10,服务器ubuntu)
1)生成 公私钥
生成密钥对id_rsa,id_rsa.pub,在本地打开cmd终端,输入命令:
ssh-keygen
直接回车
The key's randomart image is:
+--[ RSA 2048]----+
| ..oooE.++|
| o. o.o |
| .. . |
| o . . o|
| S . . + |
| . . . o|
| . o o ..|
| + + |
| +. |
+-----------------+
2)将公钥id_rsa.pub复制到服务器
进到本地ssh目录, 一般在 C:\用户\用户名.ssh。输入以下命令:
type id_rsa.pub | ssh -p 端口号 服务器用户@服务器IP "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
3)禁止密码登陆
回到服务器那边操作,输入命令编辑 sshd_config 文件设置禁止密码登录,找到 PasswordAuthentication yes 把 yes 改成 no ,如:
编辑文件:
sudo vi /etc/ssh/sshd_config
修改:
PasswordAuthentication no
保存退出并重启 ssh 服务:
systemctl restart sshd.service
4)测试是否设置成功
回到本地操作,不要断开刚才打开服务器链接的窗口,另外新建一个链接服务器终端窗口,输入命令:
服务器用户@服务器IP
如果成功链接服务器,说明设置已经成功。
不成功的话,返回刚刚保持链接服务器的窗口重新检查设置是否正确。
二、remote ssh链接服务器并调试其代码
1.remote ssh链接服务器
- 下载remote ssh插件
- 点击如下图图标
- 打开c:\Users\用户名\config,SSH TARGETS-》设置-》c:\Users\用户名\config
- 输入以下内容:
# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
Host alias
HostName 服务器IP地址
User 服务器用户名
Port ssh使用的端口号
- 打开file-》Preferences-》Settings-》Extensions-》Remote SSH,找到Show Login Terminal,并勾选。
2.调试代码
参考链接:https://blog.csdn.net/qq_33764934/article/details/103974973
- vscode连接服务器后,下载python
- 生成tasks.json文件,按shift+ctrl+b,在屏幕中上的搜索栏中点击配置生成任务,然后点使用模板创建tasks.json,最后点击other生成tasks.json程序
然后把下面的代码复制,覆盖原始生成代码:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run Pyhton Code",
"type": "shell",
"command": "/home/xxx/Anaconda3/envs/faceDetection/bin/python3.5",
"args": [
"'${file}'"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared"
}
}
]
}