Bash Reference Manual, Controlling the Prompt, Linux 系统添加操作记录审计

本文主要是介绍Bash Reference Manual, Controlling the Prompt, Linux 系统添加操作记录审计,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Bourne-Shell-Variables

http://www.68idc.cn/help/server/linux/2014042190951.html

Linux 系统添加操作记录审计

有时候我们需要对线上用户操作记录进行历史记录待出现问题追究责任人,,但Linux系统自带的history命令用户有自行删除权限,那怎么设置可以让用户的操作记录实时记录,并保证普通用户无权删除呢?本文教你一招

1.mkdir -p /usr/local/domob/records/

chmod 777 /usr/local/domob/records/

chmod +t /usr/local/domob/records/

2.vi /etc/profile 在最后添加下面的代码

if [ ! -d  /usr/local/domob/records/${LOGNAME} ]thenmkdir -p /usr/local/domob/records/${LOGNAME}chmod 300 /usr/local/domob/records/${LOGNAME}fiexport HISTORY_FILE="/usr/local/domob/records/${LOGNAME}/bash_history"export PROMPT_COMMAND='{ date "+%Y-%m-%d %T ##### $(who am i |awk "{print \$1\" \"\$2\" \"\$5}") #### $(history 1 | { read x cmd; echo "$cmd"; })"; } >>$HISTORY_FILE'

这个命令你看得懂么
http://blog.lishiming.net/?p=484

符号使用

大括号里面是一条date命令加分号,大括号和分号应该可以省略,但赋值给 PROMPT_COMMAND还是括起来好一点;
单引号应该是文字形式赋值,但是PROMPT_COMMAND让这句文字运行起来,就像在本shell运行一个一句话的脚本;
date里面放置内容没问题,但是还可以把命令结果输出;$()的形式;
$()里面不支持特殊符号,所以需要反斜杠;
awk一般用单引号,但是$()可能是变量赋值形式,单引号脱义,不适用,改成双引号;
管道符后是两句命令,所以需要大括号和分号;
\" \"就是提供了一个空格;

PROMPT_COMMAND

If set, the value is interpreted as a command to execute before the printing of each primary prompt ($PS1).

6.9 Controlling the Prompt

The value of the variable PROMPT_COMMAND is examined just before Bash prints each primary prompt. If PROMPT_COMMAND is set and has a non-null value, then the value is executed just as if it had been typed on the command line.

In addition, the following table describes the special characters which can appear in the prompt variables PS0, PS1, PS2, and PS4:

\a
A bell character.

\d
The date, in “Weekday Month Date” format (e.g., “Tue May 26”).

\D{format}
The format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required.

\e
An escape character.

\h
The hostname, up to the first ‘.’.

\H
The hostname.

\j
The number of jobs currently managed by the shell.

\l
The basename of the shell’s terminal device name.

\n
A newline.

\r
A carriage return.

\s
The name of the shell, the basename of $0 (the portion following the final slash).

\t
The time, in 24-hour HH:MM:SS format.

\T
The time, in 12-hour HH:MM:SS format.

@
The time, in 12-hour am/pm format.

\A
The time, in 24-hour HH:MM format.

\u
The username of the current user.

\v
The version of Bash (e.g., 2.00)

\V
The release of Bash, version + patchlevel (e.g., 2.00.0)

\w
The current working directory, with $HOME abbreviated with a tilde (uses the $PROMPT_DIRTRIM variable).

\W
The basename of $PWD, with $HOME abbreviated with a tilde.

!
The history number of this command.

#
The command number of this command.

$
If the effective uid is 0, #, otherwise $.

\nnn
The character whose ASCII code is the octal value nnn.

\
A backslash.

[
Begin a sequence of non-printing characters. This could be used to embed a terminal control sequence into the prompt.

]
End a sequence of non-printing characters.

The command number and the history number are usually different: the history number of a command is its position in the history list, which may include commands restored from the history file (see Bash History Facilities), while the command number is the position in the sequence of commands executed during the current shell session.

After the string is decoded, it is expanded via parameter expansion, command substitution, arithmetic expansion, and quote removal, subject to the value of the promptvars shell option (see The Shopt Builtin). This can have unwanted side effects if escaped portions of the string appear within command substitution or contain characters special to word expansion.

这篇关于Bash Reference Manual, Controlling the Prompt, Linux 系统添加操作记录审计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/816250

相关文章

apache的commons-pool2原理与使用实践记录

《apache的commons-pool2原理与使用实践记录》ApacheCommonsPool2是一个高效的对象池化框架,通过复用昂贵资源(如数据库连接、线程、网络连接)优化系统性能,这篇文章主... 目录一、核心原理与组件二、使用步骤详解(以数据库连接池为例)三、高级配置与优化四、典型应用场景五、注意事

Linux CPU飙升排查五步法解读

《LinuxCPU飙升排查五步法解读》:本文主要介绍LinuxCPU飙升排查五步法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录排查思路-五步法1. top命令定位应用进程pid2.php top-Hp[pid]定位应用进程对应的线程tid3. printf"%

Linux下安装Anaconda3全过程

《Linux下安装Anaconda3全过程》:本文主要介绍Linux下安装Anaconda3全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录简介环境下载安装一、找到下载好的文件名为Anaconda3-2018.12-linux-x86_64的安装包二、或者通

使用Python实现Windows系统垃圾清理

《使用Python实现Windows系统垃圾清理》Windows自带的磁盘清理工具功能有限,无法深度清理各类垃圾文件,所以本文为大家介绍了如何使用Python+PyQt5开发一个Windows系统垃圾... 目录一、开发背景与工具概述1.1 为什么需要专业清理工具1.2 工具设计理念二、工具核心功能解析2.

Linux系统之stress-ng测压工具的使用

《Linux系统之stress-ng测压工具的使用》:本文主要介绍Linux系统之stress-ng测压工具的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、理论1.stress工具简介与安装2.语法及参数3.具体安装二、实验1.运行8 cpu, 4 fo

Linux lvm实例之如何创建一个专用于MySQL数据存储的LVM卷组

《Linuxlvm实例之如何创建一个专用于MySQL数据存储的LVM卷组》:本文主要介绍使用Linux创建一个专用于MySQL数据存储的LVM卷组的实例,具有很好的参考价值,希望对大家有所帮助,... 目录在Centos 7上创建卷China编程组并配置mysql数据目录1. 检查现有磁盘2. 创建物理卷3. 创

Linux查看系统盘和SSD盘的容量、型号及挂载信息的方法

《Linux查看系统盘和SSD盘的容量、型号及挂载信息的方法》在Linux系统中,管理磁盘设备和分区是日常运维工作的重要部分,而lsblk命令是一个强大的工具,它用于列出系统中的块设备(blockde... 目录1. 查看所有磁盘的物理信息方法 1:使用 lsblk(推荐)方法 2:使用 fdisk -l(

Linux中的more 和 less区别对比分析

《Linux中的more和less区别对比分析》在Linux/Unix系统中,more和less都是用于分页查看文本文件的命令,但less是more的增强版,功能更强大,:本文主要介绍Linu... 目录1. 基础功能对比2. 常用操作对比less 的操作3. 实际使用示例4. 为什么推荐 less?5.

SpringBoot实现文件记录日志及日志文件自动归档和压缩

《SpringBoot实现文件记录日志及日志文件自动归档和压缩》Logback是Java日志框架,通过Logger收集日志并经Appender输出至控制台、文件等,SpringBoot配置logbac... 目录1、什么是Logback2、SpringBoot实现文件记录日志,日志文件自动归档和压缩2.1、

python操作redis基础

《python操作redis基础》Redis(RemoteDictionaryServer)是一个开源的、基于内存的键值对(Key-Value)存储系统,它通常用作数据库、缓存和消息代理,这篇文章... 目录1. Redis 简介2. 前提条件3. 安装 python Redis 客户端库4. 连接到 Re