0

How can I log all executed commands over SSH on the client machine?

Let's say I'm making a connection to example.com and type the following commands:

$ ls
$ touch hello
$ mkdir world
$ mv hello world/

I want on my client to have all typed commands in a file like this:

ls
touch hello
mkdir world
mv hello world/

My goal is if I make a mistake and break the server, I still can search which command goes wrong. I can't use ~/.bash_logout to save commands after disconnection nor ~/.bash_history because it's server-side. I didn't find anything relevant with ~/.ssh/config so I'm asking here.

Any idea?

EDIT:

To be more clear, if I connect from machine A to machine B, I want to have the history file on machine A.

0

It most cases, this will already in your history file, e.g. ~/.bash_history

  • 1
    ~/.bash_history is on the server side, not on the client side. If I close the connection, I can't check which commands I have typed. – wqeqwsd Jul 12 '18 at 8:26
0

you can find all commands you have typed by history or cat ~/.bash_history or even if you want redirect output of that command to a file you can write cat ~/.bash_history > your file name your file name is a file that you want to save history in that file or if you want to review last n command that you have typed from history you can type history | tail -n and you can redirect them to a file by typing history | tail -n > your_file_name

  • ~/.bash_history is on the server side, not on the client side. If I close the connection, I can't check which commands I have typed. – wqeqwsd Jul 12 '18 at 8:27
0

This can done by adding following line in the /etc/bashrc file

PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "$(whoami) [$$]: $(history 1 | sed "s/^[ ][0-9]+[ ]//" ) [$RETRN_VAL]"'

you also have to create a file commands.conf within the /etc/rsyslog.d/ directory and the contents of the file should be like:

local6.*    /var/log/commands.log

Then restart rsyslog service All log will be capture in /var/log/commands.log

  • If I connect to B with A, will the file be on A? – wqeqwsd Jul 12 '18 at 9:41
  • No. its system file, Not user based. put into /etc/bashrc. and work for all users. – sanjayparmar Jul 12 '18 at 11:05
  • @wqeqwsd please accept the answer if it solved your problem – sanjayparmar Jul 16 '18 at 7:06
  • As I need the file to be on the client side, unfortunately it does not help me – wqeqwsd Jul 16 '18 at 7:58

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.