I'm using the below command to mount CIFS share, but I want to fetch the UID and GID automatically in the same command so that I don't have to enter it manually every time I mount on different machines. Any advice how?

$ sudo mount -t cifs -o username=${USER},password=${PASSWORD},uid=<user>,gid=<group> \
   //server-address/folder /mount/path/on/ubuntu

You can use the id command:

$ id
uid=1000(muru) gid=1000(muru) groups=1000(muru),4(adm),24(cdrom),27(sudo)...

Just the UID:

$ id -u

Just the GID:

$ id -g

So, using command substitution:

$ sudo mount -t cifs -o "username=${USER},password=${PASSWORD},uid=$(id -u),gid=$(id -g)" \
   //server-address/folder /mount/path/on/ubuntu
  • Could you please advise how I can write the same command in /etc/fstab so that it's mounted automatically? – Tak Jul 23 '18 at 8:05
  • 1
    @Tak that won't be possible in fstab, since you cannot use commands or variables in it. Writing a script to be run at startup might be better. – muru Jul 23 '18 at 8:09
  • Can you advise how this script can be written and called? – Tak Jul 23 '18 at 8:20
  • 1
    Which OS are you running? – muru Jul 23 '18 at 8:21
  • I'm running on Ubuntu 16.04 – Tak Jul 23 '18 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.