0

I would like to find the umask value for a particular system user, the one that is applied when a script running as this user creates a new directory. I'm using both Ubuntu 16.04 and 20.04 systems.

The closest answer I can find to this is this question. I don't understand a word of it, but I tried the script

for user in $(awk -F: '{print $1}' /etc/passwd); 
do 
    printf "%-10s" "$user" ; su -c 'umask' -l $user 2>/dev/null
done

given in the accepted answer; it printed the word "root" and then froze the system.

How do I find the umask value for a given system user?

  • 1
    It probably didn't freeze the system - likely su wrote its Password: prompt to standard output (which you discarded) and is waiting for you to respond – steeldriver May 23 '21 at 21:26
  • Good note, probably. I don't know Bash, so unless someone can correct that for me, I still can't use it. – Borea Deitz May 23 '21 at 21:38
  • In order to work properly, the script above needs to be run as root. Otherwise su asks for a password and it will hang. However, that segues into a recommendation that you don't ever run shell scripts that you don't understand, especially as the root user. – Chris May 23 '21 at 21:39
  • I don't want to run a script I don't understand, but I have a problem to solve and Linux doesn't give me much choice. I simply don't have days or weeks available to decipher this kind of incomprehensible Bash gibberish, and I can't seem to rely on "free community support!" to provide explanations when they keep posting it. – Borea Deitz May 23 '21 at 21:47
  • Have you tried running the umask command as the appropriate user? See umask --help – Jeremy Boden May 31 '21 at 16:36

1 Answers1

3

To determine the umask of a user at a given time, you can just run the umask command as that user. The script you've given in the question fetches a list of users from the /etc/passwd file and then runs the umask command on each in succession. But it has some issues, not least of which is it will hang, waiting for a password, if run as a non-root user.

A user can change their umask at any time (actually, a user doesn't even have a umask, the umask is process-specific), so you shouldn't be depending on this to be the same every time. If you need to know what umask a user has when they're running a shell script, just add the umask command into that shell script. Or just ignore the umask entirely and chmod the directory to whatever mode you want it to be.

Chris
  • 1,539
  • 1
    Thanks for the explanation! In this case, I'm looking for the umask value of a system user, so the user doesn't have a login and I can't run the umask command as that user. If I try $ sudo -u <username> umask, it returns sudo: umask: command not found. – Borea Deitz May 23 '21 at 21:51
  • @BoreaDeitz I strongly suspect you have some kind of XY problem here. You should never need to know the umask of any user- if you care about the permissions of a directory that user creates in a script, just modify the script to explicitly chmod the directory. The sudo: umask: command not found error might be a PATH thing- try entering the full path of umask. It's /usr/bin/umask on my system but you can check what it is on yours with which umask. – Chris May 23 '21 at 21:56
  • @BoreaDeitz But basically you'll probably get better results if you ask about the underlying problem you're trying to solve. – Chris May 23 '21 at 21:58
  • Also the script you've given explicitly uses a login shell for the user. Which is probably not what you want if the user doesn't even have a login. – Chris May 23 '21 at 22:02
  • 2
    On Ubuntu AFAIK there's no standalone umask program, insteadumaskis a builtin in both bash and dash (which is sh) so it's a lack-of-shell issue rather than a path issue - trysudo -u sh -c umask` – steeldriver May 23 '21 at 22:04
  • @steeldriver Yes, you're right. In that case it's easily fixed by invoking a shell explicitly or making a small program that calls the uname system call. But I still think this probably doesn't fix the underlying issue OP has. – Chris May 23 '21 at 22:14
  • @steeldriver sudo -u <username> sh -c umask is the answer precisely. Post it and I'll accept it – Borea Deitz May 23 '21 at 23:23
  • 1
    @BoreaDeitz Please do keep in mind that that gives you the system default for umask for that user, but if you assume the umask is unchangeable you might get burnt later. – Chris May 23 '21 at 23:57