I would like to disable ssh login to my system for particular users say X,and Y on specified time 17.00 to 18.00.

so how to achieve this ?

any help would be appreciated.


The answer for this is pam_time[1]

[1]: http://www.linux-pam.org/Linux-PAM-html/sag-pam_time.html "pam_time". As always, be careful editing pam and back up your config.

| improve this answer | |
  • I've append this login & ssh;*;*;Al1700-1800 in /etc/security/time.conf file is it correct ? because it didn't work. @Andy – Rahul Apr 9 '15 at 11:01
  • @arzyfex - try just restricting one service to get started. Have you actually added pam_time.so into /etc/pam.d somewhere? "it didn't work" could be better expressed with something more specific. Anything relevant in logs? Did you add "debug" to the line in pam.d? – Andy Apr 9 '15 at 13:36
  • @Andy : Yeah I've added pam_time in /etc/pam.d/sshd file. and I've also checked log but it doesn't have relevant logs that Im looking for. One thing I wanna tell u that I'm new to linux administration, so excuse me for mistakes. – Rahul Apr 9 '15 at 14:36
  • I understand, but beyond your original question this isn't a good forum to diagnose the issue in detail. I have no idea why it's not working for you, sorry. – Andy Apr 10 '15 at 11:06
  • @Andy : Thank you, I understood this very well now. – Rahul Apr 8 '16 at 11:18

Here's an idea:

In sshd_config add the following line:


This list should be all users you want to be allowed to ssh to the system. After the list is present restart the sshd service. Then add the following lines to roots crontab:

0 17 * * * /usr/bin/sed -i 's/^AllowUsers.*$/AllowUsers USER1 USER2/' && /sbin/service sshd restart
0 18 * * * /usr/bin/sed -i 's/^AllowUsers.*$/AllowUsers USER1 USER2 USER3 USER4/' && /sbin/service sshd restart

That is written for my centos 6 server, but change paths in your crontab as appropriate as well as the sshd restart command. And of course, replace the user lists with the lists you want at each time.

| improve this answer | |
  • @Paul : that works but, if same thing I want to achieve using PAM then what would be the method ? – Rahul Apr 7 '15 at 4:29
  • I only edited the answer (for readability). It is better to direct your questions to the user that provided the answer, in this case that is @Joe. – Paul Apr 7 '15 at 15:42
  • @Paul Thanks for your edit. I'm new to this site and now I know the right way to format. – Joe Apr 7 '15 at 16:08
  • @arzyfex It looks like Andy has the answer you are looking for in PAM. – Joe Apr 7 '15 at 16:09
  • @Joe : It helped me a lot in other problems, thanks for your help. – Rahul Apr 8 '16 at 11:20

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.