存档

‘Linux’ 分类的存档

ssh连接的时候出现Host key verification failed.

2013年5月1日 没有评论

今天在服务器上执行远程操作命令出现以下的问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@www ~]# ssh 192.168.1.**
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
3c:2a:3a:d5:ae:2b:76:52:*:*.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending key in /root/.ssh/known_hosts:20
RSA host key for 192.168.1.** has changed and you have requested strict checking.
Host key verification failed.

解决方法:
在正在操作的机器上执行

1
vi ~/.ssh/known_hosts

进入此目录,删除的192.168.1.**相关rsa的信息即可.
或者删除这个文件

1
2
cd ~/.ssh/
rm known_hosts

linux(centos)绑定ARP的方法

2013年4月16日 没有评论

ARP协议是“Address Resolution Protocol”(地址解析协议) 的缩写。在局域网中,网络中实际传输的是“帧”,帧里面是有目标主机的MAC地址的。在以太网中,一个主机要和另一个主机进行直接通信,必须要知道目标主 机的MAC地址。但这个目标MAC地址是如何获得的呢?它就是通过地址解析协议获得的。所谓“地址解析”就是主机在发送帧前将目标IP地址转换成目标 MAC地址的过程。ARP协议的基本功能就是通过目标设备的IP地址,查询目标设备的MAC地址,以保证通信的顺利进行。

而ARP攻击就是通过伪造IP地址和MAC地址实现ARP欺骗,能够在网络中产生大量的ARP通信量使网络阻塞,攻击者只要持续不断的发出伪造的ARP响应包就能更改目标主机ARP缓存中的IP-MAC条目,造成网络中断或中间人攻击。

ARP攻击主要是存在于局域网网络中,局域网中若有一台计算机感染ARP木马,则感染该ARP木马的系统将会试图通过“ARP欺骗”手段截获所在网络内其它计算机的通信信息,并因此造成网内其它计算机的通信故障。

在工作中经常遇到有服务器受到ARP欺骗攻击,严重影响了网络的正常运营,于是想到将网关的ARP条目进行绑定。

本文是关于linux服务器下arp网关的绑定

1、查看网关的arp信息
[root@Vtest ~]# arp
Address HWtype HWaddress Flags Mask Iface
192.168.1.103 ether 00:16:ea:27:7b:04 C eth0
192.168.1.1 ether 40:16:9f:6c:39:1e C eth0

说明:
Address: 主机的IP地址
Hwtype: 主机的硬件类型
Hwaddress:主机的硬件地址
Flags Mask:记录标志,”C”表示arp高速缓存中的动态条目,”CM”表示静态的arp条目。

我们需要绑定的就是192.168.1.1所在的一行。

2、建立一个静态IP –>>mac对应的文件
[root@Vtest ~]#echo ’192.168.1.1 40:16:9f:6c:39:1e’>>/etc/ip-mac

3、设置开机自动绑定

arp信息重启后会清空,所以需要开机的时候自动绑定。服务器启动过程最后运行的脚本是rc.local,我们把绑定的命令就加到这个文件的最后一行。

[root@Vtest ~]#echo ‘arp -f /etc/ip-mac’ /etc/rc.local

4、手动绑定一下
[root@Vtest ~]#echo arp -f /etc/ip-mac

[root@Vtest ~]#arp -s 192.168.1.1 40:16:9f:6c:39:1e
5、验证
[root@Vtest ~]# arp -a
(192.168.1.103) at 00:16:ea:27:7b:04 [ether] on eth0
(192.168.1.1) at 40:16:9f:6c:39:1e [ether] PERM on eth0
[root@Vtest ~]# arp
Address HWtype HWaddress Flags Mask Iface
192.168.1.103 ether 00:16:ea:27:7b:04 C eth0
192.168.1.1 ether 40:16:9f:6c:39:1e CM eth0

从以上显示的信息来看,已经绑定成功了,第一个命令结果中的”PERM”表示Permanent,永久的意思,即绑定成永久arp条目。到这里绑定就完成了,你的网关arp绑定了么,为了稳定运行,赶快绑定吧。

6、这只能防住一些arp攻击,如果将网内所有ip mac导入ethers文件,能有效的防止arp攻击
安装nmap:

rpm -vhU http://nmap.org/dist/nmap-6.25-1.i386.rpm
rpm -vhU http://nmap.org/dist/zenmap-6.25-1.noarch.rpm
rpm -vhU http://nmap.org/dist/ncat-6.25-1.i386.rpm
rpm -vhU http://nmap.org/dist/nping-0.6.25-1.i386.rpm
扫描同网段所有ip
nmap -sP 192.168.1.0/24 2>&1 | tee ip.log
扫描,结果我的arp表里就有那个网段所有机器的mac,将所有信息复制至/etc/ip-mac文件

使用htaccess实现域名跳转

2013年3月31日 没有评论

要使用域名的统一,即使得vps12.com转向www.vps12.com,有很多办法,听说对于seo也有些好处!

1
2
3
RewriteEngine on
RewriteCond %{HTTP_HOST} ^vps12.com [NC]
RewriteRule ^(.*)$ http://www.vps12.com/$1 [L,R=301]

还可以利用重定向功能实现url的跳转,例如:

1
2
3
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.abc.com [NC]
RewriteRule ^(.*)$ http://www.vps12.com/$1 [L,R=301]

KVM快照管理(转)

2013年3月25日 没有评论

######## 注意:对KVM快照进行管理需要在关机机状态下 ###########
说明:
kvm默认的文件格式是raw,也就是使用文件来模拟实际的硬盘(当然也可以使用一块真实的硬盘或一个分区),不过raw格式只支持内存快照(就是启动的时候加-snapshot,所有更改都写到内存),重启之后就没了。raw格式不支持磁盘快照,因此要使用qcow或qcow2文件格式。

1. 查看镜像文件格式
[root@PLASPACSHCN14 images]# qemu-img info EBS-6.img
image: EBS-6.img
file format: raw
virtual size: 300G (322122547200 bytes)
disk size: 300G
说明: 现在的文件格式是raw,需要转换成qcow2格式。

2. 转换文件格式(把raw转换成qcow2格式)
[root@PLASPACSHCN14 images]# cd /var/lib/libvirt/images
[root@PLASPACSHCN14 images]# qemu-img convert -f raw -O qcow2 EBS-6.img EBS-6_qcow2.img

3. 查看当前目录下的文件:
[root@PLASPACSHCN14 images]# ls
EBS-3.img EBS-6.img EBS-6_qcow2.img EBS-7.img
[root@PLASPACSHCN14 images]# du -sh *
301G EBS-3.img
301G EBS-6.img
2.5G EBS-6_qcow2.img
2.5G EBS-6-qcow2.img
401G EBS-7.img
说明:可见,多出来了个文件EBS-6_qcow2.img。

4. 为虚拟机创建快照:
4.1 一定不要开机快照!
首先需要关闭虚拟机,然后按照下面的命令进行快照。最后恢复快照的时候先关机在恢复。
[root@PLASPACSHCN14 images]# qemu-img snapshot -c snapshot01 EBS-6_qcow2.img
说明: qemu-img snapshot -c snapshot01 EBS-6_qcow2.img
命令 快照参数 参数 快照的命令 镜像的名字,(为那个镜像创建的快照)

5. 列出镜像的所有快照
[root@PLASPACSHCN14 images]# qemu-img snapshot -l EBS-6_qcow2.img
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 snapshot01 0 2012-12-27 14:53:07 00:00:00.000

6. 恢复快照:
[root@PLASPACSHCN14 images]# qemu-img snapshot -a snapshot01 EBS-6_qcow2.img
说明:qemu-img snapshot -a snapshot01 EBS-6_qcow2.img
命令 快照命令 快照参数 快照名称 镜像名称

7. 删除快照:
[root@PLASPACSHCN14 images]# qemu-img snapshot -d snapshot01 EBS-6_qcow2.img
说明: qemu-img snapshot -d snapshot01 EBS-6_qcow2.img
命令 命令参数 命令参数 快照名称 镜像名称
8. 后记:
使用KVM时很复杂的,还是选择Vmware能方便一些,对于快照这块,需要提前创建qcow格式的镜像文件:qemu-img create -f qcow2 vdisk.img 50G,然后再进入图形化进行系统安装。(快照最好使用virsh创建快照,qemu-img快照出来的文件只是0KB,所以建议使用virsh快照。)

kvm快照应用 (转载)

kvm也具有快速恢复的方法,前提是必须处于关机状态才可以执行,否则会出现各种莫名其妙的问题

创建镜像:
qemu-img snapshot -c initial smokeping_falcon_test0917.qcow2
恢复镜像:
qemu-img snapshot -a initial smokeping_falcon_test0917.qcow2
删除镜像
qemu-img snapshot -d initial smokeping_falcon_test0917.qcow2
状态查看
qemu-img snapshot -l smokeping_falcon_test0917.qcow2

创建前的大小
[root@smokeping]# ll
total 7578488
-rw-r–r– 1 root root 171825168384 Sep 17 17:01 smokeping_falcon_test0917.xml
-rw-r–r– 1 root root 171825168384 Sep 17 16:01 smokeping_falcon_test_187.qcow2
-rw-r–r– 1 root root 171825168384 Sep 6 11:14 smokeping_falcon_test.qcow2
以下是创建后的文件
[root@smokeping]# ll
total 7603284
-rw-r–r– 1 qemu qemu 171850530816 Sep 17 17:22 smokeping_falcon_test0917.qcow2
-rw-r–r– 1 root root 171825168384 Sep 17 16:01 smokeping_falcon_test_187.qcow2
-rw-r–r– 1 root root 171825168384 Sep 6 11:14 smokeping_falcon_test.qcow2

查看镜像方法1
[root@smokeping]# qemu-img info smokeping_falcon_test0917.qcow2
image: smokeping_falcon_test0917.qcow2
file format: qcow2
virtual size: 160G (171798691840 bytes)
disk size: 2.2G
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 initial 0 2012-09-17 17:08:49 00:00:00.000
查看镜像方法2
[root@smokeping]# qemu-img snapshot -l smokeping_falcon_test0917.qcow2
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 initial 0 2012-09-17 17:08:49 00:00:00.000

利用这个镜像,可以迅速还原服务器状态,使用空间也不大。

实测中,若虚拟机为启动状态制作快照,恢复后会无法载入系统,关闭服务器,再次启动,服务器直接崩溃。

scp断点续传和wget自动断点续传的方法

2013年3月23日 没有评论

1. scp断点续传
scp一旦出错中断就只能重新开始,不过可以利用rsync实现scp的断点续传
1、在~/.bashrc中加入一个alias:
$vim ~/.bashrc
alias rscp=’rsync -v -P -e ssh’
2、重新载入.bashrc配置
$source ~/.bashrc

2. wget自动断点续传的方法
有时候我们使用wget下载东西被迫打断, 比如网络故障, 终端意外断开, 忘了加”&”放入该台等等.
沉稳的人或许会想到重新开启wget, 并使用 -c断点续传, 可是有时候, 因为N个g的东西突然断了, 一冲动就晕了, 就纠结了, 可能就会就重新跑wget了, 甚至截止到本文还有人不知道-c.
那么为了杯具不再发生, 我们可以让wget每次运行的时候自动加上 -c参数, 那样就万事大吉了嘛. 方法如下:
在家目录下新建文件”.wgetrc”, 内容如下:
continue = on
写入wget.sh后,可以这样同时下载多个文件:
1.nohup wget http://www.vps12.com/software/db_must.tar.gz &
2.nohup wget http://www.vps12.com/software/root_must.tar.gz &
3.nohup wget http://www.vps12.com/software/webserver_must.tar.gz &
如果想不用IP,但有两个相同的域名,则需要配置/etc/hosts加和IP对应的域名,再Wget即可。
保存退出, 以后再使用wget就会自动断点续传了。

利用Shell脚本来监控Linux系统的负载、CPU、内存、硬盘、用户登录数

2013年3月5日 没有评论

利用Shell脚本来监控Linux系统的负载、CPU、内存、硬盘、用户登录数。

这几天在学习研究shell脚本,写的一些系统负载、CPU、内存、硬盘、用户数监控脚本程序。在没有nagios监控的情况下,只要服务器能上互联网,就可通过发邮件的方式来提醒管理员系统资源的使用情况。

一、编写linux系统告警邮件脚本

1
# vim /scripts/sys-warning.sh
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
95
#!/bin/bash
#监控系统负载与CPU、内存、硬盘、登录用户数,超出警戒值则发邮件告警。
 
#提取本服务器的IP地址信息
IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "`
 
# 1、监控系统负载的变化情况,超出时发邮件告警:
 
#抓取cpu的总核数
cpu_num=`grep -c 'model name' /proc/cpuinfo`
 
#抓取当前系统15分钟的平均负载值
load_15=`uptime | awk '{print $12}'`
 
#计算当前系统单个核心15分钟的平均负载值,结果小于1.0时前面个位数补0。
average_load=`echo "scale=2;a=$load_15/$cpu_num;if(length(a)==scale(a)) print 0;print a" | bc`
 
#取上面平均负载值的个位整数
average_int=`echo $average_load | cut -f 1 -d "."`
 
#设置系统单个核心15分钟的平均负载的告警值为0.70(即使用超过70%的时候告警)。
load_warn=0.70
 
#当单个核心15分钟的平均负载值大于等于1.0(即个位整数大于0) ,直接发邮件告警;如果小于1.0则进行二次比较
if (($average_int > 0)); then
echo "$IP服务器15分钟的系统平均负载为$average_load,超过警戒值1.0,请立即处理!!!" | mutt -s "$IP 服务器系统负载严重告警!!!" test@126.com
else
 
#当前系统15分钟平均负载值与告警值进行比较(当大于告警值0.70时会返回1,小于时会返回0 )
load_now=`expr $average_load \> $load_warn`
 
#如果系统单个核心15分钟的平均负载值大于告警值0.70(返回值为1),则发邮件给管理员
if (($load_now == 1)); then
echo "$IP服务器15分钟的系统平均负载达到 $average_load,超过警戒值0.70,请及时处理。" | mutt -s "$IP 服务器系统负载告警" test@126.com
fi
 
fi
 
# 2、监控系统cpu的情况,当使用超过80%的时候发告警邮件:
 
#取当前空闲cpu百份比值(只取整数部分)
cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d "."`
 
#设置空闲cpu的告警值为20%,如果当前cpu使用超过80%(即剩余小于20%),立即发邮件告警
if (($cpu_idle < 20)); then
echo "$IP服务器cpu剩余$cpu_idle%,使用率已经超过80%,请及时处理。" | mutt -s "$IP 服务器CPU告警" test@126.com
fi
 
# 3、监控系统交换分区swap的情况,当使用超过80%的时候发告警邮件:
 
#系统分配的交换分区总量
swap_total=`free -m | grep Swap | awk '{print $2}'`
 
#当前剩余的交换分区free大小
swap_free=`free -m | grep Swap | awk '{print $4}'`
 
#当前已使用的交换分区used大小
swap_used=`free -m | grep Swap | awk '{print $3}'`
 
if (($swap_used != 0)); then
#如果交换分区已被使用,则计算当前剩余交换分区free所占总量的百分比,用小数来表示,要在小数点前面补一个整数位0
swap_per=0`echo "scale=2;$swap_free/$swap_total" | bc`
 
#设置交换分区的告警值为20%(即使用超过80%的时候告警)。
swap_warn=0.20
 
#当前剩余交换分区百分比与告警值进行比较(当大于告警值(即剩余20%以上)时会返回1,小于(即剩余不足20%)时会返回0 )
swap_now=`expr $swap_per \> $swap_warn`
 
#如果当前交换分区使用超过80%(即剩余小于20%,上面的返回值等于0),立即发邮件告警
if (($swap_now == 0)); then
echo "$IP服务器swap交换分区只剩下 $swap_free M 未使用,剩余不足20%,使用率已经超过80%,请及时处理。" | mutt -s "$IP 服务器内存告警" test@126.com
fi
 
fi
 
# 4、监控系统硬盘根分区使用的情况,当使用超过80%的时候发告警邮件:
 
#取当前根分区(/dev/sda3)已用的百份比值(只取整数部分)
disk_sda3=`df -h | grep /dev/sda3 | awk '{print $5}' | cut -f 1 -d "%"`
 
#设置空闲硬盘容量的告警值为80%,如果当前硬盘使用超过80%,立即发邮件告警
if (($disk_sda3 > 80)); then
echo "$IP 服务器 /根分区 使用率已经超过80%,请及时处理。" | mutt -s "$IP 服务器硬盘告警" test@126.com
fi
 
#5、监控系统用户登录的情况,当用户数超过3个的时候发告警邮件:
 
#取当前用户登录数(只取数值部分)
users=`uptime | awk '{print $6}'`
 
#设置登录用户数的告警值为3个,如果当前用户数超过3个,立即发邮件告警
if (($users >= 3)); then
echo "$IP 服务器用户数已经达到$users个,请及时处理。" | mutt -s "$IP 服务器用户数告警" test@126.com
fi
1
# chmod a+x /scripts/sys-warning.sh

二、加入任务计划:每十分钟检测一次,有告警则立即发邮件(十分钟发一次)。

1
2
3
# crontab -e
*/10 * * * *  /scripts/sys-warning.sh 
# service crond restart

三、要实现服务器能够发邮件,须开启Sendmail服务或是安装linux下面的一个邮件客户端msmtp软件(类似于一个foxmail的工具)

1、下载安装:http://downloads.sourceforge.net/msmtp/msmtp-1.4.16.tar.bz2?modtime=1217206451&big_mirror=0

1
2
3
4
5
# tar jxvf msmtp-1.4.16.tar.bz2
# cd msmtp-1.4.16
# ./configure --prefix=/usr/local/msmtp
# make
# make install

2、创建msmtp配置文件和日志文件(host为邮件域名,邮件用户名test,密码123456)

1
2
3
4
5
6
7
8
9
10
# vim ~/.msmtprc
account default  
host 126.com
from test@126.com
auth login
user test
password 123456
logfile ~/.msmtp.log
# chmod 600  ~/.msmtprc
# touch ~/.msmtp.log

3、mutt安装配置:(一般linux下有默认安装mutt)

1
2
3
4
5
6
7
set sendmail="/usr/local/msmtp/bin/msmtp"
set use_from=yes
set realname="memory"
set from=test@126.com
set envelope_from=yes
set rfc2047_parameters=yes
set charset="utf-8"

4、邮件发送测试(-s邮件标题)   # echo “邮件内容123456″ | mutt -s “邮件标题测试邮件”   test@126.com

解决vi在ubuntu及其他unix等系统中使用不正常的方法

2013年2月26日 没有评论

解决ubuntu中vi不能正常使用方向键与退格键的问题

方案一:
问题:
ubuntu中vi在编辑状态下方向键不能用,还有回格键不能删除等我们平时习惯的一些键都不能使用。

解决办法:
可以安装vim full版本,在full版本下键盘正常,安装好后同样使用vi命令。
安装vim:
ubuntu预装的是vim tiny版本,而需要的是vim full版本。执行下面的语句安装vim full版本:
$sudo apt-get remove vim-common
$sudo apt-get install vim
这样就行了,vim是一款优秀的软件,可以在运用中体会到。

方案二:
在vi命令行使用命令“:set nocompatible”,就是设置vi不使用兼容模式。

ubuntu11.10下的VI命令:
vi 有三种模式,输入模式,编辑模式,“:”命令模式
vi 进入以后默认是编辑模式
vi 编辑模式默认的快捷键 上下左右分别是 J K H L
vi 在编辑模式使用 i 可以进入输入模式
vi 输入模式只能输入英文,默认不能使用上下左右箭头
vi 输入模式用Esc可以返回到编辑模式
vi 编辑模式 Shift + ; 可以进入命令模式
vi 命令模式w保存,q退出

1)对所有用户都有效,解决系统中所用用户的vi中文乱码问题

sudo gedit /etc/vim/vimrc.tiny

加入如下内容:

#该文件的内容显示如下:

” Encoding related

set encoding=UTF-8

set langmenu=zh_CN.UTF-8

language message zh_CN.UTF-8

set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1

set fileencoding=utf-8
2)只对当前用户有效的解决方法

ubuntu默认在当前用户的家目录下,没有.vimrc文件,这个时候可以自己touch一个.vimrc文件,且在其中加入 www.2cto.com

#该文件的内容显示如下:

” Encoding related

set encoding=UTF-8

set langmenu=zh_CN.UTF-8

language message zh_CN.UTF-8

set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1

set fileencoding=utf-8

ubuntu服务管理器sysv-rc-conf

2013年2月15日 没有评论

Linux的各大发行版,都有些不必要的服务被默认开启了,针对ubuntu,我们可以采用选择性关闭的方法加速起动,提高系统性能。

centos使用ntsys那么ubuntu我们这样:

我们安装一个软件:

1
sudo apt-get install sysv-rc-conf

然后运行:

1
sudo sysv-rc-conf

Linux Top 命令解析

2013年1月20日 没有评论

TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序为止.比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示系统中CPU最“敏感”的任务列表.该命令可以按CPU使用.内存使用和执行时间对任务进行排序;而且该命令的很多特性都可以通过交互式命令或者在个人定制文件中进行设定.

top – 12:38:33 up 50 days, 23:15,  7 users,  load average: 60.58, 61.14, 61.22

Tasks: 203 total,  60 running, 139 sleeping,   4 stopped,   0 zombie

Cpu(s)  : 27.0%us, 73.0%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

Mem:   1939780k total,  1375280k used,   564500k free,   109680k buffers

Swap:  4401800k total,   497456k used,  3904344k free,   848712k cached

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND

4338 oracle    25   0  627m 209m 207m R    0 11.0 297:14.76 oracle

4267 oracle    25   0  626m 144m 143m R    6  7.6  89:16.62 oracle

3458 oracle    25   0  672m 133m 124m R    0  7.1   1283:08 oracle

3478 oracle    25   0  672m 124m 115m R    0  6.6   1272:30 oracle

3395 oracle    25   0  672m 122m 113m R    0  6.5   1270:03 oracle

3480 oracle    25   0  672m 122m 109m R    8  6.4   1274:13 oracle

3399 oracle    25   0  672m 121m 110m R    0  6.4   1279:37 oracle

4261 oracle    25   0  634m 100m  99m R    0  5.3  86:13.90 oracle

25737 oracle    25   0  632m  81m  74m R    0  4.3 272:35.42 oracle

7072 oracle    25   0  626m  72m  71m R    0  3.8   6:35.68 oracle

16073 oracle    25   0  630m  68m  63m R    8  3.6 175:20.36 oracle

16140 oracle    25   0  630m  66m  60m R    0  3.5 175:13.42 oracle

16122 oracle    25   0  630m  66m  60m R    0  3.5 176:47.73 oracle

786 oracle    25   0  627m  63m  63m R    0  3.4   1:54.93 oracle

4271 oracle    25   0  627m  59m  58m R    8  3.1  86:09.64 oracle

4273 oracle    25   0  627m  57m  56m R    8  3.0  84:38.20 oracle

22670 oracle    25   0  626m  50m  49m R    0  2.7  84:55.82 oracle

一.  TOP前五行统计信息

统计信息区前五行是系统整体的统计信息。

1. 第一行是任务队列信息

同 uptime  命令的执行结果:

[root@localhost ~]# uptime

13:22:30 up 8 min,  4 users,  load average: 0.14, 0.38, 0.25

其内容如下:

12:38:33

当前时间

up 50days

系统运行时间,格式为时:分

1 user

当前登录用户数

load average: 0.06, 0.60, 0.48

系统负载,即任务队列的平均长度。 三个数值分别为  1分钟、5分钟、15分钟前到现在的平均值。

2. 第二、三行为进程和CPU的信息

当有多个CPU时,这些内容可能会超过两行。内容如下:

Tasks: 29 total

进程总数

1 running

正在运行的进程数

28 sleeping

睡眠的进程数

0 stopped

停止的进程数

0 zombie

僵尸进程数

Cpu(s): 0.3% us

用户空间占用CPU百分比

1.0% sy

内核空间占用CPU百分比

0.0% ni

用户进程空间内改变过优先级的进程占用CPU百分比

98.7% id

空闲CPU百分比

0.0% wa

等待输入输出的CPU时间百分比

0.0% hi

0.0% si

3. 第四五行为内存信息。

内容如下:

Mem: 191272k total

物理内存总量

173656k used

使用的物理内存总量

17616k free

空闲内存总量

22052k buffers

用作内核缓存的内存量

Swap: 192772k total

交换区总量

0k used

使用的交换区总量

192772k free

空闲交换区总量

123988k cached

缓冲的交换区总量。 内存中的内容被换出到交换区,而后又被换入到内存,但使用过的交换区尚未被覆盖, 该数值即为这些内容已存在于内存中的交换区的大小。相应的内存再次被换出时可不必再对交换区写入。

二.  进程信息

列名 含义
PID 进程id
PPID 父进程id
RUSER Real user name
UID 进程所有者的用户id
USER 进程所有者的用户名
GROUP 进程所有者的组名
TTY 启动进程的终端名。不是从终端启动的进程则显示为 ?
PR 优先级
NI nice值。负值表示高优先级,正值表示低优先级
P 最后使用的CPU,仅在多CPU环境下有意义
%CPU 上次更新到现在的CPU时间占用百分比
TIME 进程使用的CPU时间总计,单位秒
TIME+ 进程使用的CPU时间总计,单位1/100秒
%MEM 进程使用的物理内存百分比
VIRT 进程使用的虚拟内存总量,单位kb。VIRT=SWAP+RES
SWAP 进程使用的虚拟内存中,被换出的大小,单位kb。
RES 进程使用的、未被换出的物理内存大小,单位kb。RES=CODE+DATA
CODE 可执行代码占用的物理内存大小,单位kb
DATA 可执行代码以外的部分(数据段+栈)占用的物理内存大小,单位kb
SHR 共享内存大小,单位kb
nFLT 页面错误次数
nDRT 最后一次写入到现在,被修改过的页面数。
S 进程状态。
D=不可中断的睡眠状态
R=运行
S=睡眠
T=跟踪/停止
Z=僵尸进程
COMMAND 命令名/命令行
WCHAN 若该进程在睡眠,则显示睡眠中的系统函数名
Flags 任务标志,参考 sched.h

top 的man 命令解释如下:

Listed below are top’s available fields.  They are always associated with the  letter  shown,  regardless  of the position you may have established for them with the ’o’ (Order fields) interactive command.Any field is selectable as the sort field, and you control whether they are  sorted high-to-low  or  low-to-high.   For  additional  information on sort provisions see  topic 3c. TASK Area Commands.

a: PID  –  Process Id

The task’s unique process ID, which periodically wraps, though never  restarting at zero.

b: PPID  –  Parent Process Pid

The process ID of a task’s parent.

c: RUSER  –  Real User Name

The real user name of the task’s owner.

d: UID  –  User Id

The effective user ID of the task’s owner.

e: USER  –  User Name

The effective user name of the task’s owner.

f: GROUP  –  Group Name

The effective group name of the task’s owner.

g: TTY  –  Controlling Tty

The  name of the controlling terminal.  This is usually the device (serial port, pty, etc.) from which the process was started, and which it uses  for  input  oroutput.   However,  a task need not be associated with a terminal, in which case you’ll see ‘?’ displayed.

h: PR  –  Priority

The priority of the task.

i: NI  –  Nice value

The nice value of the task.   A  negative  nice  value  means  higher  priority, whereas  a  positive nice value means lower priority.  Zero in this field simply means priority will not be adjusted in determining a task’s dispatchability.

j: P  –  Last used CPU (SMP)

A number representing the last used processor.  In a true SMP  environment  this will likely change frequently since the kernel intentionally uses weak affinity. Also, the very act of running top may break this weak affinity  and  cause  more processes  to change CPUs more often (because of the extra demand for cpu time).

k: %CPU  –  CPU usage

The task’s share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.  In a true SMP environment, if ‘Irix mode’ is Off, top will operate in ‘Solaris mode’ where a task’s cpu usage will be divided by  the  total  number  of  CPUs.   You toggle ‘Irix/Solaris’ modes with the ‘I’ interactive command.

l: TIME  –  CPU Time

Total CPU time the task has used since it started.  When  ’Cumulative  mode’  is On,  each  process is listed with the cpu time that it and its dead children has used.  You toggle ‘Cumulative mode’ with ‘S’, which is a command-line option and an interactive command.  See the ‘S’ interactive command for additional information regarding this mode.

m: TIME+  –  CPU Time, hundredths

The same as ‘TIME’, but reflecting more granularity through hundredths of a sec          ond.

n: %MEM  –  Memory usage (RES)

A task’s currently used share of available physical memory.

o: VIRT  –  Virtual Image (kb)

The total amount of virtual memory used by the task.  It includes all code, data and shared libraries plus pages that have  been  swapped  out.  (Note:  you  can define  the STATSIZE=1 environment variable and the VIRT will be calculated from the /proc/#/state VmSize field.)

VIRT = SWAP + RES.

p: SWAP  –  Swapped size (kb)

The swapped out portion of a task’s total virtual memory image.

q: RES  –  Resident size (kb)

The non-swapped physical memory a task has used.

RES = CODE + DATA.

r: CODE  –  Code size (kb)

The amount of physical memory devoted to executable  code,  also  known  as  the’text resident set’ size or TRS.

s: DATA  –  Data+Stack size (kb)

The  amount of physical memory devoted to other than executable code, also known the ‘data resident set’ size or DRS.

t: SHR  –  Shared Mem size (kb)

The amount of shared memory used by a task.   It  simply  reflects  memory  that could be potentially shared with other processes.

u: nFLT  –  Page Fault count

The  number  of  major  page faults that have occurred for a task.  A page fault occurs when a process attempts to read from or write to a virtual page  that  is not  currently  present  in  its address space.  A major page fault is when disk access is involved in making that page available.

v: nDRT  –  Dirty Pages count

The number of pages that have been modified since  they  were  last  written  to disk.   Dirty  pages  must  be written to disk before the corresponding physical memory location can be used for some other virtual page.

w: S  –  Process Status

The status of the task which can be one of:

‘D’ = uninterruptible sleep

‘R’ = running

‘S’ = sleeping

‘T’ = traced or stopped

‘Z’ = zombie

Tasks shown as running should be more properly thought of as ‘ready to run’  –their  task_struct is simply represented on the Linux run-queue.  Even without a true SMP machine, you may see numerous tasks in this state  depending  on  top’s delay interval and nice value.

x: Command  –  Command line or Program name

Display the command line used to start a task or the name of the associated program.  You toggle between command line and name with ‘c’, which is both  a  command-line option and an interactive command. When  you’ve  chosen  to display command lines, processes without a command line (like kernel threads) will be shown with only the program name  in  parentheses, as in this example:                ( mdrecoveryd ) Either  form  of  display is subject to potential truncation if it’s too long to fit in this field’s  current  width.   That  width  depends  upon  other  fields  selected, their order and the current screen width.

Note: The ‘Command’ field/column is unique, in that it is not fixed-width.  When displayed, this column will be allocated all remaining screen width (up  to  the maximum  512  characters)  to  provide for the potential growth of program names into command lines.

y: WCHAN  –  Sleeping in Function

Depending on the availability of the kernel link map (‘System.map’), this  field will  show  the  name or the address of the kernel function in which the task is currently sleeping.  Running tasks will display a dash (‘-’) in this column.

Note: By displaying this field, top’s own working set will be increased by  over 700Kb.   Your  only  means of reducing that overhead will be to stop and restart          top.

z: Flags  –  Task Flags

This column represents the task’s current scheduling flags which  are  expressed in  hexadecimal  notation and with zeros suppressed.  These flags are officially documented in <linux/sched.h>.  Less formal documentation can also be  found  on the ‘Fields select’ and ‘Order fields’ screens.

默认情况下仅显示比较重要的  PID、USER、PR、NI、VIRT、RES、SHR、S、%CPU、%MEM、TIME+、COMMAND  列。

2.1 用快捷键更改显示内容。
(1)更改显示内容通过 f键可以选择显示的内容。

按 f 键之后会显示列的列表,按 a-z  即可显示或隐藏对应的列,最后按回车键确定。

(2)按o键可以改变列的显示顺序。

按小写的 a-z 可以将相应的列向右移动,而大写的 A-Z  可以将相应的列向左移动。最后按回车键确定。

按大写的 F 或 O 键,然后按 a-z 可以将进程按照相应的列进行排序。而大写的  R 键可以将当前的排序倒转。

设置完按回车返回界面。

三.  命令使用

详细内容可以参考MAN 帮助文档。这里列举部分内容:

命令格式:

top [-] [d] [p] [q] [c] [C] [S]    [n]

参数说明:

d:  指定每两次屏幕信息刷新之间的时间间隔。当然用户可以使用s交互命令来改变之。

p:  通过指定监控进程ID来仅仅监控某个进程的状态。

q:该选项将使top没有任何延迟的进行刷新。如果调用程序有超级用户权限,那么top将以尽可能高的优先级运行。

S: 指定累计模式

s : 使top命令在安全模式中运行。这将去除交互命令所带来的潜在危险。

i:  使top不显示任何闲置或者僵死进程。

c:  显示整个命令行而不只是显示命令名

在top命令的显示窗口,我们还可以输入以下字母,进行一些交互:

帮助文档如下:

Help for Interactive Commands – procps version 3.2.7

Window 1:Def: Cumulative mode Off.  System: Delay 4.0 secs; Secure mode Off.

Z,B       Global: ‘Z’ change color mappings; ‘B’ disable/enable bold

l,t,m     Toggle Summaries: ‘l’ load avg; ‘t’ task/cpu stats; ‘m’ mem info

1,I       Toggle SMP view: ’1′ single/separate states; ‘I’ Irix/Solaris mode

f,o     . Fields/Columns: ‘f’ add or remove; ‘o’ change display order

F or O  . Select sort field

<,>     . Move sort field: ‘<’ next col left; ‘>’ next col right

R,H     . Toggle: ‘R’ normal/reverse sort; ‘H’ show threads

c,i,S   . Toggle: ‘c’ cmd name/line; ‘i’ idle tasks; ‘S’ cumulative time

x,y     . Toggle highlights: ‘x’ sort field; ‘y’ running tasks

z,b     . Toggle: ‘z’ color/mono; ‘b’ bold/reverse (only if ‘x’ or ‘y’)

u       . Show specific user only

n or #  . Set maximum tasks displayed

k,r       Manipulate tasks: ‘k’ kill; ‘r’ renice

d or s    Set update interval

W         Write configuration file

q         Quit

( commands shown with ‘.’ require a visible task display window )

Press ‘h’ or ‘?’ for help with Windows,

h或者?  : 显示帮助画面,给出一些简短的命令总结说明。

k  :终止一个进程。系统将提示用户输入需要终止的进程PID,以及需要发送给该进程什么样的信号。一般的终止进程可以使用15信号;如果不能正常结束那就使用信号9强制结束该进程。默认值是信号15。在安全模式中此命令被屏蔽。

i:忽略闲置和僵死进程。这是一个开关式命令。

q:  退出程序。

r:  重新安排一个进程的优先级别。系统提示用户输入需要改变的进程PID以及需要设置的进程优先级值。输入一个正值将使优先级降低,反之则可以使该进程拥有更高的优先权。默认值是10。

S:切换到累计模式。

s :  改变两次刷新之间的延迟时间。系统将提示用户输入新的时间,单位为s。如果有小数,就换算成ms。输入0值则系统将不断刷新,默认值是5 s。需要注意的是如果设置太小的时间,很可能会引起不断刷新,从而根本来不及看清显示的情况,而且系统负载也会大大增加。

f或者F :从当前显示中添加或者删除项目。

o或者O  :改变显示项目的顺序。

l: 切换显示平均负载和启动时间信息。即显示影藏第一行

m: 切换显示内存信息。即显示影藏内存行

t : 切换显示进程和CPU状态信息。即显示影藏CPU行

c:  切换显示命令名称和完整命令行。 显示完整的命令。 这个功能很有用。

M : 根据驻留内存大小进行排序。

P:根据CPU使用百分比大小进行排序。

T: 根据时间/累计时间进行排序。

W:  将当前设置写入~/.toprc文件中。这是写top配置文件的推荐方法。

分类: Linux 标签: , , , ,

yum安装crontab

2012年12月12日 没有评论

crontab 是linux下的计划任务服务程序。有时我们需要定时做一些备份之类的需要使用到。最简化安装时有时未安装上。我们用yum来完成安装。

1、安装

1
2
yum -y install vixie-cron
yum -y install crontabs

2、启动

1
2
3
service crond restart
Stopping crond:                                            [  OK  ]
Starting crond:                                            [  OK  ]

启动成功说明安装完毕。

分类: Linux 标签: , , , , ,