正在浏览 bash 里的文章

工作中可能会有无聊的黑客在你服务器上转悠,黑客智商都不错,所有离开的时候就会删除history记录。怎么办才能记录下用户的历史记录呢?
原理:将history记录到syslog上面,并实时的传送到了远端的日志集中服务器上。

方法:使用bash4.1的新功能:历史命令保存到syslog!然后使用syslog-ng构建集中型日志服务器收集主机日志。

1、下载bash:

#wget http://ftp.gnu.org/gnu/bash/bash-4.1.tar.gz
#tar zxvf bash-4.1.tar.gz –C /tmp/bash-4.1
#cd /tmp/bash-4.1

2、修改源码

(根据个人需要,我只保留了pid,uid,sid等,参数请看目录下的shell.c中):
文件bashhist.c大约708行的位置开始,修改成以下一段:

syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "HISTORY: PID=%d PPID=%d SID=%d  User=%s CMD=%s", getpid(), getppid(), getsid(getpid()),  current_user.user_name, line);
else
{
strncpy (trunc, line, SYSLOG_MAXLEN);
trunc[SYSLOG_MAXLEN - 1] = '\0';
syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "HISTORY (TRUNCATED): PID=%d  PPID=%d SID=%d User=%s CMD=%s", getpid(), getppid(), getsid(getpid()),  current_user.user_name, trunc);
}

注:
ppid:跟踪sh切换后的用户
Sid: 跟踪 su 切换后的用户
第二段代表log长度超过600后使用的语句
修改config-top.h文件

/*#define SYSLOG_HISTORY*/
修改为
#define SYSLOG_HISTORY

编译安装
# ./configure --prefix=/usr/local/bash_4.1 && make && make install
修改用户配置:
将用户的bash换成现在的bash4.1
# vi /etc/passwd
dongwm:x:501:501::/home/dongwm:/usr/local/bash_4.1/bin/bash
这样日志就会记在/var/log/messages
结果类似这样:

Dec 23 17:40:28 server -bash: HISTORY: PID=4089 PPID=4088 SID=4089 User=dongwm CMD=exit
Dec 23 17:41:47 server -bash: HISTORY: PID=4282 PPID=4278 SID=4282 User=root CMD=exit
Dec 23 17:41:53 server -bash: HISTORY: PID=4321 PPID=4317 SID=4321 User=root CMD=ssh java00
Dec 23 17:44:09 server -bash: HISTORY: PID=2152 PPID=2137 SID=2152 User=root CMD=vi Clean_javalog.sh
Dec 23 17:45:16 server -bash: HISTORY: PID=2152 PPID=2137 SID=2152 User=root CMD=sh Clean_javalog.sh
Dec 23 17:45:30 server -bash: HISTORY: PID=2152 PPID=2137 SID=2152 User=root CMD=cat /dev/shm/cleanJavaLog.log
Dec 23 17:46:08 server -bash: HISTORY: PID=2152 PPID=2137 SID=2152 User=root CMD=vi Clean_javalog.sh
Dec 23 17:48:54 server -bash: HISTORY: PID=2152 PPID=2137 SID=2152 User=root CMD=cat Clean_javalog.sh

......

在整个环境布置了记录功能,就能方便的查出来谁-在何时,用什么账号,做了什么操作...

3、主机syslog配置(添加日志服务器的地址)

# vi /etc/syslog.conf

在最后添加一列:
*.* @server.dongwm.com

4、搭建日志服务器

请参看:http://wenku.baidu.com/view/c3bb49c58bd63186bcebbc7a.html

下图是EventLog收集到的bash历史记录信息
Screenshot.png

  除了使用history命令查看历史命令外,Linux系统还提供了非常灵活的Shell历史命令调用方法,我们可以在Shell命令提示符或者Shell脚本中使用它们:
  !!    前一条命令;
  !:0    不带参数的前一条命令名;
  !^    前一条命令的第一个参数;
  !:n    前一条命令的第n个参数;
  !$     前一条命令的最后一个参数;
  !*     前一条命令的所有参数,命令名除外;
  !n     第n条命令;
  !-n    倒数第n条命令;
  !str    最近一条以str开头的命令;
  !?str    最近一条包含str的命令;
  ^a^b  将上一条命令名中的a替换为b;
  !:gs/a/b 将上一条命令的所有a替换为b(包含命令名和参数)。

mysql安装后提供了一个perl的脚本:mysql_convert_table_format 它默认情况下可以将某个表或某个数据库的所有表转换为MyISAM的存储引擎,通过修改该脚本可以将默认修改的存储引擎改为我们需要的存储引擎,如Innodb。该脚本使用了DBI和DBD的perl模块,如果系统没有安装的话可以使用yum intall perl-DBI perl-DBD-mysql 命令来安装。本文介绍的不是mysql_convert_table_format工具的使用,而是我自己写的一个shell脚本,也可以完成批量转换表存储引擎的目的,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
 
# File          : convert_storage_engine.sh
# Info          : 批量转换表存储引擎,如果忽略-t参数
#                 则转换指定数据库中的所有表。
# Arg1          : -d dbname
# Arg2          : -t [tables]
# Arg3          : -e engine type (myisam | innodb)
# Author        : Cooper
# Site          : http://salogs.com 
# Version       : 2010.05.10 First Release
 
User=root
Pwd=dbpwd
TmpFile="/dev/shm/table.tmp"
MySQLBin="mysql -u$User -p$Pwd -e "
 
function Usage()
{
 echo "Useage:$0 -d dbname [-t table] -e engine_type"
}
 
# 检查用户输入的参数
if [ $# -eq 0 ] ;then
        Usage
        exit 1
fi
 
# 获得命令行参数值,并作相应处理
while getopts t:d:e:h OPTION
do
        case $OPTION in
        d)
           {    # 检查数据库是否存在
                DBName=$OPTARG
                DBExists=`$MySQLBin "show databases;" | grep $DBName`
                if [ "$DBExists" == "" ];then
                   echo "$DBName database not exists!"
                   exit 1
                fi
           }
           ;;
        t)
           {     # 检查表是否存在
                 TableName=$OPTARG
                 TableExists=`$MySQLBin "use $DBName;show tables" | grep $TableName`
                  if [ "$TableExists" == "" ];then
                      echo "$TableName table not exists!"
                      exit 1
                  fi
             }
             ;;
        e)
             EngineName=`echo $OPTARG | tr A-Z a-z `
             if [ $EngineName != "innodb" ] && [ $EngineName != "myisam" ];then
                Usage
                 exit 1
              fi
              ;;
        \?|h)
              Usage
               exit 0
               ;;
        esac
done
 
# 转换表
if [ "$TableName" != "" ];then
{
    CurrentEngine=`$MySQLBin "use $DBName;show table status like \"$TableName\"\G"\
| grep Engine | awk '{print $2}'|tr A-Z a-z`
    if [ $CurrentEngine == $EngineName ];then
        echo "Current Table $Table is already of type $EngineName;Ignored"
        exit 0
    fi
    $MySQLBin "use $DBName;alter table $TableName engine=$EngineName;"
}
else
{
    $MySQLBin "use $DBName;show tables;" | sed 1d >$TmpFile
    while read Table
    do
        CurrentEngine=`$MySQLBin "use $DBName;show table status like '$Table'\G"\
| grep Engine | awk '{print $2}'| tr A-Z a-z`
        if [ $CurrentEngine == $EngineName ];then
           echo "Current Table $Table is already of type $EngineName;Ignored"
           continue
        fi
        $MySQLBin "use $DBName;alter table $Table engine=$EngineName;"
    done<$TmpFile
}
fi
 
rm -rf $TmpFile

注意:
1、由于Innodb不支持FULLTEXT索引,因此在转换表时要格外注意。
2、不要将系统表mysql转换为Innodb,如果这样做了的话,mysql绝对无法启动,需要使用mysql_install_db 命令重建系统表。
3、修改现网数据库需格外注意,转换表时会对表进行锁定。

原创文章,转载请注明: 转自 http://salogs.com

Bash 使用技巧

抢沙发

Bash 是我们经常与之打交道的 Shell 程序,本文针对其使用技巧进行了搜罗。相信在你看过这些内容之后,定会在 Bash 的世界里游刃有余。

  • 从历史中执行命令 有时候,我们需要在 Bash 中重复执行先前的命令。你当然可以使用上方向键来查看之前曾经运行过的命令。但这里有一种更好的方式:你可以按 Ctrl + r 组合键进入历史搜索模式,一旦找到需要重复执行的命令,按回车键即可。
  • 重复命令参数 先来看一个例子: mkdir /path/to/exampledir cd !$ 本例中,第一行命令将创建一个目录,而第二行的命令则转到刚创建的目录。这里,“!$”的作用就是重复前一个命令的参数。事实上,不仅是命令的参数可以重复,命令的选项同样可以。另外,Esc + . 快捷键可以切换这些命令参数或选项。
  • 用于编辑的快捷键
    • Ctrl + a:将光标定位到命令的开头
    • Ctrl + e:与上一个快捷键相反,将光标定位到命令的结尾
    • Ctrl + u:剪切光标之前的内容
    • Ctrl + k:与上一个快捷键相反,剪切光标之后的内容
    • Ctrl + y:粘贴以上两个快捷键所剪切的内容
    • Ctrl + t:交换光标之前两个字符的顺序
    • Ctrl + w:删除光标左边的参数(选项)或内容
    • Ctrl + l:清屏
  • 处理作业 首先,使用 Ctrl + z 快捷键可以让正在执行的命令挂起。如果要让该进程在后台执行,那么可以执行 bg 命令。而 fg 命令则可以让该进程重新回到前台来。使用 jobs 命令能够查看到哪些进程在后台执行。 你也可以在 fg 或 bg 命令中使用作业 id,如: fg %3 又如: bg %7
  • 使用置换
    • 命令置换 先看例子: du -h -a -c $(find . -name *.conf 2>&-) 注意 $() 中的部分,这将告诉 Bash 运行 find 命令,然后把返回的结果作为 du 的参数。
    • 进程置换 仍然先看例子: diff <(ps axo comm) <(ssh user@host ps axo comm) 该命令将比较本地系统和远程系统中正在运行的进程。请注意 <() 中的部分。
    • xargs 看例: find . -name *.conf -print0 | xargs -0 grep -l -Z mem_limit | xargs -0 -i cp {} {}.bak 该命令将备份当前目录中的所有 .conf 文件。
  • 使用管道 下面是一个简单的使用管道的例子: ps aux | grep init 这里,“|”操作符将 ps aux 的输出重定向给 grep init。 下面还有两个稍微复杂点的例子: ps aux | tee filename | grep init 及: ps aux | tee -a filename | grep init
  • 将标准输出保存为文件 你可以将命令的标准输出内容保存到一个文件中,举例如下: ps aux > filename 注意其中的“>”符号。 你也可以将这些输出内容追加到一个已存在的文件中: ps aux >> filename 你还可以分割一个较长的行: command1 | command2 | ... | commandN > tempfile1 cat tempfile1 | command1 | command2 | ... | commandN > tempfile2
  • 标准流:重定向与组合 重定向流的例子: ps aux 2>&1 | grep init 这里的数字代表:
    • 0:stdin
    • 1:stdout
    • 2:sterr

    上面的命令中,“grep init”不仅搜索“ps aux”的标准输出,而且搜索 sterr 输出。

如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率。本文将通过实例的方式向你介绍 history 命令的 15 个用法。

使用 HISTTIMEFORMAT 显示时间戳

当你从命令行执行 history 命令后,通常只会显示已执行命令的序号和命令本身。如果你想要查看命令历史的时间戳,那么可以执行:
# export HISTTIMEFORMAT='%F %T '
# history | more
1  2008-08-05 19:02:39 service network restart
2  2008-08-05 19:02:39 exit
3  2008-08-05 19:02:39 id
4  2008-08-05 19:02:39 cat /etc/redhat-release

注意:这个功能只能用在当 HISTTIMEFORMAT 这个环境变量被设置之后,之后的那些新执行的 bash 命令才会被打上正确的时间戳。在此之前的所有命令,都将会显示成设置 HISTTIMEFORMAT 变量的时间。[感谢 NightOwl 读者补充]

使用 Ctrl+R 搜索历史

Ctrl+R 是我经常使用的一个快捷键。此快捷键让你对命令历史进行搜索,对于想要重复执行某个命令的时候非常有用。当找到命令后,通常再按回车键就可以执行该命令。如果想对找到的命令进行调整后再执行,则可以按一下左或右方向键。
# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt]
(reverse-i-search)`red‘: cat /etc/redhat-release
[Note: Press enter when you see your command, which will execute the command from the history]
# cat /etc/redhat-release
Fedora release 9 (Sulphur)

快速重复执行上一条命令
有 4 种方法可以重复执行上一条命令:
使用上方向键,并回车执行。
按 !! 并回车执行。
输入 !-1 并回车执行。
按 Ctrl+P 并回车执行。

从命令历史中执行一个指定的命令

在下面的例子中,如果你想重复执行第 4 条命令,那么可以执行 !4:
# history | more
1  service network restart
2  exit
3  id
4  cat /etc/redhat-release
# !4
cat /etc/redhat-release
Fedora release 9 (Sulphur)
通过指定关键字来执行以前的命令
在下面的例子,输入 !ps 并回车,将执行以 ps 打头的命令:
# !ps
ps aux | grep yp
root     16947  0.0  0.1  36516  1264 ?        Sl   13:10   0:00 ypbind
root     17503  0.0  0.0   4124   740 pts/0    S+   19:19   0:00 grep yp

使用 HISTSIZE 控制历史命令记录的总行数
将下面两行内容追加到 .bash_profile 文件并重新登录 bash shell,命令历史的记录数将变成 450 条:
# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450

使用 HISTFILE 更改历史文件名称
默认情况下,命令历史存储在 ~/.bashhistory 文件中。添加下列内容到 .bashprofile 文件并重新登录 bash shell,将使用 .commandline_warrior 来存储命令历史:
# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior
使用 HISTCONTROL 从命令历史中剔除连续重复的条目
在下面的例子中,pwd 命令被连续执行了三次。执行 history 后你会看到三条重复的条目。要剔除这些重复的条目,你可以将 HISTCONTROL 设置为 ignoredups:
# pwd
# pwd
# pwd
# history | tail -4
44  pwd
45  pwd
46  pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above]
47  history | tail -4
# export HISTCONTROL=ignoredups
# pwd
# pwd
# pwd
# history | tail -3
56  export HISTCONTROL=ignoredups
57  pwd [Note that there is only one pwd command in the history, even after executing pwd 3 times as shown above]
58  history | tail -4

使用 HISTCONTROL 清除整个命令历史中的重复条目
上例中的 ignoredups 只能剔除连续的重复条目。要清除整个命令历史中的重复条目,可以将 HISTCONTROL 设置成 erasedups:
# export HISTCONTROL=erasedups
# pwd
# service httpd stop
# history | tail -3
38  pwd
39  service httpd stop
40  history | tail -3

# ls -ltr
# service httpd stop
# history | tail -6
35  export HISTCONTROL=erasedups
36  pwd
37  history | tail -3
38  ls -ltr
39  service httpd stop
[Note that the previous service httpd stop after pwd got erased]
40  history | tail -6

使用 HISTCONTROL 强制 history 不记住特定的命令
将 HISTCONTROL 设置为 ignorespace,并在不想被记住的命令前面输入一个空格:
# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
service httpd stop [Note that there is a space at the beginning of service, to ignore this command from history]
# history | tail -3
67  ls -ltr
68  pwd
69  history | tail -3

使用 -c 选项清除所有的命令历史
如果你想清除所有的命令历史,可以执行:
# history -c

命令替换
在下面的例子里,!!:$ 将为当前的命令获得上一条命令的参数:
# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg
补充:使用 !$ 可以达到同样的效果,而且更简单
下例中,!^ 从上一条命令获得第一项参数:
# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi -5 !^
vi anaconda-ks.cfg
为特定的命令替换指定的参数
在下面的例子,!cp:2 从命令历史中搜索以 cp 开头的命令,并获取它的第二项参数:
# cp ~/longname.txt /really/a/very/long/path/long-filename.txt
# ls -l !cp:2
ls -l /really/a/very/long/path/long-filename.txt
下例里,!cp:$ 获取 cp 命令的最后一项参数:
# ls -l !cp:$
ls -l /really/a/very/long/path/long-filename.txt
使用 HISTSIZE 禁用 history
如果你想禁用 history,可以将 HISTSIZE 设置为 0:
# export HISTSIZE=0
# history
# [Note that history did not display anything]
使用 HISTIGNORE 忽略历史中的特定命令
下面的例子,将忽略 pwd、ls、ls -ltr 等命令:
# export HISTIGNORE=”pwd:ls:ls -ltr:”
# pwd
# ls
# ls -ltr
# service httpd stop
# history | tail -3
79  export HISTIGNORE=”pwd:ls:ls -ltr:”
80  service httpd stop
81  history
[Note that history did not record pwd, ls and ls -ltr]

增强 Bash 的功能

抢沙发

下面两个诀窍可以增强 Bash 的功能,一个是针对 Bash 的命令历史管理进行了改善,另一个是使 Bash 能够具有更加智能的自动完成特性。实现的过程并不复杂,只需修改 Bash 的默认配置即可。

  1. 改善 Bash 的命令历史管理功能: Bash 的默认配置会存在一个问题,如果同时打开两个(或两个以上的)控制台,那么在这两个控制台中执行的命令并不会互相分享到 history 中。有的命令历史甚至最终会被覆盖掉。要解决这个问题,可把下列内容添加到 ~/.bashrc 或 ~/.bash_profile 文件中:
    shopt -s histappend
    PROMPT_COMMAND='history -a'
    第一句的作用是将命令追加到 history 中。第二句是在显示命令提示符时,保存 history。
  2. 设置智能的自动完成功能: 在 Bash 中我们已经可以通过按 Tab 键来享用自动完成的特性。通过下面的设置,则可以使用 Up 和 Down 键来选择命令后所跟的参数。在 .inputrc(如果该文件不存在,则创建一个)中加入下列内容:
    "\e[A": history-search-backward
    "\e[B": history-search-forward
    set show-all-if-ambiguous on
    前两句使用 Up 和 Down 在 history 中进行搜索。后一句是按 Tab 显示自动完成。如果结合 Ctrl - R,则更加好用。