存档

文章标签 ‘Linux’

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 标签: , , , , ,

Ubuntu给网卡增加和删除IP地址

2012年12月11日 没有评论

永久修改
修改/etc/network/interfaces

1
vim /etc/network/interfaces
1
2
3
4
5
6
7
8
9
10
auto eth0
iface eth0 inet static
    address 172.16.3.123
    netmask 255.255.255.0
    gateway 172.16.3.1
 
auto eth0:1
iface eth0:1 inet static
    address 10.16.3.123
    netmask 255.255.0.0

然后使用以下命令重启网卡配置并生效。

1
ifup eth0:1

注:不要使用/etc/init.d/network restart的方法!!

动态修改方法1
#网卡上增加一个IP:

1
ifconfig eth0:1 192.168.0.1 netmask 255.255.255.0

#删除网卡的第二个IP地址:

1
ip addr del 192.168.0.1 dev eth0

这种方式增加的虚拟IP,可以通过ifconfig查看

动态修改方法2
另一种增加虚拟IP的方法(ifconfig查看不到):
增加虚拟IP:

1
ip -f inet addr add 192.168.146.229/32 brd 192.168.146.229 dev eth0

查看虚拟IP:

1
ip -f inet addr

删除虚拟IP:

1
ip -f inet addr delete 192.168.146.229/32 brd 192.168.146.229 dev eth0
分类: Linux 标签: , , , , ,

使用wget下载需要用户名和密码访问的网站资源

2012年12月11日 没有评论

wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上。它有以下功能和特点:

(1)支持断点下传功能;这一点,也是网络蚂蚁和FlashGet当年最大的卖点,现在,Wget也可以使用此功能,那些网络不是太好的用户可以放心了;
(2)同时支持FTP和HTTP下载方式;尽管现在大部分软件可以使用HTTP方式下载,但是,有些时候,仍然需要使用FTP方式下载软件;
(3)支持代理服务器;对安全强度很高的系统而言,一般不会将自己的系统直接暴露在互联网上,所以,支持代理是下载软件必须有的功能;
(4)设置方便简单;可能,习惯图形界面的用户已经不是太习惯命令行了,但是,命令行在设置上其实有更多的优点,最少,鼠标可以少点很多次,也不要担心是否错点鼠标;
(5)程序小,完全免费;程序小可以考虑不计,因为现在的硬盘实在太大了;完全免费就不得不考虑了,即使网络上有很多所谓的免费软件,但是,这些软件的广告却不是我们喜欢的;

wget虽然功能强大,但是使用起来还是比较简单的,基本的语法是:wget [参数列表] URL。下面就结合具体的例子来说明一下wget的用法。
1、下载整个http或者ftp站点。
wget http://place.your.url/here
这个命令可以将http://place.your.url/here 首页下载下来。使用-x会强制建立服务器上一模一样的目录,如果使用-nd参数,那么服务器上下载的所有内容都会加到本地当前目录。

wget -r http://place.your.url/here
这个命令会按照递归的方法,下载服务器上所有的目录和文件,实质就是下载整个网站。这个命令一定要小心使用,因为在下载的时候,被下载网站指向的所有地址同样会被下载,因此,如果这个网站引用了其他网站,那么被引用的网站也会被下载下来!基于这个原因,这个参数不常用。可以用-l number参数来指定下载的层次。例如只下载两层,那么使用-l 2。

要是您想制作镜像站点,那么可以使用-m参数,例如:wget -m http://place.your.url/here
这时wget会自动判断合适的参数来制作镜像站点。此时,wget会登录到服务器上,读入robots.txt并按robots.txt的规定来执行。

2、断点续传。
当文件特别大或者网络特别慢的时候,往往一个文件还没有下载完,连接就已经被切断,此时就需要断点续传。wget的断点续传是自动的,只需要使用-c参数,例如:
wget -c http://the.url.of/incomplete/file
使用断点续传要求服务器支持断点续传。-t参数表示重试次数,例如需要重试100次,那么就写-t 100,如果设成-t 0,那么表示无穷次重试,直到连接成功。-T参数表示超时等待时间,例如-T 120,表示等待120秒连接不上就算超时。

3、批量下载。
如果有多个文件需要下载,那么可以生成一个文件,把每个文件的URL写一行,例如生成文件download.txt,然后用命令:wget -i download.txt
这样就会把download.txt里面列出的每个URL都下载下来。(如果列的是文件就下载文件,如果列的是网站,那么下载首页)

4、选择性的下载。
可以指定让wget只下载一类文件,或者不下载什么文件。例如:
wget -m –reject=gif http://target.web.site/subdirectory
表示下载http://target.web.site/subdirectory,但是忽略gif文件。–accept=LIST 可以接受的文件类型,–reject=LIST拒绝接受的文件类型。

5、密码和认证。
wget只能处理利用用户名/密码方式限制访问的网站,可以利用两个参数:
–http-user=USER设置HTTP用户
–http-passwd=PASS设置HTTP密码
对于需要证书做认证的网站,就只能利用其他下载工具了,例如curl。

6、利用代理服务器进行下载。
如果用户的网络需要经过代理服务器,那么可以让wget通过代理服务器进行文件的下载。此时需要在当前用户的目录下创建一个.wgetrc文件。文件中可以设置代理服务器:
http-proxy = 111.111.111.111:8080
ftp-proxy = 111.111.111.111:8080
分别表示http的代理服务器和ftp的代理服务器。如果代理服务器需要密码则使用:
–proxy-user=USER设置代理用户
–proxy-passwd=PASS设置代理密码
这两个参数。
使用参数–proxy=on/off 使用或者关闭代理。
wget还有很多有用的功能,需要用户去挖掘。

附录:

命令格式:
wget [参数列表] [目标软件、网页的网址]

-V,–version 显示软件版本号然后退出;
-h,–help显示软件帮助信息;
-e,–execute=COMMAND 执行一个 “.wgetrc”命令

-o,–output-file=FILE 将软件输出信息保存到文件;
-a,–append-output=FILE将软件输出信息追加到文件;
-d,–debug显示输出信息;
-q,–quiet 不显示输出信息;
-i,–input-file=FILE 从文件中取得URL;

-t,–tries=NUMBER 是否下载次数(0表示无穷次)
-O –output-document=FILE下载文件保存为别的文件名
-nc, –no-clobber 不要覆盖已经存在的文件
-N,–timestamping只下载比本地新的文件
-T,–timeout=SECONDS 设置超时时间
-Y,–proxy=on/off 关闭代理

-nd,–no-directories 不建立目录
-x,–force-directories 强制建立目录

–http-user=USER设置HTTP用户
–http-passwd=PASS设置HTTP密码
–proxy-user=USER设置代理用户
–proxy-passwd=PASS设置代理密码

-r,–recursive 下载整个网站、目录(小心使用)
-l,–level=NUMBER 下载层次

-A,–accept=LIST 可以接受的文件类型
-R,–reject=LIST拒绝接受的文件类型
-D,–domains=LIST可以接受的域名
–exclude-domains=LIST拒绝的域名
-L,–relative 下载关联链接
–follow-ftp 只下载FTP链接
-H,–span-hosts 可以下载外面的主机
-I,–include-directories=LIST允许的目录
-X,–exclude-directories=LIST 拒绝的目录

中文文档名在平常的情况下会被编码, 但是在 –cut-dirs 时又是正常的,
wget -r -np -nH –cut-dirs=3 ftp://host/test/
测试.txt
wget -r -np -nH -nd ftp://host/test/
%B4%FA%B8%D5.txt
wget “ftp://host/test/*”
%B4%FA%B8%D5.txt

由於不知名的原因,可能是为了避开特殊档名, wget 会自动将抓取档名的部分用 encode_string 处理过, 所以该 patch 就把被 encode_string 处理成 “%3A” 这种东西, 用 decode_string 还原成 “:”, 并套用在目录与档案名称的部分,decode_string 是 wget 内建的函式。

常用:

如何使用wget下载一个需要用户名和密码的ftp服务上的文件 .linux下载文件命令、wget通过shell运行FTP Wget实现上传下载,如果用wget下载有密码的ftp链接地址呢?

(1)wget ftp://vps12com@192.168.0.1/myfile.txt –ftp-password=123456

(2)wget ftp://vps12com:123456@192.168.0.1/myfile.txt 注意如果密码是123456@321,此时只能使用1和3.

(3)wget ftp://192.168.0.1/myfile.txt –ftp-user=piaohailin –ftp-password=123456

Linux禁止root帐号直接登录

2012年11月4日 没有评论

Linux禁止root帐号直接登录
Linux的默认管理员名即是root,只需要知道ROOT密码即可直接登录SSH。禁止Root从SSH直接登录可以提高服务器安全性。经过以下操作后即可实现。
本文适用于CentOS、Debian等Linux系统。

一、新建帐户

1
useradd newuser

SSH执行以上命令,可以创建名为“newuser”的帐号,可以自定义。

二、设置帐户密码

1
passwd newuser

使用passwd命令即可给相应帐户设置或修改密码。

三、不允许root直接登陆
1、修改相关文件

1
vi /etc/ssh/sshd_config

SSH执行以上命令,修改sshd_config文件

2、禁止root登陆
查找“#PermitRootLogin yes”,将前面的“#”去掉,短尾“Yes”改为“No”,并保存文件。

四、下次登陆
1、先使用新建账号“newuser”以普通用户登陆。
2、若要获得ROOT权限,在SSH中执行以下命令

1
su root

执行以上命令并输入root密码后即可获得root权限。

Smartmontools – Linux中监控硬碟的健康状态及温度告警

2012年11月3日 没有评论

Smartmontools – Linux中监控硬碟的健康状态及温度告警

Linux当中没有很多方便又有图形化的监控工具,大部分的监控可以透过SNMP或是其他工具辅助,像我通常都使用单颗硬碟居多,硬碟的健康状况总是得偶尔关心一下,而且老实说我真的没有在意过Linux的硬碟温度,还真是糟糕…..不过既然得知了有方便的工具,当然得使用看看,多关心硬碟一点,寿命久一点!

「Smartmontools」在Linux当中可以透过yum来安装即可,我要安装的同时才发现我系统内原本就有这个套件,之前都没有用到还真是小小浪费了~来看看除了文件档之外有哪些档案呢?

1
2
3
4
5
6
$ rpm -ql smartmontools
/etc/rc.d/init.d/smartd
/etc/smartd.conf
/etc/sysconfig/smartmontools
/usr/sbin/smartctl
/usr/sbin/smartd

原来这工具就是系统内一直都没开啟使用的「smartd」服务啊^^a,一直以来我都没有开啟过,一直以来都只开起【必要开啟的服务】,其他的能省则省。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ smartctl -a /dev/sdb3
smartctl version 5.38 [i686-redhat-linux-gnu] Copyright (C) 2002-8 Bruce Allen
Home page is http://smartmontools.sourceforge.net/
 
=== START OF INFORMATION SECTION ===
Device Model: ST31000333AS
Serial Number: 9TE10MBY
Firmware Version: CC1H
User Capacity: 1,000,203,804,160 bytes
Device is: Not in smartctl database [for details use: -P showall]
ATA Version is: 8
ATA Standard is: ATA-8-ACS revision 4
Local Time is: Tue Feb 8 14:25:49 2011 CST
SMART support is: Available - device has SMART capability.
SMART support is: Disabled(未啟动HDD SMART)
 
SMART Disabled. Use option -s with argument 'on' to enable it.(未啟动服务)

啟动服务后就会有一堆硬碟资讯出现了~

真的还不少,展开来看吧! more..

资讯真的是多到眼花撩乱,若不是有特殊的监控项目,还真的不知道该如何著手,而且我的Linux是架设在ESXi上,系统的硬碟其实是看不到SMART的资讯,自己【Mapping Physical Disk to vmdk】的硬碟才可以看的到SMART的讯息,ESXi的硬碟可能还是得从ESXi的方面著手。而我主要要观察的项目為温度,在上方资讯要观察的项目是194这个项次。

自己下指令或是写script观察温度是可行的,不过既然smartd是一个服务,不妨看看设定档,果然有监控的选项即设定,当发生问题时可以主动发信告知,也可以将一些纪录写入syslog,日后有需要查询时也方便许多。仔细查看/etc/smartd.conf的设定档,发现可以监控的项目不少,而且预设的范例都有写在设定档内,若有一些进阶需求,可以翻翻设定档或许会有解答,而我目前只改了一些基本的设定。

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
# 全域设定寄发邮件的对象
DEVICESCAN -H -m 电子信箱
 
# 每小时纪录194、231、9的讯息到syslog
DEVICESCAN -I 194 -I 231 -I 9
 
# Monitor all attributes except normalized Temperature (usually 194),
# but track Temperature changes &gt;= 4 Celsius, report Temperatures
# &gt;= 45 Celsius and changes in Raw value of Reallocated_Sector_Ct (5).
# Send mail on SMART failures or when Temperature is &gt;= 55 Celsius.
# 应该是温度改变超过4度,温度大於45、55度告警,以及磁区故障系统自动配置备用磁区超过5时告警
/dev/sdb -a -I 194 -W 4,45,55 -R 5 -m 电子信箱
 
# 默默的检查,SMART发生异常时才发出通知
/dev/sdb -H -C 0 -U 0 -m 电子信箱
 
# 更多的设定选项如下
-d TYPE Set the device type: ata, scsi, marvell, removable, 3ware,N, hpt,L/M/N
-T TYPE set the tolerance to one of: normal, permissive
-o VAL Enable/disable automatic offline tests (on/off)
-S VAL Enable/disable attribute autosave (on/off)
-n MODE No check. MODE is one of: never, sleep, standby, idle
-H Monitor SMART Health Status, report if failed
-l TYPE Monitor SMART log. Type is one of: error, selftest
-f Monitor for failure of any 'Usage' Attributes
-m ADD Send warning email to ADD for -H, -l error, -l selftest, and -f
-M TYPE Modify email warning behavior (see man page)
-s REGE Start self-test when type/date matches regular expression (see man page)
-p Report changes in 'Prefailure' Normalized Attributes
-u Report changes in 'Usage' Normalized Attributes
-t Equivalent to -p and -u Directives
-r ID Also report Raw values of Attribute ID with -p, -u or -t
-R ID Track changes in Attribute ID Raw value with -p, -u or -t
-i ID Ignore Attribute ID for -f Directive
-I ID Ignore Attribute ID for -p, -u or -t Directive
-C ID Report if Current Pending Sector count non-zero
-U ID Report if Offline Uncorrectable count non-zero
-W D,I,C Monitor Temperature D)ifference, I)nformal limit, C)ritical limit
-v N,ST Modifies labeling of Attribute N (see man page)
-a Default: equivalent to -H -f -t -l error -l selftest -C 197 -U 198
-F TYPE Use firmware bug workaround. Type is one of: none, samsung
-P TYPE Drive-specific presets: use, ignore, show, showall

大致上我就只有设定那麼一些些,主要还是藉由服务帮忙监控,若是需要更简单的方式监控硬碟温度,可以设定「hddtemp」的服务,不过能侦测的硬碟有限,snmp的资讯似乎也没温度的相关讯息,所以暂时就是简单的设定告警萝!

分类: Linux 标签: , ,

如何选择Linux发行版:CentOS、Debian、Ubuntu之异同

2012年11月2日 没有评论

Linux有非常多的发行版本,从性质上划分,大体分为由商业公司维护的商业版本与由开源社区维护的免费发行版本。

商业版本以Redhat为代表,开源社区版本则以debian为代表。这些版本各有不同的特点,在不同的应用领域发挥着不同的作用,不能一概而论。而绝大多数VPS上只提供开源社区维护的发行版本。下面就这些不同的Linux发行版进行简单的分析。

Ubuntu

Ubuntu近些年的粉丝越来越多,Ubuntu有着漂亮的用户界面,完善的包管理系统,强大的软件源支持,丰富的技术社区,Ubuntu还对大多数硬件有着良好的兼容性,包括最新的图形显卡等等。这一切让Ubuntu越来越向大众化方向发展。但别忘了:你所需要的只是一个简约、稳定、易用的服务器系统而已!

Ubuntu的图形界面固然漂亮,但这也决定了它最佳的应用领域是桌面操作系统而非服务器操作系统。如何你希望在学习Linux的过程中有个沉浸式的环境,那么Ubuntu的确不错:仅仅安装在自己的电脑中而非服务器中。从这一点来讲,Ubuntu并没有在VPS安装的操作系统选择之列,相信你也不会为了那数百M的驱动与宝贵的内存买单。

CentOS

你会发现非常多的商业公司部署在生产环境上的服务器都是使用的CentOS系统,CentOS是从RHEL源代码编译的社区重新发布版。CentOS简约,命令行下的人性化做得比较好,稳定,有着强大的英文文档与开发社区的支持。与Redhat有着相同的渊源。虽然不单独提供商业支持,但往往可以从Redhat中找到一丝线索。相对debian来说,CentOS略显体积大一点。是一个非常成熟的Linux发行版。

Debian

一般来说Debian作为适合于服务器的操作系统,它比Ubuntu要稳定得多。可以说稳定得无与伦比了。debian整个系统,只要应用层面不出现逻辑缺陷,基本上固若金汤,是个常年不需要重启的系统(当然,这是夸张了点,但并没有夸大其稳定性)。debian整个系统基础核心非常小,不仅稳定,而且占用硬盘空间小,占用内存小。

分类: Linux 标签: , ,

如何解决Ubuntu上压缩包乱码问题

2012年10月16日 没有评论

安装Linux版7zip:

1
sudo apt-get install p7zip p7zip-full p7zip-rar

卸载可能安装过的rar相关包(默认是没有安装的):

1
sudo apt-get remove rar unrar

经过这样操作,以后解压rar和zip文件时,file-roller(归档管理器)自动会调用7z来解压,
而7z能识别中文编码,不会出现乱码,解压和压缩都通过图形程序file-roller操作,完全不用涉及命令行。

如果不卸载掉rar,file-roller默认使用rar解压,而rar不能识别中文编码,所以可能会出现乱码。
当然你也就不能在Linux上创建rar文件了,但有必要打包成rar格式吗?

unrar虽然能识别中文编码,但它和rar一样不是自由软件,它们都是WinRAR那家公司的商业软件,
既然7z能解压rar文件,我又何必用它呢?

总之file-roller默认使用7z解压7z/rar/zip,创建7z/zip包时也是使用7z,所以统一使用7z格式
能保证自己的压缩包不会乱码。在Linux创建7z包时,也可以像Windows版那样加密压缩包目录。

分类: Linux, 软件使用 标签: , ,

Linux VPS Centos挂载未使用硬盘教程

2012年9月23日 没有评论

用户挂载未使用硬盘,默认装完系统只装载了10G,如果你购买了更多需要自行挂载才可以。
需要使用ROOT登陆SSH进行操作,使用PUTTY或XSHELL之类软件,或面板的VNC控制也可以。
注: 挂载硬盘过程请按照下面代码复制执行既可,不同Linux系统,只需途径名称修改对应名称。

一、 首先查看一下,当前空间情况:

1
fdisk -l

LinuxVPS   14G硬盘,系统盘8589M,空余6442M是我们今天要挂载的目标:

二、 查看一下当前已划分空间使用情况,总5.7G,使用1.8G,余3.7G:

1
df -hal

三、 增加分区,并进行相关操作,并重启VPS:

1
fdisk /dev/xvdb

接下来按顺序输入以下内容,#后为说明:
n #新建分区
p #主分区
3 #第3个分区
回车 #默认即可
回车 #默认即可
t #指定分区格式
8e #分区格式为LVM
w #保存

1
reboot #重启VPS

四、 再查看,就会发现新增加了一个刚才分区的内容:

五、 创建物理卷及加入组:

1
2
pvcreate /dev/xvdb3
vgextend VolGroup00 /dev/xvdb3

六、 查看物理卷情况,得到需要增加的空间容量:

1
vgdisplay

七、 按照大小增加(略小于实际容量,例如实际5.97G,增加5.95G即可)

1
lvresize -L +19.95G /dev/VolGroup00/LogVol00

八、 动态扩容分区大小:

1
resize2fs /dev/VolGroup00/LogVol00

九、 再次查看空间容量,发现已经加上了:

1
df -hal

IPv4网络掩码对照表

2012年7月21日 没有评论
序号 子网掩码 CIDR 总IP数 可用IP数 C网子网数
1 255.255.255.255 /32 1 1 1/256
2 255.255.255.254 /31 2 0 1/128
3 255.255.255.252 /30 4 2 1/64
4 255.255.255.248 /29 8 6 1/32
5 255.255.255.240 /28 16 14 1/16
6 255.255.255.224 /27 32 30 1/8
7 255.255.255.192 /26 64 62 1/4
8 255.255.255.128 /25 128 126 1/2
9 255.255.255.0 /24 256 254 1
10 255.255.254.0 /23 512 510 2
11 255.255.252.0 /22 1024 1022 4
12 255.255.248.0 /21 2048 2046 8
13 255.255.240.0 /20 4096 4094 16
14 255.255.224.0 /19 8192 8190 32
15 255.255.192.0 /18 16,384 16,382 64
16 255.255.128.0 /17 32,768 32,766 128
17 255.255.0.0 /16 65,536 65,534 256
18 255.254.0.0 /15 131,072 131,070 512
19 255.252.0.0 /14 262,144 262,142 1024
20 255.248.0.0 /13 524,288 524,286 2048
21 255.240.0.0 /12 1,048,576 1,048,574 4096
22 255.224.0.0 /11 2,097,152 2,097,150 8192
23 255.192.0.0 /10 4,194,304 4,194,302 16,384
24 255.128.0.0 /9 8,388,608 8,388,606 32,768
25 255.0.0.0 /8 16,777,216 16,777,214 65,536
26 254.0.0.0 /7 33,554,432 33,554,430 131,072
27 252.0.0.0 /6 67,108,864 67,108,862 262,144
28 248.0.0.0 /5 134,217,728 134,217,726 1,048,576
29 240.0.0.0 /4 268,435,456 268,435,454 2,097,152
30 224.0.0.0 /3 536,870,912 536,870,910 4,194,304
31 192.0.0.0 /2 1,073,741,824 1,073,741,822 8,388,608
32 128.0.0.0 /1 2,147,483,648 2,147,483,646 16,777,216
33 0.0.0.0 /0 4,294,967,296 4,294,967,294 33,554,432