How to chown a file on a CIFS mount

chownsamba

Here are my steps:

mkdir /mnt/docs
chown www-data:www-data /mnt/docs
ls -l /mnt

Positive results:

drwxr-xr-x 2 www-data www-data 4096 Jan  6 01:14 docs
drwxr-xr-x 2 root     root     4096 Dec  8 21:46 cdrom

Now I mount the remote share successfully (I can touch files to it as root) like so

mount -t cifs //192.168.1.10/Public/Documents/Docs -o username=****,password="****" /mnt/docs
ls -l /mnt

Negative results (the owner is back to root):

drwxr-xr-x 2 root root    0 Jan 12 02:51 docs
drwxr-xr-x 2 root root 4096 Dec  8 21:46 cdrom

I then tried to chown the symlink like so (with -h)

chown -h www-data:www-data /mnt/docs
ls -l /mnt

But the results are still

drwxr-xr-x 2 root root    0 Jan 12 02:51 docs
drwxr-xr-x 2 root root 4096 Dec  8 21:46 cdrom

How can I mount the share to a local mount point as a non-root user? The goal is to let Apache be able to write to the remote share. Solutions for either Debian or CentOS systems are appreciated. Also, chmod as no effect either.


Related: Symbolic link creation failing: change ownership issue

Best Answer

I'm not sure what symlink you are referring to. What you fail to change is the owner of /mnt/docs, the mount-point of your CIFS share. The mount-point owner changed to the user who mounted something onto it (root).

Since only root is able to mount, how can you change the owner of the mount-point (and its underlying content)? With the uid and gid options (and also, if necessary, the forceuid and forcegid options).

See the man-page of mount.cifs:

uid=arg

sets the uid that will own all files or directories on the mounted filesystem when the server does not provide ownership information. It may be specified as either a username or a numeric uid. When not specified, the default is uid 0.

Therefore, your mount command should be:

mount -t cifs \
      -o username=****,password="****",uid=www-data \
      //192.168.1.10/Public/Documents/Docs \
      /mnt/docs
Related Question