工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆
浏览模式: 标准 | 列表分类:linux

linux下硬盘检测工具

linux下硬盘检测工具: smartmontools

工具主页: http://smartmontools.sourceforge.net/

Smartmontools for SCSI硬盘: http://smartmontools.sourceforge.net/smartmontools_scsi.html

smartctl命令参数列表:

The following options are currently available for SCSI disks and tape drives unless otherwise noted:

  • -a | --all : equivalent to the combination -i -H -A -l error -l selftest options invoked in that order.

  • -A | --attributes : outputs the current device temperature, trip temperature, the number of elements in the grown defect list (GLIST) and data from the start-stop log page. Outputs some vendor specific information if available.

  • -C | --captive : used in conjunction with -t short or -t long options to do short or long self tests in the foreground. [Has no effect on tape drives.]

  • -d TYPE | --device=TYPE where TYPE is "ata", "scsi", "sat", "marvell", "3ware,N", "hpt,L/N[,M]" or "cciss,N". Overrides utility's guess about the class of the device which is based on the form of the nominated device's name.

  • -h | --help : outputs lengthy usage message and exits without any other action.

  • -H | --health : outputs single device health metric determined by the device manufacturer. This will be "OK" or a failure message.

  • -i | --info : outputs device identification information (derived from a SCSI INQUIRY command) and whether the device supports SMART (and temperature warnings) and if those facilities are currently enabled. The type of transport (e.g. FC or SAS) is also reported, if available. Some users have reported disks that report the wrong transport.

  • -l TYPE | --log=TYPE where TYPE is either "background", "selftest" or "error". Decodes are outputs the requested log. Note that --all does not include --log=background .

  • -q TYPE | --quietmode=TYPE where TYPE is either "silent" or "errorsonly". When the type is silent then nothing is output to the console but the exit status is set (so it is suitable for scripts). For "errorsonly" only errors are output to the console. The exit status is always set. [See the smartctl man page.]

  • -r TYPE | --report=TYPE where TYPE is either "ioctl[,<n>]" or "scsiioctl[,<n>]". Turns on low level debugging of issued commands and responses. These commands are issued through a system command called an "ioctl" in Unix. The debug can be for all issued commands (i.e. "ioctl") or only SCSI commands ("scsiioctl"). Optionally the TYPE can have a comma and a number post pended to increase the volume of debug. See this section for more details.

  • -s VALUE | --smart=VALUE where VALUE is either "on" or "off". Enables or disables SMART monitoring (and temperature warnings).

  • -S VALUE | --saveauto=VALUE where VALUE is either "on" or "off". Controls whether the error log values are preserved across device power cycles.

  • -t TEST | --test=TEST where TEST is either "offline", "short" or "long". Despite its name "offline" is a short foreground test that all SCSI devices should support. A "short" self test is typically 2 minutes or less. A "long" self test will be considerably longer than 2 minutes, depending on the size of the media. The estimated time that a "long" self test will take is printed after the "selftest" log (i.e. with '-l selftest' or '-a')

  • -V | --version : outputs the smartctl version number (including the cvs version of all its source files) and build information then exits without any other action.

  • -X | --abort : will terminate a background short or long self test. Usually the self test log notes that a self test has been aborted. [Has no effect on tape drives.]

简单用法:
1、smartctl -a  <device>         检查该设备是否已经打开SMART技术。
2、smartctl -s on <device>    如果没有打开SMART技术,使用该命令打开SMART技术。
3、smartctl -t short <device>    后台检测硬盘,消耗时间短;
      smartctl -t long <device>      后台检测硬盘,消耗时间长;
      smartctl -C -t short <device> 前台检测硬盘,消耗时间短;
      smartctl -C -t long <device>  前台检测硬盘,消耗时间长。
其实就是利用硬盘SMART的自检程序。
4、smartctl -X <device>  中断后台检测硬盘。
5、smartctl -l selftest <device>  显示硬盘检测日志。
6、smartctl -l error <device> 显示硬盘错误汇总。

或用 badblocks 工具

1024 new_web

一 系统设置

1 查看系统版本 cat /etc/redhat-release
  查看内核版本 unmae -r or uname -a
下载centos内核,升级内核 http://mirror.centos.org/ rpm -ivh

2 增加用户,删除用户

3 停掉相关的服务,ntsysv/chkconfig

4 bin md5

5 相关命令程序的s权限

6 相关的内核参数 sysctl

7 检查网卡的传输参数 mii-tool

8 设置运行的tty

9 iptables

二 软件安装(脚本略)

*** glibc detected *** double free or corruption //glibc版本有关

[Fri Oct 24 11:35:58 2008] [alert] (12)Cannot allocate memory: apr_thread_create: unable to create worker thread
Error in my_thread_global_end(): 1 threads didn't exit

[Fri Oct 24 11:54:56 2008] [alert] (12)Cannot allocate memory: apr_thread_create: unable to create worker thread
[Fri Oct 24 11:54:56 2008] [alert] (12)Cannot allocate memory: apr_thread_create: unable to create worker thread
[Fri Oct 24 11:54:56 2008] [notice] seg fault or similar nasty error detected in the parent process
[Fri Oct 24 11:54:56 2008] [notice] seg fault or similar nasty error detected in the parent process
[Fri Oct 24 11:54:57 2008] [notice] child pid 28311 exit signal Segmentation fault (11)
[Fri Oct 24 11:54:57 2008] [notice] child pid 28314 exit signal Segmentation fault (11)

//调整apache参数

Apache的KeepAlive和TCP/IP的TIME_WAIT(转)

今天检查了一下基本一台服务器,发现TIME_WAIT高到3k多.TIME_WAIT本身并不会占用很大资源的,除非受到攻击.但太多服务器还是有可能挂掉.
TIME_WAIT 3699
CLOSE_WAIT 52
FIN_WAIT1 32
SYN_SENT 1
FIN_WAIT2 2
ESTABLISHED 17
SYN_RECV 45
CLOSING 6

根据《TCP/IP详解》中的TCP的建立和终止中有关"TCP的终止"的讲解

TCP的终止通过双方的四次握手实现。发起终止的一方执行主动关闭,响应的另一方执行被动关闭。

1. 发起方更改状态为FIN_WAIT_1,关闭应用程序进程,发出一个TCP的FIN段;
2. 接收方收到FIN段,返回一个带确认序号的ACK,同时向自己对应的进程发送一个文件结束符EOF,同时更改状态为CLOSE_WAIT,发起方接到ACK后状态更改为FIN_WAIT_2;
3. 接收方关闭应用程序进程,更改状态为LAST_ACK,并向对方发出一个TCP的FIN段;
4. 发起方接到FIN后状态更改为TIME_WAIT,并发出这个FIN的ACK确认。ACK发送成功后(2MSL内)双方TCP状态变为CLOSED。

我们不难看出上面的显示的结果的意思。根据TCP协议,主动发起关闭的一方,会进入TIME_WAIT状态(TCP实现必须可靠地终止连接的两个方向(全双工关闭)),持续2*MSL(Max Segment Lifetime),缺省为240秒.

为什么 TIME_WAIT 状态需要保持 2MSL 这么长的时间?

TIME_WAIT的等待时间为2MSL,即最大段生存时间.如果 TIME_WAIT 状态保持时间不足够长(比如小于2MSL),第一个连接就正常终止了。第二个拥有相同相关五元组的连接出现(因为连接终止前发起的一方可能需要重发 ACK,所以停留在该状态的时间必须为MSL的2倍。),而第一个连接的重复报文到达,干扰了第二个连接。TCP实现必须防止某个连接的重复报文在连接终 止后出现,所以让TIME_WAIT态保持时间足够长(2MSL),连接相应方向上的TCP报文要么完全响应完毕,要么被丢弃。建立第二个连接的时候,不 会混淆。

注:MSL(最大分段生存期)指明TCP报文在Internet上最长生存时间,每个具体的TCP实现都必须选择一个确定的MSL值。RFC 1122建议是2分钟,但BSD传统实现采用了30秒。TIME_WAIT 状态最大保持时间是2 * MSL,也就是1-4分钟。

对apache的操作
HTTP协议1.1版规定default行为是Keep-Alive,也就是会重用TCP连接传输多个request/response.所以我打开 http中的keepalive On,发现TIME_WAIT就立刻少了下来.只有300的样子.总结一下.我认为有二个原因.

1.keepalive没有开,导致每次请求都要建立新的tcp连接,请求完成以后关闭,增加了很多time_wait的状态,没有重 用,KeepAlive我认为它指的是保持连接活跃,类似于Mysql的永久连接。如果将KeepAlive设置为On,那么来自同一客户端的请求就不需 要再一次连接,避免每次请求都要新建一个连接而加重服务器的负担。
2.然后keepalive在系统中本身的值很高.默认空闲连接 7200 秒(2 小时)内没有活动.才会断开.

我们开启KeepAlive

KeepAlive On
MaxKeepAliveRequests 120
KeepAliveTimeout 15

这样每个连接可以发送100次请求,超时时间为15秒(如果第二次请求和第一次请求之间超过KeepAliveTimeOut的时间的话,第一次连接就会中断,再新建第二个连接)。

有关内核级别的keepalive和time_wait的优化调整

vi /etc/sysctl

net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_keepalive_time = 1800
net.ipv4.tcp_fin_timeout = 30
net.core.netdev_max_backlog =8096

修改完记的使用sysctl -p 让它生效

以上参数的注解
/proc/sys/net/ipv4/tcp_tw_reuse
该文件表示是否允许重新应用处于TIME-WAIT状态的socket用于新的TCP连接。

/proc/sys/net/ipv4/tcp_tw_recycle
recyse是加速TIME-WAIT sockets回收

对tcp_tw_reuse和tcp_tw_recycle的修改,可能会出现.warning, got duplicate tcp line warning, got BOGUS tcp line.上面这二个参数指的是存在这两个完全一样的TCP连接,这会发生在一个连接被迅速的断开并且重新连接的情况,而且使用的端口和地址相同。但基本 上这样的事情不会发生,无论如何,使能上述设置会增加重现机会。这个提示不会有人和危害,而且也不会降低系统性能,目前正在进行工作

/proc/sys/net/ipv4/tcp_keepalive_time
表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时

/proc/sys/net/ipv4/tcp_fin_timeout   最佳值和BSD一样为30
fin_wait1状态是在发起端主动要求关闭tcp连接,并且主动发送fin以后,等待接收端回复ack时候的状态。对于本端断开的socket连接,TCP保持在FIN-WAIT-2状态的时间。对方可能会断开连接或一直不结束连接或不可预料的进程死亡。

/proc/sys/net/core/netdev_max_backlog
该文件指定了,在接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目。

解决TIME_WAIT过多问题(转)

#netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’

LAST_ACK 14
SYN_RECV 348
ESTABLISHED 70
FIN_WAIT1 229
FIN_WAIT2 30
CLOSING 33
TIME_WAIT 18122

状态:描述
CLOSED:无连接是活动的或正在进行
LISTEN:服务器在等待进入呼叫
SYN_RECV:一个连接请求已经到达,等待确认
SYN_SENT:应用已经开始,打开一个连接
ESTABLISHED:正常数据传输状态
FIN_WAIT1:应用说它已经完成
FIN_WAIT2:另一边已同意释放
ITMED_WAIT:等待所有分组死掉
CLOSING:两边同时尝试关闭
TIME_WAIT:另一边已初始化一个释放
LAST_ACK:等待所有分组死掉

也就是说,这条命令可以把当前系统的网络连接状态分类汇总。

下面解释一下为啥要这样写:

一个简单的管道符连接了netstat和awk命令。

——————————————————————

先来看看netstat:

netstat -n

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 123.123.123.123:80 234.234.234.234:12345 TIME_WAIT

你实际执行这条命令的时候,可能会得到成千上万条类似上面的记录,不过我们就拿其中的一条就足够了。

——————————————————————

再来看看awk:

/^tcp/
滤出tcp开头的记录,屏蔽udp, socket等无关记录。

state[]
相当于定义了一个名叫state的数组

NF
表示记录的字段数,如上所示的记录,NF等于6

$NF
表示某个字段的值,如上所示的记录,$NF也就是$6,表示第6个字段的值,也就是TIME_WAIT

state[$NF]
表示数组元素的值,如上所示的记录,就是state[TIME_WAIT]状态的连接数

++state[$NF]
表示把某个数加一,如上所示的记录,就是把state[TIME_WAIT]状态的连接数加一

END
表示在最后阶段要执行的命令

for(key in state)
遍历数组

print key,”\t”,state[key]
打印数组的键和值,中间用\t制表符分割,美化一下。

如发现系统存在大量TIME_WAIT状态的连接,通过调整内核参数解决,
vim /etc/sysctl.conf
编辑文件,加入以下内容:
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
然后执行 /sbin/sysctl -p 让参数生效。

net.ipv4.tcp_syncookies = 1 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
net.ipv4.tcp_fin_timeout 修改系統默认的 TIMEOUT 时间

下面附上TIME_WAIT状态的意义:

客户端与服务器端建立TCP/IP连接后关闭SOCKET后,服务器端连接的端口
状态为TIME_WAIT

是不是所有执行主动关闭的socket都会进入TIME_WAIT状态呢?
有没有什么情况使主动关闭的socket直接进入CLOSED状态呢?

主动关闭的一方在发送最后一个 ack 后
就会进入 TIME_WAIT 状态 停留2MSL(max segment lifetime)时间
这个是TCP/IP必不可少的,也就是“解决”不了的。

也就是TCP/IP设计者本来是这么设计的
主要有两个原因
1。防止上一次连接中的包,迷路后重新出现,影响新连接
(经过2MSL,上一次连接中所有的重复包都会消失)
2。可靠的关闭TCP连接
在主动关闭方发送的最后一个 ack(fin) ,有可能丢失,这时被动方会重新发
fin, 如果这时主动方处于 CLOSED 状态 ,就会响应 rst 而不是 ack。所以
主动方要处于 TIME_WAIT 状态,而不能是 CLOSED 。

TIME_WAIT 并不会占用很大资源的,除非受到攻击。

还有,如果一方 send 或 recv 超时,就会直接进入 CLOSED 状态

 

net.ipv4.tcp_syncookies = 1 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
  net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
  net.ipv4.tcp_tw_recycle = 1 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
  net.ipv4.tcp_fin_timeout = 30 表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。
  net.ipv4.tcp_keepalive_time = 1200 表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时,改为20分钟。
  net.ipv4.ip_local_port_range = 1024 65000 表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为1024到65000。
  net.ipv4.tcp_max_syn_backlog = 8192 表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数。
  net.ipv4.tcp_max_tw_buckets = 5000 表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数字,TIME_WAIT套接字将立刻被清除并打印警告信息。
默  认为180000,改为5000。对于Apache、Nginx等服务器,上几行的参数可以很好地减少TIME_WAIT套接字数量,但是对于Squid,效果却不大。此项参数可以控制TIME_WAIT套接字的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死。

注:
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1

设置这两个参数: reuse是表示是否允许重新应用处于TIME-WAIT状态的socket用于新的TCP连接; recyse是加速TIME-WAIT sockets回收
 

  net.ipv4.tcp_syncookies = 1 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
  net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
  net.ipv4.tcp_tw_recycle = 1 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
  net.ipv4.tcp_fin_timeout = 30 表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。
  net.ipv4.tcp_keepalive_time = 1200 表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时,改为20分钟。
  net.ipv4.ip_local_port_range = 1024    65000 表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为1024到65000。
  net.ipv4.tcp_max_syn_backlog = 8192 表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数。
  net.ipv4.tcp_max_tw_buckets = 5000 表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数字,TIME_WAIT套接字将立刻被清除并打印警告信息。默认为180000,改为5000。对于Apache、Nginx等服务器,上几行的参数可以很好地减少TIME_WAIT套接字数量,但是对于Squid,效果却不大。此项参数可以控制TIME_WAIT套接字的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死。

kernel :CPU1: Running in modulated clock mode 临时解决

kernel :CPU1: Running in modulated clock mode
kernel :CPU1: Temperature above threshold
服务器CPU工作温度过高.

查询资料后得知是 2.6 内核的相应阀值过低造成了这种状况。
vi /etc/syslog.conf
注释掉 *.emerg 这一行
/etc/init.d/syslog restart

就不会出现 CPU 温度过高的警告了,不知道会 不会出现其他错误...

TCP: time wait bucket table overflow

提示

Oct 10 13:47:20 localhost kernel: printk: 30 messages suppressed.
Oct 10 13:47:20 localhost kernel: TCP: time wait bucket table overflow
Oct 10 13:47:20 localhost last message repeated 9 times
Oct 10 13:47:27 localhost kernel: printk: 107 messages suppressed.
Oct 10 13:47:27 localhost kernel: TCP: time wait bucket table overflow
Oct 10 13:49:05 localhost kernel: printk: 239 messages suppressed.
Oct 10 13:49:05 localhost kernel: TCP: time wait bucket table overflow

加大 /proc/sys/net/ipv4/tcp_max_tw_buckets 值,默认为38000

可以直接 echo 50000 > /proc/sys/net/ipv4//proc/sys/net/ipv4/tcp_max_tw_buckets
或设置sysctl

 

awk用法小结(转)

awk 用法:awk ' pattern {action} ' 

变量名 含义
ARGC 命令行变元个数
ARGV 命令行变元数组
FILENAME 当前输入文件名
FNR 当前文件中的记录号
FS 输入域分隔符,默认为一个空格
RS 输入记录分隔符
NF 当前记录里域个数
NR 到目前为止记录数
OFS 输出域分隔符
ORS 输出记录分隔符

1、awk '/101/'               file 显示文件file中包含101的匹配行。
   awk '/101/,/105/'         file
   awk '$1 == 5'             file
   awk '$1 == "CT"'          file 注意必须带双引号
   awk '$1 * $2 >100 '       file 
   awk '$2 >5 && $2<=15'     file
2、awk '{print NR,NF,$1,$NF,}' file 显示文件file的当前记录号、域数和每一行的第一个和最后一个域。
   awk '/101/ {print $1,$2 + 10}' file 显示文件file的匹配行的第一、二个域加10。
   awk '/101/ {print $1$2}'  file
   awk '/101/ {print $1 $2}' file 显示文件file的匹配行的第一、二个域,但显示时域中间没有分隔符。
3、df | awk '$4>1000000 '         通过管道符获得输入,如:显示第4个域满足条件的行。
4、awk -F "|" '{print $1}'   file 按照新的分隔符“|”进行操作。
   awk  'BEGIN { FS="[: \t|]" }
   {print $1,$2,$3}'       file 通过设置输入分隔符(FS="[: \t|]")修改输入分隔符。

   Sep="|"
   awk -F $Sep '{print $1}'  file 按照环境变量Sep的值做为分隔符。   
   awk -F '[ :\t|]' '{print $1}' file 按照正则表达式的值做为分隔符,这里代表空格、:、TAB、|同时做为分隔符。
   awk -F '[][]'    '{print $1}' file 按照正则表达式的值做为分隔符,这里代表[、]
5、awk -f awkfile       file 通过文件awkfile的内容依次进行控制。
   cat awkfile
/101/{print "\047 Hello! \047"} --遇到匹配行以后打印 ' Hello! '.\047代表单引号。
{print $1,$2}                   --因为没有模式控制,打印每一行的前两个域。
6、awk '$1 ~ /101/ {print $1}' file 显示文件中第一个域匹配101的行(记录)。
7、awk   'BEGIN { OFS="%"}
   {print $1,$2}'           file 通过设置输出分隔符(OFS="%")修改输出格式。
8、awk   'BEGIN { max=100 ;print "max=" max}             BEGIN 表示在处理任意行之前进行的操作。
   {max=($1 >max ?$1:max); print $1,"Now max is "max}' file 取得文件第一个域的最大值。
   (表达式1?表达式2:表达式3 相当于:
   if (表达式1)
       表达式2
   else
       表达式3
   awk '{print ($1>4 ? "high "$1: "low "$1)}' file 
9、awk '$1 * $2 >100 {print $1}' file 显示文件中第一个域匹配101的行(记录)。
10、awk '{$1 == 'Chi' {$3 = 'China'; print}' file 找到匹配行后先将第3个域替换后再显示该行(记录)。
    awk '{$7 %= 3; print $7}'  file 将第7域被3除,并将余数赋给第7域再打印。
11、awk '/tom/ {wage=$2+$3; printf wage}' file 找到匹配行后为变量wage赋值并打印该变量。
12、awk '/tom/ {count++;} 
         END {print "tom was found "count" times"}' file END表示在所有输入行处理完后进行处理。
13、awk 'gsub(/\$/,"");gsub(/,/,""); cost+=$4;
         END {print "The total is $" cost>"filename"}'    file gsub函数用空串替换$和,再将结果输出到filename中。
    1 2 3 $1,200.00
    1 2 3 $2,300.00
    1 2 3 $4,000.00

    awk '{gsub(/\$/,"");gsub(/,/,"");
    if ($4>1000&&$4<2000) c1+=$4;
    else if ($4>2000&&$4<3000) c2+=$4;
    else if ($4>3000&&$4<4000) c3+=$4;
    else c4+=$4; }
    END {printf  "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n",c1,c2,c3,c4}"' file
    通过if和else if完成条件语句

    awk '{gsub(/\$/,"");gsub(/,/,"");
    if ($4>3000&&$4<4000) exit;
    else c4+=$4; }
    END {printf  "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n",c1,c2,c3,c4}"' file
    通过exit在某条件时退出,但是仍执行END操作。
    awk '{gsub(/\$/,"");gsub(/,/,"");
    if ($4>3000) next;
    else c4+=$4; }
    END {printf  "c4=[%d]\n",c4}"' file
    通过next在某条件时跳过该行,对下一行执行操作。


14、awk '{ print FILENAME,$0 }' file1 file2 file3>fileall 把file1、file2、file3的文件内容全部写到fileall中,格式为
    打印文件并前置文件名。
15、awk ' $1!=previous { close(previous); previous=$1 }   
    {print substr($0,index($0," ") +1)>$1}' fileall 把合并后的文件重新分拆为3个文件。并与原文件一致。
16、awk 'BEGIN {"date"|getline d; print d}'         通过管道把date的执行结果送给getline,并赋给变量d,然后打印。 
17、awk 'BEGIN {system("echo \"Input your name:\\c\""); getline d;print "\nYour name is",d,"\b!\n"}'
    通过getline命令交互输入name,并显示出来。
    awk 'BEGIN {FS=":"; while(getline< "/etc/passwd" >0) { if($1~"050[0-9]_") print $1}}'
    打印/etc/passwd文件中用户名包含050x_的用户名。

18、awk '{ i=1;while(i<NF) {print NF,$i;i++}}' file 通过while语句实现循环。
    awk '{ for(i=1;i<NF;i++) {print NF,$i}}'   file 通过for语句实现循环。    
    type file|awk -F "/" '
    { for(i=1;i<NF;i++)
    { if(i==NF-1) { printf "%s",$i }
    else { printf "%s/",$i } }}'               显示一个文件的全路径。
    用for和if显示日期
    awk  'BEGIN {
for(j=1;j<=12;j++)
{ flag=0;
  printf "\n%d月份\n",j;
        for(i=1;i<=31;i++)
        {
        if (j==2&&i>28) flag=1;
        if ((j==4||j==6||j==9||j==11)&&i>30) flag=1;
        if (flag==0) {printf "%02d%02d ",j,i}
        }
}
}'
19、在awk中调用系统变量必须用单引号,如果是双引号,则表示字符串
Flag=abcd
awk '{print '$Flag'}'   结果为abcd
awk '{print  "$Flag"}'   结果为$Flag

linux命令大全

一篇非常好的linux学习笔记
linux目录架构
/   根目录
/bin    常用的命令 binary file 的目錄
/boot   存放系统启动时必须读取的档案,包括核心 (kernel) 在内
     /boot/grub/menu.lst   GRUB设置
     /boot/vmlinuz   内核
     /boot/initrd     核心解壓縮所需 RAM Disk
/dev    系统周边设备    
/etc    系统相关设定文件
     /etc/DIR_COLORS   设定颜色
     /etc/HOSTNAME   设定用户的节点名
     /etc/NETWORKING   只有YES标明网络存在
     /etc/host.conf 文件说明用户的系统如何查询节点名
     /etc/hosts 设定用户自已的IP与名字的对应表
     /etc/hosts.allow 设置允许使用inetd的机器使用
     /etc/hosts.deny 设置不允许使用inetd的机器使用
     /etc/hosts.equiv 设置远端机不用密码
     /etc/inetd.conf 设定系统网络守护进程inetd的配置
     /etc/gateways 设定路由器
     /etc/protocols 设定系统支持的协议
     /etc/named.boot 设定本机为名字服务器的配置文件
     /etc/sysconfig/network-scripts/ifcfg-eth0   设置IP
     /etc/resolv.conf    设置DNS 
     /etc/X11  X Window的配置文件,xorg.conf 或 XF86Config 這兩個 X Server 的設定檔
     /etc/fstab    记录开机要mount的文件系统
     /etc/inittab 设定系统启动时init进程将把系统设置成什么样的runlevel
     /etc/issue 记录用户登录前显示的信息
     /etc/group 设定用户的组名与相关信息
     /etc/passwd 帐号信息
     /etc/shadow 密码信息
     /etc/sudoers 可以sudo命令的配置文件
     /etc/securetty 设定哪些终端可以让root登录
     /etc/login.defs 所有用户登录时的缺省配置
     /etc/exports 设定NFS系统用的
     /etc/init.d/   所有服務的預設啟動 script 都是放在這裡的,例如要啟動或者關閉
     /etc/xinetd.d/  這就是所謂的 super daemon 管理的各項服務的設定檔目錄
     /etc/modprobe.conf   内核模块额外参数设定
     /etc/syslog.conf   日志设置文件
/home   使用者家目录
/lib    系统会使用到的函数库
     /lib/modules   kernel 的相关模块
     /var/lib/rpm   rpm套件安装处
/lost+found    系統不正常產生錯誤時,會將一些遺失的片段放置於此目錄下
/mnt     外设的挂载点
/media   与/mnt类似
/opt     主机额外安装的软件
/proc    虚拟目录,是内存的映射
      /proc/version   内核版本
       /proc/sys/kernel   系统内核功能
/root    系统管理员的家目录
/sbin    系统管理员才能执行的指令
/srv     一些服務啟動之後,這些服務所需要取用的資料目錄
/tmp     一般使用者或者是正在執行的程序暫時放置檔案的地方
/usr     最大的目录,存许应用程序和文件
    /usr/X11R6:   X-Window目录
    /usr/src:    Linux源代码
    /usr/include:系统头文件
    /usr/openwin 存放SUN的OpenWin
    /usr/man 在线使用手册
    /usr/bin           使用者可執行的 binary file 的目錄
    /usr/local/bin     使用者可執行的 binary file 的目錄
    /usr/lib           系统会使用到的函数库
    /usr/local/lib     系统会使用到的函数库
    /usr/sbin          系统管理员才能执行的指令
    /usr/local/sbin    系统管理员才能执行的指令
/var   日志文件
    /var/log/secure    記錄登入系統存取資料的檔案,例如 pop3, ssh, telnet, ftp 等都會記錄在此檔案


    /var/log/wtmp      記錄登入者的訊息資料, last
    /var/log/messages  幾乎系統發生的錯誤訊息
    /var/log/boot.log  記錄開機或者是一些服務啟動的時候,所顯示的啟動或關閉訊息
    /var/log/maillog   紀錄郵件存取或往來( sendmail 與 pop3 )的使用者記錄
    /var/log/cron      記錄 crontab 這個例行性服務的內容
    /var/log/httpd, /var/log/news, /var/log/mysqld.log, /var/log/samba, /var/log/procmail.log


    分別是幾個不同的網路服務的記錄檔
一些常用的基本命令:
uname -a    查看内核版本      
ls -al    显示所有文件的属性
pwd         显示当前路径       
cd -    返回上一次目录     cd ~    返回主目录
date s      设置时间、日期         
cal      显示日历     cal 2006
bc          计算器具              
man  & info     帮助手册
locale     显示当前字体     locale -a    所有可用字体     /etc/sysconfig/i18n设置文件
LANG=en    使用英文字体           
sync       将数据同步写入硬盘       
shutdonw -h now & half & poweroff  关机
reboot     重启                  
startx  &  init 5   进入图形介面
/work  & ?work    向上、下查找文档内容
chgrp      改变档案群组  chgrp testing install.log   
chown     改变所属人   chown root:root install.log
chmod      改变属性     chmod 777 install.log     read=4  write=2  execute=1
cp   复制   cp filename
rm   删除文件  rm -rf filename   强制删除文件
rmdir   删除文件夹
mv  移动    mv 123.txt 222.txt  重命名
mkdir     创建文件夹
touch     创建文件  更新当前时间
cat       由第一行开始显示     cat |more  分页
nl        在内容前加行号
more  &  less   一面一面翻动
head -n filename   显示第N行内容
tail -n filename  显示后N行内容
od        显示非纯文档
df -h 显示分区空间
du  显示目录或文件的大小
fdisk   分区设置    fdisk -l /dev/hda  显示硬盘分区状态
mkfs    建立各种文件系统  mkfs -t ext3  /dev/ram15  
fsck    检查和修复LINUX档案
ln      硬链接   ln -s  软件链接
whereis   查找命令
locate    查找
find      查找   find / -name "***.***"
which     查看工具
whoami    显示当前用户
gcc -v    查看GCC版本
chattr +i filename  禁止删除   chattr -i filename  取消禁止
lsattr    显示隐藏档属性
updatedb  更新资料库
mke2fs    格式化   mkfs -t ext3
dd if=/etc/passwd of=/tmp/passwd.bak    备份
mount     列出系统所有的分区
mount -t iso9660 /dev/cdrom /mnt/cdrom   挂载光盘
mount -t vfat /dev/fd0 /mnt/floppy       挂载软盘
mount -t vfat -o iocharset=utf8,umask=000 /dev/hda2 /mnt/hda2   挂载fat32分区
mount -t ntfs -o nls=utf8,umask=000 /dev/hda3 /mnt/hda3         挂载ntfs分区
Linux-NTFS Project: http://linux-ntfs.sourceforge.net/
umount /mnt/hda3  缷载
ifconfig   显示或设置网络设备
service network restart   重启网卡 
ifdown eth0  关闭网卡
ifup eth0    开启网卡
clear    清屏
history    历史记录       !55  执行第55个指令
stty   设置终端    stty -a
fdisk /mbr   删除GRUB
at     僅進行一次的工作排程
crontab   循環執行的例行性命令    [e]编辑,[l]显示,[r]删除任务
&       后台运行程序    tar -zxvf 123.tar.gz & --------->后台运行
jobs    观看后台暂停的程序   jobs -l
fg      将后台程序调到前台   fg n ------>n是数字,可以指定进行那个程序
bg      让工作在后台运行
kill    结束进程    kill -9 PID     [9]强制结束,[15]正常结束,[l]列出可用的kill信号
ps aux  查看后台程序  
top     查看后台程序   top -d 2    每两秒更新一次        top -d 2 -p10604   观看某个PID
        top -b -n 2 > /tmp/top.txt ----->將 top 的資訊進行 2 次,然後將結果輸出到 /tmp/top.txt  

 
pstree   以树状图显示程序    [A]以 ASCII 來連接, [u]列出PID, [p]列出帐号
killall   要刪除某個服務    killall -9 httpd
free      显示内存状态     free -m  -------->以M为单位显示
uptime    显示目前系统开机时间
netstat   显示网络状态    netstat -tulnp------>找出目前系統上已在監聽的網路連線及其 PID
dmesg     显示开机信息    demsg | more
nice      设置优先权      nice -n -5 vi & ----->用 root 給一個 nice 植為 -5 ,用於執行 vi
renice    调整已存在优先权
runlevel  显示目前的runlevel
depmod    分析可载入模块的相依性
lsmod     显示已载入系统的模块
modinfo   显示kernel模块的信息
insmod    载入模块
modprobe   自动处理可载入模块
rmmod     删除模块
chkconfig   检查,设置系统的各种服务     chkconfig --list ----->列出各项服务状态
ntsysv     设置系统的各种服务
cpio      备份文件
 
压缩命令:
 *.Z      compress 程式壓縮的檔案;
 *.bz2    bzip2 程式壓縮的檔案;
 *.gz     gzip 程式壓縮的檔案;
 *.tar    tar 程式打包的資料,並沒有壓縮過;
 *.tar.gz tar 程式打包的檔案,其中並且經過 gzip 的壓縮
compress filename  压缩文件  加[-d]解压  uncompress
gzip filename   压缩  加[-d]解压  zcat 123.gz 查看压缩文件内容
bzip2 -z filename  压缩  加[-d]解压   bzcat filename.bz2  查看压缩文件内容
tar -cvf /home/123.tar /etc  打包,不压缩
tar -xvf 123.tar   解开包
tar -zxvf /home/123.tar.gz  以gzip解压
tar -jxvf /home/123.tar.bz2  以bzip2解压
tar -ztvf /tmp/etc.tar.gz   查看tar内容
cpio -covB  > [file|device]   份份
cpio -icduv < [file|device]   还原
vi一般用法
一般模式              编辑模式                  指令模式
h 左               a,i,r,o,A,I,R,O             :w 保存
j 下                进入编辑模式                :w! 强制保存
k 上                dd 删除光标当前行           :q! 不保存离开
l 右                ndd 删除n行                 :wq! 保存后离开
0 移动到行首        yy 复制当前行                :e! 还原原始档
$ 移动到行尾        nyy 复制n行                  :w filename 另存为
H 屏幕最上          p,P 粘贴                     :set nu 设置行号
M 屏幕中央          u  撤消                      :set nonu 取消行号
L 屏幕最下          [Ctrl]+r 重做上一个动作       ZZ 保存离开
G 档案最后一行      [ctrl]+z 暂停退出            :set nohlsearch   永久地关闭高亮显示
/work 向下搜索                                   :sp 同时打开两个文档
?work 向上搜索                                   [Ctrl]+w 两个文档设换
gg 移动到档案第一行                              :nohlsearch    暂时关闭高亮显示
认识SHELL
alias    显示当前所有的命令别名      alias lm="ls -al"   命令别名    unalias lm 取消命令别名
type      类似which
exprot    设置或显示环境变量
exprot PATH="$PATH":/sbin  添加/sbin入PATH路径
echo $PATH    显示PATH路径
bash      进入子程序
name=yang     设定变量
unset name    取消变量
echo $name    显示变量的内容
myname="$name its me"   &   myname='$name its me'     单引号时$name失去变量内容
ciw=/etc/sysconfig/network-scripts/     设置路径
env      列出所有环境变量
echo $RANDOM    显示随意产生的数
set      设置SHELL
PS1='[\u@\h \w \A #\#]\$ '     提示字元的設定
   [root@linux ~]# read [-pt] variable     -----------读取键盘输入的变量
   參數:
   -p  :後面可以接提示字元!
   -t  :後面可以接等待的『秒數!』
declare    声明 shell 变量
ulimit -a   显示所有限制资料
 ls /tmp/yang && echo "exist" || echo "not exist"
 意思是說,當 ls /tmp/yang 執行後,若正確,就執行echo "exist" ,若有問題,就執行echo "not exist"
 echo $PATH | cut -d ':' -f 5       以:为分隔符,读取第5段内容
 export | cut -c 10-20      读取第10到20个字节的内容
 last | grep 'root'    搜索有root的一行,加[-v]反向搜索
 cat /etc/passwd | sort    排序显示
 cat /etc/passwd | wc      显示『行、字数、字节数』
正规表示法
[root@test root]# grep [-acinv] '搜尋字串' filename
       參數說明:
       -a :將 binary 檔案以 text 檔案的方式搜尋資料
       -c :計算找到 '搜尋字串' 的次數
       -i :忽略大小寫的不同,所以大小寫視為相同
       -n :順便輸出行號
       -v :反向選擇,亦即顯示出沒有 '搜尋字串' 內容的那一行!
 grep -n 'the' 123.txt     搜索the字符 -----------搜尋特定字串      
 grep -n 't[ea]st' 123.txt    搜索test或taste两个字符---------利用 [] 來搜尋集合字元
 grep -n '[^g]oo' 123.txt     搜索前面不为g的oo-----------向選擇 [^]
 grep -n '[0-9]' 123.txt  搜索有0-9的数字
 grep -n '^the' 123.txt 搜索以the为行首-----------行首搜索^
 grep -n '^[^a-zA-Z]' 123.txt  搜索不以英文字母开头
 grep -n '[a-z]$' 123.txt    搜索以a-z结尾的行---------- 行尾搜索$
 grep -n 'g..d' 123.txt     搜索开头g结尾d字符----------任意一個字元 .
 grep -n 'ooo*' 123.txt     搜索至少有两个oo的字符---------重複字元 *
sed    文本流编辑器    利用脚本命令来处理文本文件
awd    模式扫描和处理语言
 nl 123.txt | sed '2,5d'   删除第二到第五行的内容
diff     比较文件的差异
cmp      比较两个文件是否有差异
patch    修补文件
pr       要打印的文件格式化
 
帐号管理
/etc/passwd    系统帐号信息
/etc/shadow    帐号密码信息    经MD5 32位加密
     在密码栏前面加『 * 』『 ! 』禁止使用某帐号
/etc/group     系统群组信息
/etc/gshadow
newgrp    改变登陆组
useradd  &  adduser    建立新用户  ---------> useradd -m test  自动建立用户的登入目录
          useradd -m -g pgroup test --------->指定所属级
/etc/default/useradd   相关设定
/etc/login.defs       UID/GID 有關的設定
passwd    更改密码 -----------> passwd test
usermod   修改用户帐号
userdel   删除帐号 ----------->userdel -r test
chsh      更换登陆系统时使用的SHELL   [-l]显示可用的SHELL;[-s]修改自己的SHELL
chfn      改变finger指令显示的信息
finger    查找并显示用户信息
id        显示用户的ID ----------->  id test
groupadd   添加组
groupmod   与usermod类似
groupdel   删除组
su test    更改用户   su -    进入root,且使用root的环境变量
sudo       以其他身份来执行指令
visudo     编辑/etc/sudoers      加入一行『 test ALL=(ALL) ALL 』
           %wheel ALL = (ALL) ALL               系统里所有wheel群组的用户都可用sudo
           %wheel ALL = (ALL) NOPASSWD: ALL     wheel群组所有用户都不用密码NOPASSWD
       User_Alias ADMPW = vbird, dmtsai, vbird1, vbird3         加入ADMPW组
       ADMPW ALL = NOPASSWD: !/usr/bin/passwd, /usr/bin/passwd [A-Za-z]*, \
       !/usr/bin/passwd root      可以更改使用者密码,但不能更改root密码 (在指令前面加入 ! 代表不

可)
PAM (Pluggable Authentication Modules, 嵌入式模組)
who & w     看谁在线                    
last        最近登陆主机的信息
lastlog     最近登入的時間    读取 /var/log/lastlog
talk        与其他用户交谈
write       发送信息    write test   [ctrl]+d 发送
mesg        设置终端机的写入权限    mesg n 禁止接收     mesg y
wall        向所有用户发送信息    wall this is q test
mail        写mail  
/etc/default/useradd    家目录默认设置
quota      显示磁盘已使用的空间与限制     quota -guvs ----->秀出目前 root 自己的 quota 限制值
           quota -vu   查询
quotacheck   检查磁盘的使用空间与限制     quotacheck -avug  ----->將所有的在 /etc/mtab 內,含有

quota 支援的 partition 進行掃瞄
             [-m] 强制扫描 
     quota一定要是独立的分区,要有quota.user和quota.group两件文件,在/etc/fstab添加一句:
     /dev/hda3 /home ext3 defaults,usrquota,grpquota 1 2
     chmod 600 quota*         设置完成,重启生效
edquota    编辑用户或群组的quota  [u]用户,[g]群组,[p]复制,[t]设置宽限期限
           edquota -a yang       edquota -p yang -u young ----->复制   
quotaon    开启磁盘空间限制     quotaon -auvg -------->啟動所有的具有 quota 的 filesystem
quotaoff   关闭磁盘空间限制     quotaoff -a  -------->關閉了 quota 的限制
repquota -av     查閱系統內所有的具有 quota 的 filesystem 的限值狀態
Quota 從開始準備 filesystem 的支援到整個設定結束的主要的步驟大概是:
1、設定 partition 的 filesystem 支援 quota 參數:
由於 quota 必須要讓 partition 上面的 filesystem 支援才行,一般來說, 支援度最好的是 ext2/ext3 ,
其他的 filesystem 類型鳥哥我是沒有試過啦! 啟動 filesystem 支援 quota 最簡單就是編輯 /etc/fstab


使得準備要開放的 quota 磁碟可以支援 quota 囉;
2、建立 quota 記錄檔:
剛剛前面講過,整個 quota 進行磁碟限制值記錄的檔案是 aquota.user/aquota.group,
要建立這兩個檔案就必須要先利用 quotacheck 掃瞄才行喔!
3、編輯 quota 限制值資料:
再來就是使用 edquota 來編輯每個使用者或群組的可使用空間囉;
4、重新掃瞄與啟動 quota :
設定好 quota 之後,建議可以再進行一次 quotacheck ,然後再以 quotaon 來啟動吧!
开机流程简介
1、載入 BIOS 的硬體資訊,並取得第一個開機裝置的代號;
2、讀取第一個開機裝置的 MBR 的 boot Loader (亦即是 lilo, grub, spfdisk 等等) 的開機資訊;
3、載入 Kernel 作業系統核心資訊, Kernel 開始解壓縮,並且嘗試驅動所有硬體裝置;
4、Kernel 執行 init 程式並取得 run-level 資訊;
5、init 執行 /etc/rc.d/rc.sysinit 檔案;
6、啟動核心的外掛模組 (/etc/modprobe.conf);
7、init 執行 run-level 的各個批次檔( Scripts );
8、init 執行 /etc/rc.d/rc.local 檔案;
9、執行 /bin/login 程式,並等待使用者登入;
10、登入之後開始以 Shell 控管主機。
在/etc/rc.d/rc3.d內,以S开头的为开机启动,以K开头的为关闭,接着的数字代表执行顺序
GRUB vga设定
彩度\解析度  640x480  800x600  1024x768  1280x1024   bit
    256        769      771      773       775      8 bit
   32768       784      787      790       793     15 bit
   65536       785      788      791       794     16 bit
   16.8M       786      789      792       795     32 bit
./configure    检查系统信息       ./configure --help | more  帮助信息
make clean     清除之前留下的文件
make           编译
make install   安装
rpm -q  ----->查询是否安装             rpm -ql ------>查询该套件所有的目录
rpm -qi ----->查询套件的说明资料       rpm -qc[d] ----->设定档与说明档
rpm -ivh  ---->安装                    rpm -V  -------->查看套件有否更动过
rpm -e  ------>删除                    rpm -Uvh ------->升级安装 
--nodeps ----->强行安装                --test ----->测试安装