Search code examples
google-cloud-platformvirtual-machinestartupstartupscript

GCP linux startup script to grant all users permission to directory and files


I am attempting to run the following startup script on a GCP vm instance:

mkdir /home/example_user/
mkdir /home/example_user/test
echo "hello world" >> /home/example_user/test/.env
chmod 777 /home/example_user/

When I login to the created vm, and the startup script has finished running. I try to confirm the permissions were set properly via:

nano /home/example_user/test/.env

Permission denied

I would like that an user that logins via ssh does not need to prefix sudo with there commands, or enter root via sudo su prior to

nano /home/example_user/test/.env

I have tried

chmod 777 /home/example_user/
chmod a+x /home/example_user/
chmod -R a+x /home/example_user/ 

How do I properly give access permissions to all users on the vm instance within the startup script?


Solution

  • Change permissions of the folder and its children recursively:

    chmod -R 777 /home/example_user/
    

    However, there are better strategies. Linux implements the concept of groups. Add the users that should have access to a shared folder to the same group. Then set the folder's group and the permissions for that group.

    How to Manage Users with Groups in Linux