Automatically acquire the operation log in the terminal when logging in to Linux

This is the first post in a long time. Recently, I'm playing with ubuntu mate installed on gpd-micropc.

Background

I was looking for a way to automatically get the operation log in the terminal. At first I thought I should write a script command in .bashrc, but it doesn't seem to work. .. I found various articles that I searched for and that were helpful. How do I log all input and output in a terminal session?

environment

ubuntu-mate installed on gpd micropc

OS: Ubuntu 19.10 eoan                                                                                                   
Kernel: x86_64 Linux 5.3.0-40-generic
Shell: bash
DE: MATE 1.22.2
CPU: Intel Celeron N4100 @ 4x 2.4GHz
GPU: Mesa DRI Intel(R) UHD Graphics 600 (Geminilake 2x6)-

What i did

1. Create rsyslog configuration file

Since the log will be output with the logger command, create a configuration file below

vim /etc/rsyslog.d/bash.conf

/etc/rsyslog.d/bash.conf


local6.*    /var/log/commands.log

2. Add .bashrc

Add the following to .bashrc under the home directory of the user whose operation log is to be acquired.

vim /home/hoge(Target user)/.bashrc

/home/hoge(Target user)/.bashrc


whoami="$(whoami)@$(echo $SSH_CONNECTION | awk '{print $1}')"export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "$whoami [$$]: $(history 1 | sed "s/^[ ]*[0-9]\+[ ]*//" ) [$RETRN_VAL]"'

3. Restart the rsyslog service

Restart the rsyslog service

systemctl restart rsyslog.service 

4. Create a configuration file for log rotation

Create a config file to rotate daily with logrotate.d

vim /etc/logrotate.d/commands

/etc/logrotate.d/commands


/var/log/commands.log*
{
»-rotate 7
»-daily
»-dateext
»-missingok
»-notifempty
»-compress
»-postrotate
»-»-/usr/lib/rsyslog/rsyslog-rotate
»-endscript
»-su root root                                                                                                          
}

Verification

Read .bashrc

source .bashrc 

Confirm that the file is created under / var / log as shown below.

ls -la /var/log/commands.log*
-rw-r-----1 syslog adm 4753 March 15 19:34 /var/log/commands.log

/var/log/commands.log


Mar 15 19:34:29 hoge hoge: [email protected] [3457]: 2020-03-15 19:34:29 cd [0]
Mar 15 19:34:39 hoge hoge: [email protected] [3457]: 2020-03-15 19:34:37 source .bashrc  [0]
Mar 15 19:36:56 hoge hoge: [email protected] [3457]: 2020-03-15 19:36:56 ls -la /var/log/commands.log* [0]

Impressions

I'm wondering if I can logrotate. .. After that, I want the output like the script command. ..

Recommended Posts

Automatically acquire the operation log in the terminal when logging in to Linux
How to log in automatically like 1Password from the CLI
Change the message displayed when logging in to Raspberry Pi
[Linux] I want to know the date when the user logged in
Log in to the remote server with SSH
Twitter streaming client to enjoy in the terminal
Use pygogo to get the log in json.
I want to be notified when the command operation is completed on linux!
What happens to the access log when the website is automatically accessed by selenium-webdriver
How to allow nologin users to log in on Linux
How to exit when using Python in Terminal (Mac)
Convenient Linux keyboard operation that I want to teach myself when I was in school
How to delete "(base)" that appears in the terminal when Anaconda is installed on Mac
How to use linux commands in Visual Studio Code terminal
Log in to the fortigate (6.0) management screen from selenium-try to log out
How to update user information when logging in to Django RemoteUserMiddleware
[Django] I want to log in automatically after new registration
I can't log in to the admin page with Django3
How to log in to AtCoder with Python and submit automatically
The minimum methods to remember when aggregating data in Pandas
A story that didn't work when I tried to log in with the Python requests module
Hit the echo command in the Mac terminal to output Hello World
[Linux] When the screen is cut off when installing CentOS in VirtualBox
Measures to be taken when Suspicious Operation occurs in HttpResponse Redirect
LINEbot development, I want to check the operation in the local environment
Settings to be made when starting up the linux server centos7
Try to log in to Netflix automatically using python on your PC
Goodbye to the command prompt in WSL, VSCode and Windows Terminal
I made a command to display a colorful calendar in the terminal
I tried to log in to twitter automatically with selenium (RPA, scraping)
What to do when the value type is ambiguous in Python?
Automatically convert to py file when ui file is updated in PySide
Get the IPv4 address assigned to the network interface in code (Linux)
I tried to make a system to automatically acquire the program guide → register it in the calendar in one day
Programming to fight in the world ~ 5-5,5-6
Programming to fight in the world 5-3
Programming to fight in the world-Chapter 4
In the python command python points to python3.8
[linux] kill command to kill the process
Cython to try in the shortest
When the node disappears in rqt_graph
How to log in to Docker + NGINX
Programming to fight in the world ~ 5-2
Operate Paints Chainer with Selenium to automatically color images in the directory
What to do when the result downloaded via scrapy is in English
How to hide the command prompt when running python in visual studio 2015
[Linux] Command to get a list of commands executed in the past
Use a shortcut to enable or disable the touchpad in Linux Mint
Settings to automatically open and close the QuickFix window when using errormaker.vim
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
How to automatically notify by phone when the python system is down
It is convenient to use stac_info and exc_info when you want to display traceback in log output by logging.