I am transitioning servers away from port 22 in an existing multiuser environment. I have configured sshd to listen on two ports: 22 and the new port.
Now I would like to detect when a user connects or is connected to port 22. This is apparently harder than I anticipated.
I tried cranking up the logging in sshd_config but even DEBUG doesn't record the port number.
I am currently scanning netstat's output for TCP connections to port 22 but that lists a hell of a lot of false positives from random bot scanners. [the reason for the port move]
Using
lsof -n -i TCP:22 -a -c sshd -a -u ^root,^sshd
you can get a list ofsshd
processes and user names with their sockets on port 22. It is skipping those owned byroot
orsshd
because they do not correspond to logged in users.A completely different approach would be to add some commands to
/etc/ssh/sshrc
, which will parse$SSH_CONNECTION
and log it:logger -p auth.notice -t "sshd[$$]" "$SSH_CONNECTION"
A third approach is to create a second instance of
sshd
for port 22 and configure it to log to a different facility.Try something like this:
To clarify, the objective of this command is to match the
lsof
hits (which show the port used) with the sessions fromwho
withpts
(extracting the PIDs from the output) in order to filter out the false positives.In other words, it shows the connections from the processes with the PIDs holding a pts ssh session.