I'm using PHP script to connect to ssh server. It executes one command and exit. Connection is made to non interactive and non login shell. Because of that it does not load .bashrc or .bash_profile files so I can't set proper umask there. I googled some information on ssh inner workings and it seems that I can use ~/.ssh/rc file to execute some commands after ssh connection. My ~/.ssh/rc on server I am connecting:


echo "Setting umask to 002"
umask 002
echo "Umask set"

The problem is that my umask is not set properly. When I run my script and it run umask command on server, I get 0022 umask :

Run: cd /local/web/ && umask
> Setting umask to 002
> Umask set
> 0022

Why isn't it working?

  • Surely you mean ~/.ssh/rc not ~/.ss/rc, if not then that may be part of your issue. Otherwise here is an answer on ServerFault.
    – DanSut
    Oct 16, 2015 at 10:27
  • Yes, it was typo, I fixed it. Your link with pam_umask is probably correct solution. But I'm curious why umask does not work inside my server ~/.ssh/rc file since echo commands are executed.
    – piotrekkr
    Oct 16, 2015 at 10:54
  • 1
    Check the man page for sshd - in the SSHRC section it specifically states It must not produce any output on stdout; stderr must be used instead. maybe that is your issue.
    – DanSut
    Oct 16, 2015 at 11:05
  • Here's Stack Overflow Q where it is commented that the SSHRC method does not work - although no reason why not.
    – DanSut
    Oct 16, 2015 at 11:08
  • I also tried putting only umask 002 &>/dev/null inside ~/.ssh/rc but it does not work either, like in SO question from your comment above. I'll try use pam_umask or some server script to fix permissions. Thanks for help.
    – piotrekkr
    Oct 16, 2015 at 11:23

1 Answer 1


umask only works if it is run by a builtin command.

Do not call the script by name but rather call . script to let it be executed by the current shell process.

If you however are confused by the value printed as 0022instead of 022, there os no difference, these values are identical.

Try the POSIX umask -S to get a better understandable output.

  • I'm not really sure what you telling me. Should I create another script beside ~/.ssh/rc and source it inside? How is it different from just calling umask directly inside ~/.ssh/rc on ssh server? This file is executed by ssh on connect. echo commands are executed then why not umask?
    – piotrekkr
    Oct 16, 2015 at 10:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .