0

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
slm
  • 363,520
  • 117
  • 767
  • 871
Tak
  • 519
  • 4
  • 11
  • 23

1 Answers1

7

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
1000

Just the GID:

$ id -g
1000

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
slm
  • 363,520
  • 117
  • 767
  • 871
muru
  • 69,900
  • 13
  • 192
  • 292
  • 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 08: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 08:09
  • Can you advise how this script can be written and called? – Tak Jul 23 '18 at 08:20
  • 1
    Which OS are you running? – muru Jul 23 '18 at 08:21
  • I'm running on Ubuntu 16.04 – Tak Jul 23 '18 at 08:31
  • I've posted a question here https://unix.stackexchange.com/questions/457921/mount-using-fstab-instead-of-terminal if you can check please? as the provided answer didn't mention how to autotomatically fetch the uid and gid and you said it can be done using a script but I don't know how, so if you could please help? – Tak Jul 23 '18 at 11:42