0

After researching the TMOUT variable and various recommendations I set the following in the /etc/bashrc file:

TMOUT=600
readonly TMOUT
export TMOUT

Now when a user logs in TMOUT gets set to 600, set to readonly and exported but this error occurs:

-bash: TMOUT: readonly variable
-bash: TMOUT: readonly variable

Any suggestions?

1

I guess that /etc/bashrc gets sourced more than once -- debug it by adding a

echo "TMOUT=<$TMOUT>: ${BASH_SOURCE[*]}"

line before your assignment.

AFAIR /etc/bashrc is not sourced by default, but explicitly from ~/.bashrc, and only on RHEL-like distros.

But exporting and making a variable read-only does not work as you apparently expect it to:

(readonly FOO=33; export FOO; bash -c 'FOO=47')
  # no error

Environment variables don't know about the shell's "readonly" attribute: that will only matter within the current session / script.

This means that a user can bypass your TMOUT by simply starting another shell with env -u TMOUT bash --norc. Making it read-only is only a nuisance.

2
  • This got me thinking and I saw where /etc/profile and /etc/skel/.bashrc (used when creating new user home dir's) both sourced /etc/bashrc so th at maybe where the duplicate error is coming from. – Bill R Feb 9 '20 at 20:44
  • As to the user bypassing, this server is for entry level UNIX/Linux students. If they know how to bypass the TMOUT var then they probably already know UNIX. – Bill R Feb 9 '20 at 20:54
1

Somewhere, you're trying to assign to the variable, but it's now readonly.

You can check whether the variable is readonly using declare and only assign to it if it isn't readonly:

declare -p TMOUT 2>/dev/null | grep -- '-r' >/dev/null || TMOUT=600

The redirections hide the output of declare and the error message when the variable hasn't been declared yet.

2
  • when using the above example instead of my original config it works but the TMOUT varable is not readonly. – Bill R Feb 9 '20 at 20:45
  • You should put the readonly TMOUT bellow this line. – choroba Feb 9 '20 at 21:04
0

OK - Uncle Billy's response nudged me onto the path for resolving this issue. It seems that both /etc/profile and each user's .bashrc files were both sourcing the /etc/bashrc where the TMOUT variable was being set, exported and made read only. By eliminating the extra calls for sourcing /etc/bashrc I have a resolution.

Thanks for the help choroba and Uncle Billy

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.