[Linux Development Notes] Use sudo privileges to start gdb debugging without password during remote development of vscode

1 Introduction

We often use the remote development function of vscode during the development of Linux programs, that is, use vscode locally to connect to a remote Linux host and program code.

However, when using vscode for remote gdb debugging, the normal operation of some functional modules or the operation of calling the shell inside the program requires the use of sudo authority.

sudo

2 Configure the gdb script

2.1 Create a new gdb script

Create a new text file named gdb in any directory of the remote Linux host. I created it in the ${workspaceFolder} directory of the current project vscode workspace. Any directory is fine. For the convenience of future development, it can also be created in the current user's Created under the home directory.

2.2 Edit in gdb script

Open the file gdb just created with a text editor, and enter the following command in the file:

sudo /usr/bin/gdb "$@"

"/usr/bin/gdb" in this command is the actual path of the gdb debugger on the remote Linux host, save and close after editing.

2.3 Change the permissions of the gdb script

Use the chmod command to modify the permissions of the gdb script file to be executable by the current user. I simply changed the permissions to 777 here.

chmod 777 gdb

3 Configure launch.json

Open launch.json in .vscode under the current workspace of vscode, and add the following configuration:

"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "${workspaceFolder}/gdb", // gdb脚本的路径

4 Configure sudo permission as password-free

Go to the /etc/sudoers.d directory,

cd /etc/sudoers.d

Create a new text file named "sudo_gdb_for_vscode" (file name is arbitrary) in the sudoers.d directory.

sudo touch sudo_gdb_for_vscode

Use sudo permission to open sudo_gdb_for_vscode for editing, and write the following configuration in the file:

%admin ALL=(ALL) NOPASSWD:ALL

Save the file and exit.

5 tests

5.1 Test sudo password-free

After the configuration is complete, open a new terminal. At this time, use the sudo authority to execute any command. If you are not prompted to enter the administrator password, it means that the configuration of the sudo authority is successful.

5.2 Test vscode remote debugging

Go back to the local vscode for remote debugging. At this time, you can find that the functional modules in the program that need to use sudo permissions or the shell operations that use sudo permissions inside the program can run normally in the remote Linux host with sudo permissions.

vscode


This article was originally published on the public account Linux Future Engineer .

Guess you like

Origin blog.csdn.net/qq_37354286/article/details/130208879