What's the best method of logging a BASH shell session to a file on the host machine?

The preferred output format would be equivalent to PuTTY's "printable output" log setting, as viewed by the user inclusive of input and exlusive of ncurses control character data.

  • Curious to know which shell you're using.
    – dr.pooter
    Jun 15, 2009 at 5:29

7 Answers 7


It might be worth looking at shell or system auditing programs like rootsh or sudosh.

  • rootsh turns out to be nearest to what I want, thanks.
    – Andy
    Jun 19, 2009 at 10:29
  • 1
    You can also take in consideration acct/psacct (BSD accounting) or snoopy. They do not log the output, but they are also less verbose. See: cyberciti.biz/tips/… and sourceforge.net/projects/snoopylogger Mar 23, 2010 at 17:28
  • The rootsh app is now deprecated and sudosh seems unmaintained. We implemented a paper-thin wrapper around sudo to allow sudo to be used as a login shell for the purpose of session logging. See here: github.com/cloudposse/sudosh Mar 14, 2017 at 4:13

You could start gnu screen and use the log feature. I don't know of anything that is going to give you a really good log for ncurses applications like top.

man screen

log [on|off]

Start/stop writing output of the current window to a file "screenlog.n"
in the window's default directory, where n is the number of the current
window. This filename can be changed with the `logfile' command. ...

logfile filename
logfile flush secs

Defines the name the log files will get. The default is "screenlog.%n".
The  second  form changes the number of seconds screen will wait before
flushing the logfile buffer to the file-system. The default value is 10
  • The output of screen is equivalent to script - ncurses handling appears unsupported.
    – Andy
    Jun 15, 2009 at 14:54

TTYrec is probably the easiest solution. You can use the original:


Or roll your own:


Or, you could get all old school and | all your commands to tee command.timestamp.tee


The script program will take a complete log of your shell session. Dunno if it's equivalent to any PuTTY thing.

  • Thanks, but it's not suitable as I spend time on the shell with top open, which screen records as: [andy@165 ~]$ top ESC[HESC[2JESC(BESC[mtop - 23:34:49 up 19 days, 17:42, 1 user, load average: 0.11, 0.07, 0.01ESC(BESC[mESC[39;49mESC[K
    – Andy
    Jun 14, 2009 at 22:40

I'm not sure exactly what you're trying to achieve here. Is this for training or forensics? If you want something that produces nice readable log files you're probably out of luck. If you're looking at being able to playback sessions, ttyrec is what you're after. You can run it as part of a login script to capture a user's session, but make sure you use 'trap' to stop a user from exiting ttyrec and getting back to a shell.

  • Personal auditing and plain text logging; playback of sessions is not required.
    – Andy
    Jun 15, 2009 at 14:02
  • 2
    script is the nearest thing to what you want, although as stated it can't deal with ncurses nicely. We use script for logging all sessions and just try to see past the mess that ncurses will make.
    – goo
    Jun 15, 2009 at 15:40
  • Thanks Geoff, looks like I might be stuck with script then.
    – Andy
    Jun 15, 2009 at 16:22

All entered commands are written into .bash_history file, normally in your home. You can set count of lines that are stored via HISTFILESIZE setting.

There are no timestamps and output, however.

If this is really needed, I'd make a wrapper around bash, copying stdin and stdout to a file and work through that.


You can try to use http://xgu.ru/wiki/LiLaLo It is made in Russia, so documentation is also in Russian, but Google Translate will help you. Here is an example of the output:alt text
(source: xgu.ru)

  • The translation is a bit wonky. Will this handle something like an interactive top process.
    – dr.pooter
    Jun 15, 2009 at 6:14
  • as to picture, it does not handle vi, so no top i think. But logging is pretty attractive.
    – TiFFolk
    Jun 16, 2009 at 7:22

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.