Cant write files to /var/www/html using the VS Code editor

4.8k Views Asked by At

I am trying to save a .php file to /var/www/html/mysql using VS Code on Fedora 27 but I get an message saying

enter image description here

Also if I try to open VS Code as root I get the following message:

enter image description here

3

There are 3 best solutions below

0
On BEST ANSWER

You need to change access rights to the folder. There are 2 options.

  1. sudo chmod 777 /var/www/html/mysql - Changes access rights to the mysql directory
  2. sudo chmod -R 777 /var/www/html/mysql - Changes access rights to both the mysql directory and all subdirectories inside.
0
On

You can run Visual Studio Code as root

sudo code /directory-to-open --user-data-dir='/var/www/html/' --no-sandbox

Or

sudo code --user-data-dir="~/.vscode-root"

Running Visual Studio Code as root is not recommended, as it can be dangerous and compromise your system’s security.

Another way

you can change the permissions of /var/www/html/ to allow your regular user to save files there. so you can run VSCode as a normal user and make changes to the file without any problem.

command change the owner of the folder to your user:

sudo chown -R $USER:$USER /var/www/html/

Or

to change the permissions of the folder to allow read and write access for your user:

sudo chmod -R u+rw /var/www/html/

0
On

Even though the accepted answer actually works, I don't recommend to change access rights when using LINUX. Your "web" folder is usually owned by web server's user and group, therefore, it's a good practice to keep the same in your DEV (local) environment (i.e. your PC). It is OK to execute your IDE as sudo if you need to, as long as you handle it locally and keep the same access rights as in QA and PRODUCTION environments.