windows下使用vscode远程连接Linux服务器进行开发

远程连接Linux服务器

通过vscode中的Remote-SSH插件完成远程连接,这个插件是微软帝国亲自操刀编写的,让你通过SSH连接远程服务器作为本地的开发环境。

安装OpenSSH

该服务的作用是让你可以在终端使用ssh指令,Windows10通常自带OpenSSH不需要安装。

  • Windows10下检查是否已经安装OpenSSH
    # Win + X -> 选择Windows PoweShell(管理员)
    $Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
    # 如果电脑未安装OpenSSH,则State会显示NotPresent | 安装了则是Installed
    $Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
    
    # Win+R输入cmd进入终端
    $ssh

安装Remote-SSH

  1. 打开VSCode左侧的插件搜索: SSH
  2. 找到Remote-SSH直接安装即可

配置Remote-SSH

  1. 安装完成后,在VSCode左侧会出现一个远程资源管理器图标,点击
  2. 左侧右边的SSH Targets右上方点击管理图标会出现一个输入框,进入config配置文件: C:\Users\ACS1.ssh\config
  3. 在配置文件中设置服务器信息,输入HostName和User,保存以后左侧会出现对应机器名称
    Host Linux # 随意机器名
        Hostname 10.6.36.231 # 服务器IP地址
        User kh # 用户名
    Host Windows # 随意机器名
        Hostname 10.6.36.230 # 服务器IP地址
        User AstriACS # 用户名
  4. 更改设置: File -> Preferences -> Settings -> Extension -> Remote-SSH
    • 找到Show Login Terminal并勾选

连接服务器

  1. 在左侧出现的对应机器名称的右侧有一个按钮,点击
  2. 选择服务器的平台,并输入密码
  3. 成功连上服务器,点击有右侧的+号创建服务器的终端窗口,可以正常使用了!

进入服务器的文件夹

  1. 左侧点击就可以看到 Open Folder,打开以后可以看到服务器文件目录
  2. 直接在文件目录中选择文件进行编辑,实时同步到服务器上,这时候已经可以开始愉快的进行开发了,开发体验媲美本地开发!

Linux开发

查看系统信息

$cat /proc/version              # 查看Linux内核版本命令
$uname -a                       # 查看Linux内核版本命令

$lsb_release -a                 # 查看Linux系统版本的命令: Ubuntu 20.04.1 LTS
$cat /etc/issue

$echo $PATH                     # 查看PATH环境变量
$vim /etc/profile               # 添加PATH环境变量
# 在文档最后,添加: export PATH="/opt/xxx/bin:$PATH"
# 保存,退出: Esc : wq
$source /etc/profile

$cd ~                           # 用户的主目录: /home/name

查看GPU信息

$lspci | grep -i nvidia         # 查看NVIDIA GPU显卡信息
3b:00.0 3D controller: NVIDIA Corporation GV100GL [Tesla V100 PCIe 32GB] (rev a1)
af:00.0 3D controller: NVIDIA Corporation GV100GL [Tesla V100 PCIe 32GB] (rev a1)
d8:00.0 3D controller: NVIDIA Corporation GV100GL [Tesla V100 PCIe 32GB] (rev a1)
序号 3b:00.0, af:00.0, d8:00.0 是显卡的代号

$lspci -v -s 3b:00.0            # 查看指定显卡的详细信息
$nvidia-smi                     # 查看NVIDIA GPU显卡使用情况
$watch -n 10 nvidia-smi         # 周期性的输出显卡的使用情况, -n后边跟的是执行命令的周期,以s为单位
$sudo lshw -c video             # 查看显卡驱动
configuration: driver=nvidia latency=0

$apt search nvidia-cuda-toolkit # 查询目前可安装的CUDA Toolkit版本

Ubuntu20.04安装CUDA Toolkit

  1. 安装NIVIDIA GPU的驱动
    $sudo apt update            # 更新软件源
    $sudo apt upgrade           # 升级软件包
    $nvidia-smi                 # 查看支持的cuda版本, 如果无法查看,则说明尚未安装nvidia驱动
    • 查看CUDA不同的版本在linux平台上需要的驱动版本: here
  2. 下载对应平台架构的CUDA Toolkit x.0
    $uname -a                   # 查到Linux的架构是x86_64
    $nvcc -V                    # 查看系统当前安装的CUDA的版本: Build cuda_11.0_bu.TC445_37.28540450_0
    $conda activate facenet
  3. 不用修改facenet代码,测试GPU是否能够使用 (CUDA 11.0 + tensorflow-gpu 1.14.0)
    $pip install tensorflow-gpu==1.14.0 # CUDA 11.0 + tensorflow-gpu 1.14.0
  4. 修改facenet代码,测试GPU是否能够使用 (CUDA 11.0 + tensorflow-gpu 2.4.0)
    $pip install tensorflow-gpu==2.4.0 # CUDA 11.0 + tensorflow-gpu 2.4.0
    - 修改文件:
        1. facenet/src/align/align_dataset_mtcnn.py
        2. anaconda3/envs/facenet/lib/python3.6/site-packages/align/detect_face.py
    - 修改内容: 把所有的 tf. 替换为 tf.compat.v1.  (兼容性处理: tf.compat允许您编写在TensorFlow 1.x和2.x中均可使用的代码)
    
    - 修改文件: anaconda3/envs/facenet/lib/python3.6/site-packages/align/detect_face.py
    - 修改内容: 把第194行的 feed_in, dim = (inp, input_shape[-1].value) 替换为 feed_in, dim = (inp, input_shape[-1])
    $conda activate facenet
    $pip install numpy==1.16.2
    $pip install scipy==1.2.1
    $python src/align/align_dataset_mtcnn.py datasets/lfw datasets/haha --image_size 160 --margin 32 --gpu_memory_fraction 0.5
    Successfully opened dynamic library libcudart.so.11.0
    Creating networks and loading parameters
    tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX512F
    To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
    tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
    Successfully opened dynamic library libcuda.so.1
    tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties: 
        pciBusID: 0000:3b:00.0 name: Tesla V100-PCIE-32GB computeCapability: 7.0
        coreClock: 1.38GHz coreCount: 80 deviceMemorySize: 31.75GiB deviceMemoryBandwidth: 836.37GiB/s
    tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 1 with properties: 
        pciBusID: 0000:af:00.0 name: Tesla V100-PCIE-32GB computeCapability: 7.0
        coreClock: 1.38GHz coreCount: 80 deviceMemorySize: 31.75GiB deviceMemoryBandwidth: 836.37GiB/s
    tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 2 with properties: 
        pciBusID: 0000:d8:00.0 name: Tesla V100-PCIE-32GB computeCapability: 7.0
        coreClock: 1.38GHz coreCount: 80 deviceMemorySize: 31.75GiB deviceMemoryBandwidth: 836.37GiB/s
    Successfully opened dynamic library libcudart.so.11.0
    Successfully opened dynamic library libcublas.so.11
    Successfully opened dynamic library libcublasLt.so.11
    Successfully opened dynamic library libcufft.so.10
    Successfully opened dynamic library libcurand.so.10
    Successfully opened dynamic library libcusolver.so.10
    Successfully opened dynamic library libcusparse.so.11
    tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'libcudnn.so.8'; dlerror: libcudnn.so.8: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: :/usr/local/cuda-11.0/lib64/:/usr/local/cuda-11.0/lib64/
    tensorflow/core/common_runtime/gpu/gpu_device.cc:1757] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
    Skipping registering GPU devices...
    tensorflow/core/common_runtime/gpu/gpu_device.cc:1261] Device interconnect StreamExecutor with strength 1 edge matrix:
    tensorflow/core/common_runtime/gpu/gpu_device.cc:1267]      0 1 2 
    tensorflow/core/common_runtime/gpu/gpu_device.cc:1280] 0:   N Y Y 
    tensorflow/core/common_runtime/gpu/gpu_device.cc:1280] 1:   Y N Y 
    tensorflow/core/common_runtime/gpu/gpu_device.cc:1280] 2:   Y Y N 
    WARNING:tensorflow:From /home/kh/anaconda3/envs/facenet/lib/python3.6/site-packages/align/detect_face.py:213: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
    Instructions for updating:
    Deprecated in favor of operator or tf.math.divide.
    tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:196] None of the MLIR optimization passes are enabled (registered 0 passes)
    tensorflow/core/platform/profile_utils/cpu_utils.cc:112] CPU Frequency: 2200000000 Hz

远程连接Windows服务器

配置远程Windows服务器

环境配置

用ssh连接时,本地称为client,远程主机称为host。

Windows服务器安装 OpenSSH:
- 设置 -> 应用 -> 应用和功能 -> 管理可选功能
- 查看OpenSSH客户端是否已安装
- 如果没有,则在页面顶部选择“添加功能”: OpenSSH客户端 or OpenSSH 服务器

SSH服务器的初始配置

在搜索框中搜索powershell,单击“以管理员身份运行”,然后运行以下命令来启动 SSHD 服务:

$Start-Service sshd                                 # OPTIONAL but recommended:
$Set-Service -Name sshd -StartupType 'Automatic'    # Confirm the Firewall rule is configured. It should be created automatically by setup. 
$Get-NetFirewallRule -Name *ssh*                    # There should be a firewall rule named "OpenSSH-Server-In-TCP", which should be enabled
# If the firewall does not exist, create one
$New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22         

找到Windows服务器的的username: C:\Users\username的username(AstriACS)

配置本地电脑

启动PowerShell测试ssh连接

# $Ssh username@servername    # <username> 是服务器 <server> 上的用户账号
$Ssh AstriACS@10.6.36.230
# 到任何服务器的第一个连接都将生成类似以下内容的消息
The authenticity of host servername (10.00.00.001) cant be established.
ECDSA key fingerprint is SHA256:(<a large string>).
Are you sure you want to continue connecting (yes/no)?
# 回答 Yes 会将该服务器添加到本地系统的已知 ssh 主机列表中
# 系统此时会提示你输入密码(也就是远程主机的开机密码)。 作为安全预防措施,密码在键入的过程中不会显示
# 在连接后,你将看到类似于以下内容的命令shell提示符: 
astriacs@DESKTOP-U1MHT5M C:\Users\AstriACS> # domain\username@SERVERNAME C:\Users\username>

在vscode中连接

To connect to a remote host for the first time, follow these steps:
Verify you can connect to the SSH host by running the following command from a terminal / PowerShell window replacing user@hostname as appropriate.

$ssh user@hostname          # for Linux Server
$ssh user@domain@hostname   # Or for Windows when using a domain / AAD account 

打开vscode,打开Command Palette (F1/ctrl+shift+P) ,输入”Remote-SSH: Connect to Host…” 并选中,选择”+ Add New SSH Host…”,and use the same user@hostname as above,选择服务器的平台,并输入密码,可以正常使用了!

Author: ElaineXHZhong
Link: https://elainexhzhong.github.io/2021/06/03/windows下使用vscode远程连接Linux服务器进行开发/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.