一、连接ssh服务器

1.打开Visual Studio Code,进入拓展市场(Ctrl+Shift+X),下载拓展Remote - SSH

2. 点击远程资源管理器选项卡,并选择远程(隧道/SSH)类别

3. 点击ssh配置

4.在弹出的选择配置文件中,点击第一个

 5.在config文件中输入以下内容

Host <显示的服务器名字>

HostName <服务器的ip>

Port 22

User

如:

6.保存后点击左边刷新按钮

 7.这时展开SSH,就能看到刚刚创建的配置了

 

8. 使用Ctrl + Shift + P,打开命令窗口,输入ssh connect to host,选择第一个(在当前窗口连接)或第二个(在新窗口连接)都可以

9.选择刚刚创建好的那个配置

10.询问是否保存known_hosts,选择Continue即可

 11.输入访问的密码(即第5步配置的用户,其对应的登陆密码)

 12.该用户第一次访问该服务器可以看到该提示信息,耐心等待即可,这时是插件在服务器上面安装需要的依赖,大约会占用服务器150mb左右的空间

 13.如果长时间都一直是该情况,可以使用Ctrl + Shift + P,打开命令窗口,输入reload window来重新加载窗口(会要求你重新手动输入密码)

14.最终显示如下页面就代表已经连接成功了

 

 二、免密登录

1.生成ssh使用的公钥/密钥对,请从客户端上的 PowerShell 或 cmd 提示符运行以下命令,具体使用方法详细见:微软官方

ssh-keygen -t rsa

2.选择的配置文件就是之前连接到服务器使用的配置文件,或者打开刚刚创建的公钥文件

3.进入到编辑页面后,选择id_rsa.pub

4.拷贝id_ras.pub文件中的内容

5.进入终端输入以下命令

 echo "xxxx" >> ~/.ssh/authorized_keys

如:

6.验证方式

使用Ctrl + Shift + P,打开命令窗口,输入重启窗口命令reload window,确认左下角进入到连接成功的状态,期间没有提示输入密码的窗口,即代表成功了

 点击资源管理器的打开文件夹,选择任意文件夹打开,期间没有要求输入密码,也代表免密登录配置成功了

三、常见错误情况处理

1.连接过程中出现错误提示:Permission denied (publickey).

这是因为服务器默认禁用了ssh密码登录权限。修改方法如下:

在服务器上的终端输入:sudo vim /etc/ssh/sshd_config,打开该文件。找到PasswordAuthentication,将其后的 no 改为 yes。重启ssh服务:sudo service sshd restart

2.对于堡垒机的解决方法(未验证)

有时,您可能需要通过公司的内部网或防火墙从台式机或笔记本电脑连接到远程计算机。在这种情况下,您可能正在使用中间服务器或跳转框。如果您在配置为仅接受来自固定主机集的SSH连接的安全系统中工作,则这种设置非常有用。

要使用带有Remote - SSH扩展的跳转框设置,您可以使用ProxyCommand config选项。此配置将打开到跳转框的后台SSH连接,然后通过私有IP地址连接到目标。

您可以在SSH配置文件中设置ProxyCommand config选项,如下所示:

# Jump box with public IP address

Host jump-box

HostName 52.179.157.97

User sana

IdentityFile ~/.ssh/jumpbox

# Target machine with private IP address

Host target-box

HostName

User sana

IdentityFile ~/.ssh/target

ProxyCommand ssh -q -W %h:%p jump-box

或者是

This requires making an ssh tunnel inside the bastion tunnel which will allow multiple connections:

Add the following to your $HOME.ssh\config

Host tunnel

HostName 127.0.0.1

Port 2222

User mylogin

StrictHostKeyChecking=No

UserKnownHostsFile=\\.\NUL

Add the tunnel connection to your Establish the tunnel connection: az network bastion tunnel --name mybastion --resource-group myrg --target-resource-id /subscriptions//resourceGroups/myrg/providers/Microsoft.Compute/virtualMachines/myvm --resource-port 22 --port 22 Create an ssh tunnel in the bastion tunnel ssh -L 2222:127.0.0.1:22 mylogin@127.0.0.1 Have VSCode connect to your "tunnel" connection above.

参考以下文:Remote SSH via Azure Bastion (with AAD auth) · Issue #7179 · microsoft/vscode-remote-release (github.com)Visual Studio Code Remote SSH Tips and Tricks

Make the limitations of the `az network bastion tunnel` feature explicit · Issue #98747 · MicrosoftDocs/azure-docs (github.com)

相关文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。