# How to monitor ssh remote commands

**URL:** <https://forums.rockylinux.org/t/how-to-monitor-ssh-remote-commands/8760>\
**Category:** Rocky Linux Help & Support\
**Created:** [February 2, 2023, 9:25am UTC](https://forums.rockylinux.org/t/how-to-monitor-ssh-remote-commands/8760 "2023-02-02T09:25:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghooton](https://avatars.discourse-cdn.com/v4/letter/g/ed8c4c/32.png) [@ghooton](https://forums.rockylinux.org/u/ghooton)\
**Post date:** [February 2, 2023, 9:25am UTC](https://forums.rockylinux.org/t/how-to-monitor-ssh-remote-commands/8760/1 "2023-02-02T09:25:48Z")

</div>

Hi All,  
I know that ssh connections on the serve are logged in /var/log/secure  
However, where can I find a record of single commands that are executed before disconnecting.  
Example :  
ssh 192.168.1.1 uname -r

---

<div class="post-metadata">

**Author:** ![AJCxZ0](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.rockylinux.org/ajcxz0/32/2289_2.png) [@AJCxZ0](https://forums.rockylinux.org/u/AJCxZ0)\
**Post date:** [February 8, 2023, 7:18pm UTC](https://forums.rockylinux.org/t/how-to-monitor-ssh-remote-commands/8760/2 "2023-02-08T19:18:29Z")

</div>

Nowhere.

The fact that the user got a shell by connecting and authenticating with ssh is incidental to what the user did once logged in and running that shell with the ability to do whatever that user is authorised to do on the host.  
Since you are already heading down the rabbit hole which is wanting to know what the users whom you trust (but don’t trust) are doing on your host and are already engineering clever solutions to capture and log things via shell profile and rc configs, don’t bother. By the time you realise that it’s almost impossible to reliably capture what you think is the simple series of commands which would look nice in a log file, you might as well have used the tool for the real job: auditing.

Setting up auditing to capture the things you (should) want isn’t terribly difficult, but is far from trivial, however the real question - aside from the storage and transport considerations\* - is who or what is going to analyse that audit data. If the answer is your SIEM and the Security team, then go ahead; else if it’s you one day after something bad happens, then you need to work out in advance how to manage the audit data consistent with the badness\*\*, but if you think that you or some junior sysadmin’s job will now include “Review audit data for… um… things”, then don’t bother.

There are some other approaches to more specific cases which might be better suited to your environment, but this sounds like the ages-old problem of users with a shell on the host. Of course if those users don’t need a (unrestricted) shell on the host, then that’s a different matter.

\* [Get it wrong and your host will run out of space and shut down shortly after booting before you can determine why.]  
\*\* [e.g. Did the user delete the file versus call in an air strike on the data center through an unsecured API.]

---

<div class="post-metadata">

**Author:** ![blank](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@blank](https://forums.rockylinux.org/u/blank)\
**Post date:** [February 10, 2023, 12:13pm UTC](https://forums.rockylinux.org/t/how-to-monitor-ssh-remote-commands/8760/3 "2023-02-10T12:13:35Z")

</div>

Unfortunately there are still (!) no perfect open source solutions for this. It’s just a hard problem when user has a shell.  
pam\_tty\_audit and keystrokes from audit log is one clumsy solution.  
tlog is not much better and has issues with scp and many other problems.

Maybe those options could work for you but they are far from perfect. A bastion host dedicated for session recording would probably work better.

---

<div class="post-metadata">

**Author:** ![gustav](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.rockylinux.org/gustav/32/1381_2.png) [@gustav](https://forums.rockylinux.org/u/gustav)\
**Post date:** [February 14, 2023, 11:58am UTC](https://forums.rockylinux.org/t/how-to-monitor-ssh-remote-commands/8760/4 "2023-02-14T11:58:13Z")

</div>

I have been using snoopy, can be found on github.

---

<div class="post-metadata">

**Author:** ![bcdmshade](https://avatars.discourse-cdn.com/v4/letter/b/858c86/32.png) [@bcdmshade](https://forums.rockylinux.org/u/bcdmshade)\
**Post date:** [February 14, 2023, 5:31pm UTC](https://forums.rockylinux.org/t/how-to-monitor-ssh-remote-commands/8760/5 "2023-02-14T17:31:08Z")

</div>

You can also switch from the standard shell (sh, bash, etc) to a logging shell, like sudosh.

```auto
joeblow@server1:~$ ssh server2 "/sbin/ifconfig"

root@server2:~# sudosh-replay
...
02/14/2023 12:20:08 -c arg joeblow joeblow /sbin/ifconfig

```

---

<div class="post-metadata">

**Author:** ![zaffiro](https://avatars.discourse-cdn.com/v4/letter/z/0ea827/32.png) [@zaffiro](https://forums.rockylinux.org/u/zaffiro)\
**Post date:** [February 15, 2023, 4:50am UTC](https://forums.rockylinux.org/t/how-to-monitor-ssh-remote-commands/8760/6 "2023-02-15T04:50:49Z")

</div>

There is an effective way to do this, using built-in feature and command on Linux:

```auto
PROMPT_COMMAND='history -a >(tee -a ~/.bash_history | logger -t "$USER[$$] ($SSH_CONNECTION) bash")'

```

Bash will read the PROMPT\_COMMAND variable every time before it displays the prompt, so we will insert a command for auditing purposes there.

---

<div class="post-metadata">

**Author:** ![brian](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.rockylinux.org/brian/32/5777_2.png) [@brian](https://forums.rockylinux.org/u/brian)\
**Post date:** [August 25, 2023, 3:25am UTC](https://forums.rockylinux.org/t/how-to-monitor-ssh-remote-commands/8760/7 "2023-08-25T03:25:02Z")

</div>

Closed on August 25, 2023, 3:25am UTC


