Hosting & Domaining Forum

Hosting Discussion => Hosting Security and Technology => Systems Management Requests => Topic started by: peterwiter on Jan 19, 2023, 03:59 AM

Title: VPS File Write permissions
Post by: peterwiter on Jan 19, 2023, 03:59 AM
When I first tried a VPS, I used Centos as the OS and KVM for virtualization. Although I had little experience, I managed to make basic settings and move several sites that were previously on a virtual host.

On virtual hosting, directories had chmod 755 and files could be uploaded without issues. However, after transferring the files to the VPS, they could only be uploaded to specific directories with 777 rights installed, which is problematic. Additionally, files that should be in the root of the site, such as sitemap.xml, cannot be overwritten or created.

What can be done to solve this issue? Is it possible to maintain 755 rights while also allowing the site owner to write files, including in the root directory?
Title: Re: File Write permissions
Post by: SerenMckay on Jan 19, 2023, 04:32 AM
To resolve the issue, the chown command can be used. This involves registering the owner of the directory and all of its subdirectories as the user who launches ftp (chown -R user:group).
Title: Re: File Write permissions
Post by: scottpolls on Jan 19, 2023, 05:39 AM
SSH can be used to make bulk changes to permissions on files and folders. The command "chmod -R xхx file" can be used, with "-r" indicating that changes should be made recursively to all folders and files in the specified folder. "xхx" represents the permission set for accessing the file/folder (such as 0555 or 0777). To make changes to an entire domain, the topmost folder like "/published/" or "/www/" should be specified.

Examples include setting permissions to 644 for a single file, all files and folders in a directory, or only for one folder. Permissions can also be set for directories or only on files using the commands "find /path/to/dir -type f -exec chmod 0644 {} \;" or "find /path/to/dir -type d -exec chmod 0755 {} \;". Additionally, ownership of chmod files and folders can be changed via SSH with nesting by using the "chown -R User:Group /path_to_directory/" command.
Title: Re: File Write permissions
Post by: rafiazafar on Apr 03, 2023, 04:56 AM
Access attributes for files and folders include the right to read, edit and write, and execute (launch) a script. However, on shared hosting, the execute right only applies to folders and CGI scripts - for normal files, this right will not be applicable.

For folders, the read right allows users to list contents, the write right allows creation and deletion of files, and the execute right allows navigation to the folder. Access rights can be recorded alphabetically or numerically, with corresponding sequences between the two types of notation.

Each file and folder has three categories of users - the owner, the user group that the owner belongs to, and all other users. Numeric entries indicate the rights of the owner, group, and all others, while alphabetical entries do the same in groups of three characters (owner, group, all others). Examples of access rights include 755 (full access for owner, read and execute for others) and 644 (write and read only for owner, read only for others).
Title: Re: VPS File Write permissions
Post by: NoelJones on Sep 04, 2023, 11:07 AM
To solve the issue you described, you can take several steps to ensure both security and flexibility in file permissions on your VPS. Here's a possible solution:

1. Create a separate group for web server files: You can create a new group, such as "www-data" or "apache," and assign it to all directories and files that need to be accessed by the web server.

2. Set directory permissions to 750: Change the permissions of directories to 750 (drwxr-x---), which grants read and execute permissions to the owner and group but restricts access for others.

3. Set file permissions to 640: Change the permissions of files to 640 (-rw-r-----), allowing the owner and group to read and write, while restricting access for others.

4. Assign the appropriate ownership: Set the owner of the directories and files to the website owner's user account, and assign the group to the "www-data" or "apache" group you created.

5. Enable suPHP or suexec: If available, consider enabling suPHP or suexec on your web server. These tools allow scripts to run with the user's own permissions, ensuring that PHP scripts, for example, are executed as the website owner rather than the web server user

6. Set the "umask" value: The "umask" value determines the default permission settings for newly created files and directories. You can set it to 022 in the user's profile or in the web server configuration, which will result in files having permissions of 644 (-rw-r--r--) and directories having permissions of 755 (drwxr-xr-x). This allows the website owner to write to their files, while still maintaining security.

7. Consider using ACLs: Access Control Lists (ACLs) enable fine-grained control over file permissions, allowing you to grant specific permissions to individual users or groups. ACLs can provide more flexibility when dealing with complex permission requirements. Check if your file system supports ACLs and consider using them if necessary.

8. Verify file ownership and group: Ensure that the files and directories are owned by the correct owner and group. You can use the "chown" command to change ownership if needed.

9. Review your web server configuration: Double-check your web server configuration file(s) to ensure that they are set up correctly and have the appropriate permissions. Make sure the user running the web server process has sufficient permissions to access the files.

10. Consider using a separate data directory: Instead of storing files directly in the root directory of the site, create a separate directory (e.g., "data" or "uploads") where the site owner can upload and modify files. Set the permissions of this directory to 777 (drwxrwxrwx) to allow read, write, and execute access for all users. This way, the site owner can freely upload and manage their files without compromising the security of other system files.

11. Implement file upload restrictions: If appropriate for your use case, you can implement restrictions on file uploads through PHP or other scripting languages. For example, you can limit file types, file sizes, and perform security checks to prevent malicious uploads.

12. Use a file ownership management tool: Consider using tools like "setfacl" or "chmod +s" to manage file ownership and permissions. These tools can help you grant specific permissions to users or groups, even allowing non-root users to modify files while maintaining security.

13. Regularly monitor and audit file permissions: Regularly review and audit the file permissions on your VPS to identify any changes or potential security vulnerabilities. This helps ensure that the correct permissions are maintained and prevents unauthorized access to sensitive files.

14. Use a file or directory watcher: You can set up a file or directory watcher that monitors specific directories or files for changes. When a file is uploaded or modified, the watcher triggers a script that changes the permissions of the file to allow the web server to access it, without compromising the security of other files.

15. Implement a secure file transfer protocol: Instead of using insecure file transfer methods like FTP, consider using a secure file transfer protocol like SFTP or SCP. These protocols provide encrypted and secure file transfer, ensuring that files can be uploaded to the VPS without the need for less secure file permissions.

16. Use a content management system (CMS): If you're managing websites, consider using a CMS like WordPress or Joomla. These CMS platforms provide built-in mechanisms for file uploads and management, handling the file permissions internally and ensuring proper security measures are in place.

17. Configure SELinux or AppArmor: Security-Enhanced Linux (SELinux) or AppArmor can provide an additional layer of security by implementing mandatory access control policies. These tools allow you to define fine-grained permissions for various processes, including the web server, and can help mitigate security risks associated with specific file permissions.

18. Engage with your VPS provider's support: If you're still experiencing difficulties with file permissions, consider reaching out to your VPS provider's support team. They may be able to provide guidance specific to their platform or assist in troubleshooting the issue.

19. Use Access Control Lists (ACLs): ACLs provide more granular control over file permissions by allowing you to set permissions for specific users or groups. You can use the "setfacl" command to define ACL rules for files and directories, enabling the site owner to have write access while maintaining secure permissions.

20. Configure file and folder ownership inheritance: Set up file and folder ownership inheritance so that new files and folders inherit the owner and group of their parent directory. This ensures that files uploaded by the site owner maintain the correct ownership, eliminating the need for manual ownership adjustments.

21. Explore alternative file transfer methods: If the current method of file transfer is causing file permission issues, consider using alternative methods like rsync or SCP, which preserve file permissions during the transfer process.

22. Review web application configuration: Check if there are any specific configurations within your web application that might be overriding file permissions. For example, some CMS platforms have settings that limit the ability to overwrite or create certain files.

23. Regularly update and patch your server: Keeping your server's operating system, web server software, and other components up to date can help address any security vulnerabilities related to file permissions. Regular updates often include important security fixes.

24. Implement a robust backup strategy: Having a reliable backup strategy in place allows you to recover files and restore correct permissions if any issues arise. Regularly backup your files and test the restore process to ensure the backups are working effectively.