本文主要是介绍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 系统添加操作记录审计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!