How can you log every command someone has entered into the shell?

I'm asking on both the basis of protecting yourself if you are logged into someone else's server and something breaks, or if someone else is logged into your server (either intentionally or maliciously).

Even a novice can bypass history with unset history or create a new shell to hide their tracks.

I'm curious how the senior Linux admins track what commands have been entered or what changes made to the system.

  • Another way to bypass bash history is to put leading space before command.
    – EsmaeelE
    Feb 26, 2022 at 8:45

2 Answers 2


Check out auditd. If you add

-a exit,always -F arch=b64 -S execve
-a exit,always -F arch=b32 -S execve

to /etc/audit/audit.rules every executed commands will be logged. See: https://whmcr.com/2011/10/14/auditd-logging-all-commands/

Then send it to a syslog server.

  • Surely execve is not the only syscall you need to log. Why not execvp as well? Or execl? etc.
    – 2rs2ts
    May 10, 2016 at 20:05
  • Actually, I noticed that in the man page for exec it says that "The exec() family of functions replaces the current process image with a new process image. The functions described in this manual page are front-ends for execve(2)." So while I'm not sure about execveat I can feel assured about the exec family.
    – 2rs2ts
    May 11, 2016 at 17:34
  • For people stumbling upon the broken link: security.blogoverflow.com/2013/01/… Apr 15, 2020 at 16:37
  • you can also make a look at tlog. it's not yet in debian (bugs.debian.org/cgi-bin/bugreport.cgi?bug=886927) but integrates very will into cockpit and is natively supported on redhat.
    – blaimi
    Oct 19, 2020 at 16:30
  • In Debian 10, the config file will change constantly, use /etc/audit/rules.d/audit.rules instead.
    – EsmaeelE
    Feb 26, 2022 at 8:57

You can use the script command. This command is not included in POSIX, but you'll find it useful to store in a file all keystrokes as well as output and error messages. You can later view the file. If you are doing some important work and wish to keep a log of all your activities, you should invoke this command immediately after you log in:

$ script
Script started, file is typescript
$ _ Note that this is another shell--child of the login shell

The prompt returns and all your keystrokes (including the one used to backspace) that you now enter here get recorded in the file 'typescript'. After your recording is over, you can terminate the session by entering exit.
Note: If you enter script filename, the session will be stored in the file filename rather than typescript, i.e, typescript is the default if no specific filename is specified.

You can now use cat filename or cat typescript, whichever the case may be, to view the recorded session.

If you wish to append a new session to an old file use: script -a Appends the new session to typescript, same default rule applies here too

This is one way in which a system admin can keep track of the sessions. Hope it has been informative and useful. Cheers!

  • 1
    That is incredibly easy to subvert: (1) open view. (2) Execute any commands you like using :! sudo whatever (3) exit view. (4) Tell the auditor you just looked at a data file, and in read-only mode anyway. Neither history nor script will tell on you. Jun 17, 2020 at 8:44
  • This was a super helpful tip. I think this is great for when the actor you want to monitor isn't malicious - i.e. yourself, if you just want a log of everything conducted during a session. This can be handy in some heavy debugging sessions. Aug 20, 2020 at 23:47

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.