0

How to set bash auto logout expiration in ubuntu 20.04 ?

I,m setting the variable TMOUT for root user to 2 hours on my server using following bash-command:

sudo export TMOUT=7200

But it does not work at all, are there any other solutions to achieve this, other than putting that variable in my .profile ?

1 Answer 1

2

You could set a log out script in /etc/profile.d:

  • Create the file /etc/profile.d/autologout.sh:

    sudo nano /etc/profile.d/autologout.sh

  • The contents of autologout.sh:

    #!/bin/sh
    # Sets auto log out for all users to 2 hours
    TMOUT=7200
    readonly TMOUT
    export TMOUT
    
  • Set permissions for the script:

    sudo chmod 0755 /etc/profile.d/autologout.sh

  • Reload the profile or log out and back in.

    source /etc/profile.d/autologout.sh

5
  • 1
    looking good so far, i changed your first command as following: $ nano /etc/profile.d/autologout.sh, since i could not cat something that did not exist. Also you forgot to mention that i have to log out once it completed and log in again, so it will take effect then. Please could you update your answer accordently ? This will be good to know :). Aug 12, 2021 at 11:02
  • You should also reload profile using following command: $ source /etc/profile.d/autologout.sh, without running that command you will still get auto logout Aug 12, 2021 at 11:40
  • To finish, i had also to rename the file autologout.sh to autologout, since it will be executed by run-parts. Aug 12, 2021 at 12:22
  • I don't believe you need to rename autologout.sh to autologout,, there are other .sh files in /etc/profile.d but I made the other changes you suggested.
    – Josh Zhang
    Aug 12, 2021 at 12:54
  • As @JoshZhang introduced, i had to do followings: 1. Create the file in folder /etc/profile.d and make sure the file is run-parts-conform nano /etc/profile.d/autologout 2. Then add the commands TMOUT=7200 readonly TMOUT export TMOUT 3. Then i tested that program run-parts will execute my script properly run-parts --test /etc/profile.d 4. I verify then my setting (TMOUT-variable) echo $TMOUT 5. I then reload or update profiles without a reboot source /etc/profile.d/autologout Aug 12, 2021 at 13:37

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.