Different ways to restrict logins on the linux.

01.

tcp_wrappers

TCP Wrappers can be used only for services that support the tcp libraries and can only be used for any 'new' connections, but not existing ones.

-Install
[root@rhce1 ~]# yum install tcp_wrappers -y

#Check if the service/daemon supports the tcp wrappers.

[root@rhce1 ~]# ldd /usr/sbin/sshd | grep libwrap
  libwrap.so.0 => /lib64/libwrap.so.0 (0x00007fb65a254000)

[root@rhce1 ~]# strings -f /usr/sbin/* | grep hosts_access
/usr/sbin/auditd: hosts_access
/usr/sbin/sshd: hosts_access

#Apply (Syntax: daemon_list : client_list [ : shell_command ])

[root@rhce1 etc]# tail -2 /etc/hosts.allow
sshd :  192.168.122.0/24

[root@rhce1 etc]# tail -2 /etc/hosts.deny
sshd : ALL
sshd : dickens

[root@rhce6 ~]# tail -1 /etc/hosts.deny
ALL : ALL : spawn ( /bin/echo %a from %h attempted to access %d >> /var/log/connections.log)

[root@rhce6 ~]# tail -1 /var/log/connections.log
192.168.122.1 from 192.168.122.1 attempted to access sshd

-Verify via tcpdmpatch
[root@rhce1 exports.d]# grep rpc /etc/hosts.deny
rpcbind,rpc.mountd :  192.168.122.20

[root@rhce1 exports.d]# tcpdmatch rpcbind 192.168.122.20
client:   address  192.168.122.20
server:   process  rpcbind
access:   denied
[root@rhce1 exports.d]# tcpdmatch rpcbind 192.168.122.30
client:   address  192.168.122.30
server:   process  rpcbind
access:   granted

02.

firewalld

Useful to restrict access to the entire service and can be scoped with subnets/networks.

firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.122.0/24" invert="True" service name="ssh" 
log prefix="Reject net 192.168.122.0/24" level="info"  reject '

03.

PAM (Pluggable Access Module)

This has no effect on root users ability to login, although the access can be restricted for individual users.

Use /var/run/nologin or /etc/nologin.

[root@rhce6 txts]# echo " Access to this system temporarily disabled " > /etc/nologin

root@panchajanya:~\> ssh kannan@rhce6
kannan@rhce6's password:
 Access to this system temporarily disabled

#Add below line to /etc/pam.d/sshd

[root@rhce1 pam.d]# grep deny sshd
auth  required  pam_listfile.so item=user sense=deny file=/etc/ssh.deny onerr=fail

[root@rhce1 pam.d]# cat /etc/ssh.deny
dickens

04.

sshd_config

This configuration can be used to restrict only 'ssh' access against a group or a list of users.

Deny via sshd_config ( Refer AllowGroups, DenyGroups as well)

#Deny users
[root@rhce1 etc]# tail -1 /etc/ssh/sshd_config
DenyUsers dickens 

#Additional Reference: Restricting password auth vs non password auth based on CIDR/User

PasswordAuthentication no

Match Address 192.168.0.0/16
        PasswordAuthentication yes

Match User kuttu
        PasswordAuthentication yes


05.

/sbin/nologin and /etc/nologin

/etc/nologin can be used to restrict any new logins against any users. ssh access will be denied for any new connections coming in vs. the /sbin/nologin can be used to restrict individual user logins.


--Disable login for all users ..
[root@rhce4 ~]# cat /etc/nologin
LOgins temporarily disabled for maintanence

[root@rhce1 etc]# chsh -s /bin/bash dickens
Changing shell for dickens.
Shell changed.

06.

/etc/securetty

Prevent root login only via consoles (not remote ssh). A blank /etc/securetty file does not prevent the root user from logging in remotely using the OpenSSH suite of tools because the console is not opened until after authentication.

echo > /etc/securetty 

07.

Kick off a logged in user/session

Use write, kill and xargs to send message and kill the session.


#Identify the terminal via w or who

--Commands to find the terminals

[root@lab125a ~]# tty
/dev/ttyS0

--Run who -a
[root@lab125a ~]# who -a
           system boot  2017-09-22 09:58
LOGIN      tty1         2017-09-22 09:58               637 id=tty1
root     + ttyS0        2017-09-22 10:03   .           638
           run-level 3  2017-09-22 09:58
root     + pts/0        2017-09-22 10:00 00:01        2527 (192.168.125.1)

--Sent message to terminal
#console terminal
[root@lab125a ~]# echo "test" > /dev/ttyS0   
[root@lab125a ~]# echo "You will be kicked out soon " | write root pts/0

#pts terminal
[root@lab125a ~]# tty
/dev/pts/0

[root@lab125a ~]# echo "test" > /dev/pts/0
test

#Use killall or kill
killall -u kuttu -HUP

#Kill a specific session.
kill