Question

Changing Group/Owner prevents a sudo user to SFTP to a directory in www

I have followed all your tutorials to the letter and I now have a working WordPress installation. The problem came with the last tutorial: https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-on-ubuntu-22-04-with-a-lamp-stack

sudo chown -R www-data:www-data /var/www/mysitefolder

after doing this I cannot access the ‘mysitefolder’ directory via SFTP anymore.

What would be the correct and secure way to fix this?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
December 29, 2023
Accepted Answer

Heya @tedt,

To resolve this issue while maintaining security, you can add your SFTP user to the www-data group and adjust the directory permissions so that group members can read, write, and execute files in the WordPress directory. Here’s how to do it:

1. Add Your User to the www-data Group

Replace yourusername with your actual SFTP/SSH username.

sudo usermod -aG www-data yourusername

2. Change Directory Permissions

You’ll want to ensure that the group (which now includes your user) has the appropriate permissions. The following command sets the correct permissions, allowing the group to read, write, and execute:

sudo chmod -R 775 /var/www/mysitefolder

3. Set Group ID on the Directories

Setting the group ID ensures that new files created in the directory inherit the same group as the directory itself, which is important for maintaining access:

find /var/www/mysitefolder -type d -exec chmod g+s {} \;
alexdo
Site Moderator
Site Moderator badge
December 29, 2023

Heya, @tedt

I would assume that you’re using a different user to manage the files via SFTP. You can simply add the user to the group or use a user who is already added to the group.

To add an existing user to the www user group, you can use the usermod command.

sudo usermod -a -G www [username]

Replace [username] with the name of the user you want to add to the group.

Once you have executed the command, the user will be added to the www group and will have the appropriate permissions to access files in the /var/www directory.

If the root user is not disabled you can also use it for SFTP.

Happy holidays!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel