In my .bashrc there are a couple commands which redirect standard error to /dev/null, and this is not allowed within rbash
:
bash: /dev/null: restricted: cannot redirect output
Is there some way to get around this (short of modifying Bash)? That is, to either
- discard standard error some other way or
- only attempt to redirect if it's allowed.
Best Answer
You can close stderr as in:
While that will work in most cases, it may cause problems as poorly written applications may end up opening a new file which would automatically get
2
as the file descriptor and could end up writing error messages where you wouldn't want them to.That also means that any write to stderr done by the application would return with an error (EBADF) which may affect their behavior.
As pipes are allowed, you could provide with a command that discards all its input. Here, using
grep -v '^'
(but you could usetail -n 0
orsed d
or a script calleddiscard
that doescat > /dev/null
):Alternatively, you could have whatever starts that restricted shell start it with the fd 3 (for instance) redirected to
/dev/null
(rbash 3> /dev/null
), so you can then do within the restricted shell:Which is allowed:
You can check whether redirection to
/dev/null
is allowed/works or not with:You can check whether the shell is restricted or not with: