How to set bash auto logout expiration in ubuntu 20.04

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 ?


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