I'm receiving permission denied errors when transferring files owned by the root user from my Amazon Lightsail instance. How do I resolve this?
Short description
To transfer files owned by the root user from your Lightsail instance, first copy the files to the home directory of the current user (for example, bitnami user). Then, change the ownership of the files and give the new owner permission to copy the files. By using this method, the permissions of the original file remain the same because you're copying a copy of the file.
Note: To copy the files, the user must be able to assume root permission or be in the sudoers file.
Resolution
Note: The example in the Resolution section uses the following directory:
/tmp/systemd-private-a244e4fc08504ae39236f514bdffecc1-chrony.service-AFB67v
The example file has the following permissions:
drwx------ 3 root root 4096 Aug 17 04:24 systemd-private-a244e4fc08504ae39236f514bdffecc1-chrony.service-AFB67v
1. Connect to the Lightsail instance using SSH as your normal user. In this example, the user is bitnami.
2. Run the cp command to copy the file to the home directory of the user.
sudo cp -R systemd-private-a244e4fc08504ae39236f514bdffecc1-chrony.service-AFB67v/ /home/bitnami/
# ls -al /home/bitnami/systemd-private-a244e4fc08504ae39236f514bdffecc1-chrony.service-AFB67v/
total 12
drwx------ 3 root root 4096 Aug 17 05:02 .
drwxr-xr-x 4 bitnami bitnami 4096 Aug 17 05:02 ..
drwxr-xr-t 2 root root 4096 Aug 17 05:02 tmp
Note: You can run the cp command using sudo. Or, you can change to root user, and then run the command. Use the following command to change to root user:
sudo -i
3. Change the ownership of the copied file so that the user can own the directory or files:
sudo chown bitnami:bitnami /home/bitnami/systemd-private-a244e4fc08504ae39236f514bdffecc1-chrony.service-AFB67v/ -R
4. Transfer the copied directory or file. The following example transfers the file using Filezilla:
Status: Directory listing of "/home/bitnami" successful
Status: Using username "bitnami".
Status: Connected to 3.xx.xx.1x2
Status: Starting download of /home/bitnami/systemd-private-a244e4fc08504ae39236f514bdffecc1-chrony.service-AFB67v/tmp/testing.txt
Status: File transfer successful, transferred 230,756 bytes in 1 second