Based on Ubuntu 18.04,

I want to allow users to access only from 9 a.m. to 5 p.m.(working time) on the SSH server. (used OpenSSH-server package)

I've looked up all the materials I think will help you from user management to PAM settings on Google, but I don't think there's any information to restrict access by SSH time zone.

(But, I have not to use firewall-related services like ufw.)

If you can help me, please let me know. Thank you.

(Please understand that my English can be unnatural because I am not good at English.)

  • Do you just want to prevent users from connecting during those hours, or do you want to disconnect already-connected users? Sep 27, 2021 at 12:26
  • @steeldriver I just wanted to prevent users from connecting to SSH service from 5 p.m. to 9 a.m. (not working time). Literally. Sep 28, 2021 at 4:14

2 Answers 2


The pam_time module is designed for this.

  1. Add pam_time as the first account statement in the ssh pam config. This command will insert the statement account required pam_time.so just above the line @include common-account.
sed -i -e '/^.include common-account/i account required pam_time.so' /etc/pam.d/sshd
  1. configure /etc/security/time.conf as desired. This command will add the configuration sshd;*;*;Wk0900-1700. This limits ssh connections for all users to weekdays between 9am and 5pm.
echo 'sshd;*;*;Wk0900-1700' >> /etc/security/time.conf

See Also

  • Oh, thanks! I learned a new thing. (I'll try that, too!) Sep 29, 2021 at 6:12

Use a cron job to stop and start the ssh server at specific times. For example create a file /etc/cron.d/start-stop-ssh with the following contents:

0 9 * * * root /usr/sbin/service ssh start
0 17 * * * root /usr/sbin/service ssh stop

This will start the ssh service everyday at 9:00 and stop it everyday at 17:00.

Please note that when the service is stopped nobody (even admin) will be able to login via ssh.

  • Thank you. I'll try that method. Sep 28, 2021 at 4:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .