Record everything on command line centos /fedora/ ubuntu

When I want to log a root session I use rootsh. If admins are required to use sudo for all commands, it will also log all commands.

You might take a look at screen too, as it might accomplish what you're attempting to do.

Ultimately, logging may not be the best situation for your workflow. Requiring a motd, wiki, or simple Web site to be updated for all changes may be better.


Take a look at my history logging functions and see if they could be adapted to your needs.

This is what a couple of typical lines from my log file might look like:

echo hello ### [email protected] Bash-4.0 /dev/pts/1 192.168.2.22 20100510 17:53 /home/user/test
cd .. ### [email protected] Bash-4.0 /dev/pts/1 192.168.2.22 20100510 17:53 /home/user/test -> /home/user

The first line above includes a command, a comment marker, the user/host, shell version, terminal device and IP, the date and time and the directory that was current when the command was issued. The second line is an example of an entry that is produced when a cd command is used and the function's -t option is active. It shows the previous and new working directories.

To set that up:

  • Source the function
  • set the variable
    hcmntextra='date "+%Y%m%d %R"'
  • set the variable
    PROMPT_COMMAND='hcmnt -eityl ~/.hcmnt.log "$LOGNAME@$HOSTNAME Bash-${BASH_VERSION%.*}"'

Choose appropriate locations for files and add the steps above to your ~/.bashrc files.

The function takes several optional switches. One of interest may be -l. With it, commands are written to a log file. You can grep that file and copy and paste commands that you need to re-run. If you omit that switch, the annotated lines are instead written to the shell's history where they can be recalled and reused using the history manipulation features of Bash. The fact that the annotations follow a comment character means that you can re-use the commands as-is and the annotations will be ignored.


Here's a manual option. Try the script command.

script -a ticketnum.txt then do what you need to do ctrl+d stops the recording and the resulting file can be attached to the ticket. I'm sure this can be automated a bit with some scripting.


You may be interested in something like shell_sink for recording and annotating your shell history.