Skip to content
Advertisement

How can i save automatically the SSH_CLIENT at login?

i want to save the user’s IP when he connects to it’s home folder, this is because i’m a user in a server where my team has a folder where our public_html is located, but we use the same account, so i just want to register who connected.

So i want to make a script that triggers when a connection is made and save the user’s IP into a hidden file.

But i don’t know if i could leave running a script in background to do it, and How?

Advertisement

Answer

If you’re a root on that machine, you can simply check the auth log / messages / journal / … (depends on the distribution). By default sshd logs all you need already.

If you’re not a root, then you’ll have to keep in mind this will never be secure. You can do this in the user’s bash profile, but:

  1. Since it’s running as the same user, whoever logs in can just change the file (you can’t hide it)
  2. Anyone can workaround the script by executing some other command instead of the shell (for example ssh user@host /some/command will not be logged)
  3. It’s not secret.

If that’s ok with you, then you just need to add this to bashrc

echo "new connection at $(date) from ${SSH_CLIENT}" >> ~/your_connection_log
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement