I am attempting to copy a file into a directory where my user account is not the directory owner but belongs to a group that is the directory group owner. These are the steps I have taken:
Create a group and add me to that group
stephen@pi:~ $ sudo groupadd test-group
stephen@pi:~ $ sudo usermod -a -G test-group stephen
stephen@pi:~ $ grep 'test-group' /etc/group
test-group:x:1002:stephen
Create a file and list permission
stephen@pi:~ $ touch example.txt
stephen@pi:~ $ ls -l example.txt
-rw-r--r-- 1 stephen stephen 0 Feb 9 10:46 example.txt
Create a directory, modify the group owner to the new group and alter permission to the directory to grant write permission to the group
stephen@pi:~ $ sudo mkdir /var/www/testdir
stephen@pi:~ $ sudo chown :test-group /var/www/testdir/
stephen@pi:~ $ sudo chmod 664 /var/www/testdir/
stephen@pi:~ $ sudo ls -l /var/www
total 8
drwxr-xr-x 2 root root 4096 Oct 31 12:17 html
drw-rw-r-- 2 root test-group 4096 Feb 9 10:48 testdir
Copy the newly created file into this directory
stephen@pi:~ $ cp example.txt /var/www/testdir/straight-copy.txt
cp: failed to access '/var/www/testdir/straight-copy.txt': Permission denied
To me, this should have been successful; I'm a member of the group that has ownership of this directory, and the group permission is set to rw. Ultimately, I want any files that are copied into this directory to inherit the permission of the parent directory (/var/www/testdir).
I can copy with sudo
, but this does not inherit the owner or permission from the parent directory, nor does it retain the original ownership (probably as I'm elevated to root to copy):
Copy with sudo
and list ownership/permission of file
stephen@pi:~ $ sudo cp example.txt /var/www/testdir/straight-copy.txt
stephen@pi:~ $ sudo ls -l /var/www/testdir/
total 0
-rw-r--r-- 1 root root 0 Feb 9 11:06 straight-copy.txt
Please is someone able to explain to me what is happening?