Report this

What is the reason for this report?

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

Posted on December 29, 2023
Tero

By Tero

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?



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!

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.
0

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 {} \;

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!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.