Summary of using server program development tools

The company uses a server and is exposed to the remote connection operation of Linux for the first time. It records the operational practices and doubts about using Vscode as a development tool.

Article directory

  • 1 VsCode tool
    • 1.1 vscode remote Remote-ssh operation practice and doubts
      • 1.1.1 vscode configure Remote-ssh
      • 1.1.2 Problems encountered
        • 1.1.2.1 Setting up SSH Host “MyIDC” Downloading VS Code Server stuck waiting
        • 1.1.2.2 After updating VsCode, the RemoteSSH connection is stuck at Setting up SSH Host XX:Copying VS Code Server to host with scp
      • 1.1.3 Other operation skills of vscode Remote-ssh
    • 1.2 Vscode development Python program operation practices and doubts
    • 1.2.0 debug configuration file
      • 1.2.1 Debug python code in vscode
    • 1.3 Vscode development C++ program operation practices and doubts
  • ~~2 Graphical interface forwarding (currently unsuccessful, will be studied later)~~
    • 2.1 Brief description of X11 graphical forwarding
    • 2.2 X11 Forwarding
      • Modern technologies related to X11
    • 2.3 Operation method
      • 2.3.1 Remote server settings
      • 2.3.2 Local operating system settings
      • 2.3.2 Experiment and verification:

1 VsCode tool

1.1 vscode remote Remote-ssh operation practice and doubts

1.1.1 vscode configuration Remote-ssh

1. Install vscode and install Remote-ssh in the plug-in;
2. Open the C:\Users\XXX.ssh\config file through the Remote-ssh setting button;
3. Modify the config file:

# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
HostMyIDC
    HostName 10.4.202.13
    Port 18923
    User root
    IdentityFile "C:\Users\XXX\.ssh\id_rsa" #Set the private key path

I found that if the private key path is set in another location, it will not be recognized and I have to enter the password. Does the private key path have to be consistent with the .ssh\config file path?
4. Connect and select Linux as the operating system.

1.1.2 Problems encountered

1.1.2.1 Setting up SSH Host “MyIDC” Downloading VS Code Server stuck waiting

Using the VSCode Remote-SSH plug-in, I want to connect to the remote server and get stuck at Setting up SSH Host “MyIDC” Downloading VS Code Server.

?The reason is that the home directory /.vscode-server/bin/f80445acd5a3da24aa209168452a3d97cc32 (a string of very long things, called commit_id here)/vscode-servlet.tar.gz under the remote server is not downloaded.

Here you can use ls -la to check the size of the file. The size is 0. Check whether the download was successful. (This is usually the reason why the download is not successful)

?Solution: Manual download, do not let the server download.
1. Manually download vscode-servlet.tar.gz:
https://update.code.visualstudio.com/commit:${commit_id}/server-linux-x64/stable (note that:commit_id should be replaced with the corresponding Commit ID, and the dollar character should also be replaced)
2. Record commit_id. Home directory/.vscode-server/bin/f80445acd5a3da24aa209168452a3d97cc32 (a very long string of things, let’s call it commit_id here).
3. Run the following commands in sequence to perform operations such as copying and decompressing:
rm ~/.vscode-server/bin/* -rf Delete everything in the bin directory
mkdir -p ~/.vscode-server/bin Create directory
cd ~/.vscode-server/bin
tar -zxf vscode-server-linux-x64.tar.gz decompress
mv vscode-server-linux-x64 commit_id move rename
4. Restart vscode.

1.1.2.2 After the VsCode update, the RemoteSSH connection is stuck at Setting up SSH Host XX:Copying VS Code Server to host with scp

The solution is the same as 1.2.1. Manually download the vscode-server-linux-x64.tar.gz file of //.vscode-server/bin/, decompress it and rename it to the ID number.

1.1.3 vscode Remote-ssh other operation skills

  • Ctrl + J opens remote terminal
  • Close the ssh connection: click the connection in the lower left corner, and click Close Remote Connection at the bottom of the pop-up option list box
  • Automatic word wrapping in the editing interface: Menu bar View–>Toggle Word Wrap option or shortcut key alt + Z

1.2 Vscode development Python program operation practices and doubts

1.2.0 debug configuration file

vscode relies on launch.json in the .vscode directory to manage your debugger program. The following is an example of this file:
One instance in “configurations” is a debugger program configuration. When you want to add multiple debugger programs (for example, you may need two different sets of parameters for debugging separately, and do not want to modify them every time), you can “Add a new configuration file as follows:

{<!-- -->
  "version": "0.2.0",
  "configurations": [
    {<!-- -->
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "justMyCode": true
    },
    {<!-- -->
      "name": "the second debugger",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "justMyCode": true
    }
  ]
}
  • name is the name of the debugger, which will be displayed in the drop-down box in the running and debugging sidebar. Select the corresponding debugger and click the green triangle to start the corresponding debugger.
  • Type sets the debugger to use. Set it to python and does not need to be modified.
  • Request is debugging mode. It can choose two types, one is launch mode and the other is attach mode:
    Launch mode: Start the debugger on the file specified by program, and VSCode starts an independent program with debug function.
    Attach mode: monitor a started program (it must have debug mode turned on). Attach (inject) the debugger to a running python process. It is used for remote debugging and can also be used to solve multi-parameter problems. Please refer to the following sections for details.
  • The absolute path of the program file provides the specific python script file that you want the debugger to run. It can be a relative path or an absolute path. For example, ${file} in the configuration file above represents the file currently activated by vscode (that is, the currently opened, And enter the file where the cursor is located), you can also specify a specific file, such as setting “program”: “${workspaceFolder}/main.py”
    This means starting the debugger on main.py in the working directory.
  • console: The type of terminal, integratedTerminal refers to using vscode terminal.
    Of course, we can also set other parameters, such as stopOnEntry: true, to pause execution when entering the program.

Other parameters reference:
1. How to debug python code in vscode, including how to elegantly pass in multiple parameters

1.2.1 Debug python code in vscode

There are two ways to debug:

  • Basic debugging (basic debugging): can solve most situations
  • Command line debugging (command line debugging): used for programs that require a large number of parameters or remote debugging

It is required to install the Python extension (extension) in vscode first. The Python extension provides python syntax, operation, environment selection and other support. It is a good assistant for writing python code in vscode.
After installing the Python extension, when you open the python script file, the environment settings will appear in the lower right corner (lower versions may be in the lower left corner) to select the python interpreter you want to use, and the run and debug options will appear in the upper right corner.

If you just want to simply debug the program and have no other needs, you can directly break the point and click debug in the upper right corner, and the program will automatically debug the currently opened python file.

If you want to customize some debug options, then:
Open the DEBUG settings of VS Code, first click on the bug (Run and Debug) in the sidebar, click Run and Debug, and after clicking, you will be prompted to select a language, select Python. Then you need to specify the corresponding file type. You can see that it supports many Python debugging methods, including Django, Flask, and even Pyramid. Since what we are going to talk about below is code debugging of a single file, we choose Python File.
Then a launch.json configuration file will be automatically generated in the root directory of the current folder/.vscode, which configures your debug configuration.

1.3 Vscode development C++ program operation practices and doubts

2 Graphical interface forwarding (currently unsuccessful, to be studied later)

The graphical interface (GUI) we can see depends on the display server (Display Server). The Display Server is a key component in any graphical user interface, especially a window system. It is the basic component of the graphical user interface (GUI), located between the graphical interface and the kernel. With the display server (Display Server), we can use the computer with the GUI. Without it we would only be able to use the command line interface (TTY).

Desktop environments (Gnome, KDE, Xfce, MATE, etc.) use the underlying display server
Because the X series protocol (X11 stands for the 11th generation version of the X protocol) has been around for a long time, there is some historical design baggage. Therefore, Linux is more secure and reasonable. After Ubuntu 21.04, the Wayland protocol is now enabled by default. But precisely because the X11 protocol has existed in the market for many years, many software are still implemented based on X11, which leads to some incompatibility issues. In the future, the Wayland protocol may still dominate.

Client applications will need to be ported to the Wayland protocol, or use a graphics toolkit with a Wayland backend (such as GTK), to be able to work natively with Wayland-based compositors and display servers.

Legacy X11 applications that cannot be ported to Wayland will automatically use Xwayland as a proxy between the X11 legacy client and the Wayland compositor. XWayland acts as both an X11 server and a Wayland client. The role of Xwayland is to convert the X11 protocol to the Wayland protocol or vice versa, allowing older X11 applications to work with Wayland based display servers.

On GNOME Shell on Wayland, Xwayland starts automatically at boot, which ensures that most X11 legacy applications work as expected when using GNOME Shell on Wayland. However, the X11 and Wayland protocols are different, so some clients that rely on X11-specific functionality may behave differently under Xwayland. For these specific clients, you can switch to the X.Org display server.

2.1 Brief description of X11 graphical forwarding

X11 forwarding principle of SSH – from the ssh manpage:
If the ForwardX11 variable is set to “yes” (or see the description of the -X and -x options below), and the user is using X11 (with the DISPLAY environment variable set), connections to X11 displays will automatically be forwarded in this form to Remote: Any X11 program started with a shell or command will connect to the real X server from the local machine through the encrypted channel. The user should not set DISPLAY manually. The X11 connection can be set up on the command line or in the configuration file forwarding.
The DISPLAY value set by ssh will point to the server, but the display number is greater than zero. This is natural, because ssh creates a “proxy” X server on the server to forward the connection through the encrypted channel.

In principle, for user login,the local host is the client (SSH Client), and the remote host is the server (SSH Server); for X11 programs, the local host is the server (X Server), The remote host is the client (X Client).
From the actual experience, the user logs in to the SSH Server of the remote host with the X11 forwarding function enabled on the local host through the SSH Client to execute the GUI graphical interface program. At this time, the display on the local host (X Server DISPLAY) A graphical interfaceof the remote host’s GUI program will be presented.

X Window System (often referred to as X11 or
X uses the C/S model (this is the key): one X server communicates with multiple applications (clients). The server receives the client’s request to draw the window and passes input from the mouse, keyboard and other devices to the client.
Therefore, the X server and client can be located on the same computer, such as when using a desktop environment such as KDE on a Linux host. The X server can also communicate with the client through a homogeneous network, a heterogeneous network or the Internet.
The communication between the X server and the client is not encrypted. This problem can be solved through SSH. SSH is the abbreviation of Secure Shell. SSH can be regarded as an encrypted and compressed version of telnet.
You need to use the forwarding function of SSH. When the computers where the X server and client are located support the SSH protocol, the insecure TCP/IP connection between the X server and the client can be forwarded to the SSH connection established between them.

After understanding the principle, we can build our own X service locally, and then the server acts as an X client and sends drawing requests to the local X server. This achieves the purpose of displaying images locally.

X server is the display server in the X Window System (referred to as X11 or

  • X client: X client. Usually various GUI applications, such as Firefox browser, xterm, xclock, etc.
  • screen: screen in logical concept. It can be one physical monitor, multiple physical monitors or VNC virtual monitors
  • There are three communication methods between X client and X server: TCP, unix socket and communication through memory. The latter two methods are limited to the case where X client and X server are both located on the same machine, and the communication efficiency is higher.
  • The DISPLAY variable is used to control “where the graphical interface is drawn and displayed.”
    The format of the DISPLAY variable value:
    (1) X client and X server are both located on the same machine and communicate through unix socket or memory
    :.
    Such as:0.0, :0.1, :1.0
    (2) X client and X server are located on different machines and communicate through TCP protocol
    : . such as 192.168.1.2:0.0, 127.0.0.1:1.0, localhost:10.0

Notice:

  • :0.0, 127.0.0.1:0.0 and localhost:0.0 are all drawing on the same machine, but :0.0 uses unix socket or memory communication, and the latter two use TCP protocol communication.
  • localhost is a special hostname that refers specifically to the machine itself. In the context of IPV4, the localhost host name will be translated into the IP address 127.0.0.1 after table lookup. Therefore, 127.0.0.1:0.0 and localhost:0.0 mean the same thing.

Example: Run the Firefox browser on the Raspberry Pi’s Linux system, and then display the browser’s graphical interface on the laptop.
Device Description:
<1>The laptop has an IP address of 192.168.31.200 and only one display screen. An X server No. 0 is running in the laptop system. The X server manages a screen number 0. The content of screen No. 0 will be displayed on the notebook display.
<2>There is also a Raspberry Pi Linux development board. The Raspberry Pi and the laptop are on the same LAN, and the Raspberry Pi is not connected to the display.
Instructions:
<1>On the laptop, log in to the Raspberry Pi system Shell through SSH, and then run the following command:

export DISPLAY=192.168.31.200:0.0 # Assign the variable DISPLAY to the value 192.168.31.200:0.0 and set it as the environment variable of the current shell
firefox # Run “Firefox” in Shell

  • Prerequisite: My laptop is running X server No. 0, which will monitor all IP addresses of the machine (including 127.0.0.1, 192.168.31.200, etc.) from 6000 + which X server No. the server is running (i.e. 6000) Drawing request on TCP port
  • The identity of Firefox browser at this moment is X client. In order to find out where it should draw and display the graphical interface, the X client first obtains the value of the DISPLAY variable in the current Shell, 192.168.31.200:0.0.
  • After the X client parses the value of DISPLAY, it will send the GUI drawing request to the 6000 + 0 (i.e. 6000) port of the 192.168.31.200 machine through the TCP protocol, requesting “display the drawing results on screen No. 0 managed by the X server” “.
  • On the 192.168.31.200 machine, the X server No. 0 responsible for monitoring port 6000 receives the request. The X server parses the drawing instructions and draws in the specified screen No. 0.

2.2 X11 Forwarding

Shell of local machine
Configure the DISPLAY environment variable of the local machine
export DISPLAY=127.0.0.1:0.0
Enable SSH connection with X11 Forwarding function
ssh -X remote machine username@remote machine IP address
After logging into the shell of the remote machine
The Shell’s DISPLAY environment variable will be automatically set to localhost:10.0 by the SSH server.


Run a GUI program in the shell of the remote machine:

  • The X client of the remote machine parses the value of the Shell environment variable DISPLAY localhost:10.0, and sends a drawing request to the X server No. 10 of the remote machine itself through the TCP protocol.
  • The SSH server on the remote machine will act as the X server No. 10 and receive this request.
  • The SSH server transmits the request to the SSH client of the local machine through an encrypted tunnel.
  • After the SSH client of the local machine receives the drawing request, it will act as an X client, parse the value of the local Shell environment variable DISPLAY 127.0.0.1:0.0, and then send the drawing request to the corresponding X server.
  • X server No. 0 of the local machine receives the drawing request and draws in the specified screen No. 0.
  • The graphical interface appears in the display corresponding to screen 0 of the local machine.

X11 related modern technologies

Xpra
https://github.com/Xpra-org/xpra
X2Go
https://wiki.x2go.org/doku.php

2.3 Operation method

2.3.1 Remote server settings

  1. Configure sshd: Set to allow X11 forwarding and enable X11 Forwarding

sudo vim /etc/ssh/sshd_config
X11Forwarding yes #Allow X11 forwarding
X11UseLocalhost no #Prohibit binding X11 forwarding requests to the local loopback address
AddressFamily inet #(optional) Force the use of IPv4 channels.
X11DisplayOffset 10 #optional

2. Restart the sshd service: systemctl restart sshd / sudo systemctl restart sshd.service
3. Install the GUI program for the experiment, using xclock, a simple clock.
CentOS system, execute: sudo yum install xclock

2.3.2 Local operating system settings

1.Linux operating system

(1) Install dependencies:
xauth is used for authorization and fonts is used for font display.
Arch system, execute:
sudo pacman -S xorg-xauth xorg-fonts-*
Other distribution package names might be xorg-x11-xauth and xorg-x11-fonts-.
Install X11 forwarding related software
yum install xorg-x11-xauth xorg-x11-fonts-
xorg-x11-font-utils xorg-x11-fonts-Type1 xclock
(2) Modify the client’s ssh settings: sudo vim /etc/ssh/ssh_config
Add the following three lines:
ForwardAgent yes
ForwardX11 yes
ForwardX11Trusted yes
(3) Restart the client’s ssh service: sudo systemctl restart ssh.service
(4) Add -X parameter to connect to the server:

xhost + //Allow the server’s x11 interface to connect
ssh -X [email protected] #-X parameter means forwarding X11 data, replace the user name tsfh and the ip address of server S with your own

2.Windows operating system
There are three commonly used solutions for X11 Server under Windows:
Xmanager, MobaXterm
Cygwin and its derivatives (such as babun) are huge software packages
Use putty + Xming

2.3.2 Experiment and verification:

The local Linux operating system (X Server) connects to the remote Linux operating system (X Client) through ssh, pay attention to the option -X
ssh -X 172.18.253.19 xclock
At this time, the xclock GUI graphical interface will be opened directly on the local X Server DISPLAY (that is, the local monitor).

Of course, you can also execute the xclock command after logging in with ssh -X 172.18.253.19, which will have the same effect.