How to Set Automatic Logout Timeout for Command-Line in Ubuntu?

I am using Ubuntu, and I want to automatically log out users from the terminal if they stay inactive for some time. I know this can improve server security. The timeout should be set for all users, and I want it to be around 5 minutes (300 seconds).

How can I configure this command-line timeout for Ubuntu?

Open the file /etc/profile to add the timeout setting.

sudo vi /etc/profile

Add the following line at the end of the file to set the timeout to 300 seconds (5 minutes)

TMOUT=300
readonly TMOUT
export TMOUT

You can change 300 to any value you prefer.

Save the file and exit. Now logout and login again to apply the changes.
You should now auto logout after 5min