I recommend this VSCode extension: Save as Root in Remote SSH
Install the extension on a window opened with Remote-SSH and use “Save as Root” in the command palette instead of Ctrl+S.
Basically, it reads the contents of the editor and calls the sudo command to overwrite the file.