1、linux启动过程
开启电源 --> BIOS开机自检 --> 引导程序lilo或grub --> 内核的引导(kernel boot)--> 执行init(rc.sysinit、rc)--> mingetty(建立终端) --> shell
2、网卡绑定多IP
# ifconfig eth0:1 192.168.1.99 netmask 255.255.255.0
3、设置DNS、网关
# echo "nameserver 202.16.53.68" 》 /etc/resolv.conf
# route add default gw 192.168.1.1
4、弹出、收回光驱
# eject
# eject -t
5、用date查询昨天的日期
# date --date=yesterday
6、查询file1里面空行的所在行号
# grep ^$ file
7、查询file1以abc结尾的行
# grep abc$ file1
8、打印出file1文件第1到第三行
# sed -n '1,3p' file1
# head -3 file1
9、清空文件
# true > 1.txt
# echo "" > 1.txt
# > 1.txt
# cat /dev/null > 1.txt
10、删除所有空目录
# find /data -type d -empty -exec rm -rf {} \;
11、linux下批量删除空文件(大小等于0的文件)的方法
# find /data -type f -size 0c -exec rm -rf {} \;
# find /data -type f -size 0c|xargs rm –f
12、删除五天前的文件
# find /data -mtime +5 -type f -exec rm -rf {} \;
13、删除两个文件重复的部份,打印其它
# cat 1.txt 3.txt |sort |uniq
14、攻取远程服务器主机名
# echo `ssh $IP cat /etc/sysconfig/network|awk -F = '/HOSTNAME/ {print $2}'`
15、实时监控网卡流量(安装iftop)
# /usr/local/iftop/sbin/iftop -i eth1 -n
16、查看系统版本
# lsb_release -a
17、强制踢出登陆用户
# pkill -KILL -t pts/1
18、tar增理备份、还原
# tar -g king -zcvf kerry_full.tar.gz kerry
# tar -g king -zcvf kerry_diff_1.tar.gz kerry
# tar -g king -zcvf kerry_diff_2.tar.gz kerry
# tar -zxvf kerry_full.tar.gz
# tar -zxvf kerry_diff_1.tar.gz
# tar -zxvf kerry_diff_2.tar.gz
19、将本地80端口的请求转发到8080端口,当前主机外网IP为202.96.85.46
-A PREROUTING -d 202.96.85.46 -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.9.10:8080
20、在11月份内,每天的早上6点到12点中,每隔2小时执行一次/usr/bin/httpd.sh
# crontab -e
0 6-12/2 * 11 * /usr/bin/httpd.sh
21、查看占用端口8080的进程
# netstat -tnlp | grep 8080
lsof -i:8080
22、在Shell环境下,如何查看远程Linux系统运行了多少时间?
# ssh user@被监控主机ip "uptime"
23、查看CPU使用情况的命令
""每5秒刷新一次,最右侧有CPU的占用率的数据
# vmstat 5
""top 然后按Shift+P,按照进程处理器占用率排序
# top
24、查看内存使用情况的命令
""用free命令查看内存使用情况
# free -m
""top 然后按Shift+M, 按照进程内存占用率排序
# top
25、查看磁盘i/o
""用iostat查看磁盘/dev/sdc3的磁盘i/o情况,每两秒刷新一次
# iostat -d -x /dev/sdc3 2
26、修复文件系统
# fsck –yt ext3 /
-t 指定文件系统
-y 对发现的问题自动回答yes
27、read 命令5秒后自动退出
# read -t 5
28、grep -E -P 是什么意思
-E, --extended-regexp 采用扩展正规表达式。
-P,--perl-regexp 采用perl正规表达式
29、vi编辑器(涉及到修改,添加,查找)
插入(insert)模式
i 光标前插入
I 光标行首插入
a 光标后插入
A 光标行尾插入
o 光标所在行下插入一行,行首插入
O 光标所在行上插入一行,行首插入
G 移至最后一行行首
nG 移至第n行行首
n+ 下移n行,行首
n- 上移n行,行首
:/str/ 从当前往右移动到有str的地方
str? 从当前往左移动到有str的地方
:s/str1/str2/ 将找到的第一个str1替换为str2
:s/str2/str2/g 将当前行找到的所有str1替换为str2
:n1,n2s/str1/str2/g 将从n1行至n2行找到的所有的str1替换为str2
:1,.s/str1/str2/g 将从第1行至当前行的所有str1替换为str2
:.,$s/str1/str2/g 将从当前行至最后一行的所有str1替换为str2
30、linux服务器之间相互复制文件
copy 本地文件1.sh到远程192.168.9.10服务器的/data/目录下
# scp /etc/1.sh king@192.168.9.10:/data/
copy远程192.168.9.10服务器/data/2.sh文件到本地/data/目录
# scp king@192.168.9.10:/data/2.sh /data/
31、使用sed命令把test.txt文件的第23行的TEST换成TSET.
# sed -i '23s/TEST/TSET/' test.txt
# sed -i '23 s/TEST/TSET/' test.txt
32、使history命令能显示时间
# export HISTTIMEFORMAT="%F %T "
33、如何查看目标主机192.168.0.1开放那些端口
# nmap -PS 192.168.0.1
34、如何查看网络连接
# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
35、如何查看当前系统使用了那些库文件
# ldconfig -v
36、如何查看网卡的驱动版本
# ethtool -i eth0
37、使用tcpdump来监视主机192.168.0.1的tcp的80端口
# tcpdump tcp port 80 host 192.168.0.1
38、 如何看其它用户的邮件列表
# mial -u king
39、对大文件进行切割
按每个文件1000行来分割
# split -l 1000 httperr8007.log httperr
按照每个文件5m来分割
# split -b 5m httperr8007.log httperr
40、合并文件
取出两个文件的并集(重复的行只保留一份)
# cat file1 file2 | sort | uniq
取出两个文件的交集(只留下同时存在于两个文件中的文件)
# cat file1 file2 | sort | uniq -d
删除交集,留下其他的行
# cat file1 file2 | sort | uniq –u
41、打印文本模式下运行的服务
# chkconfig --list|awk '$5~/on/{print $1,$5}'
42、删除0字节文件
# find -type f -size 0 -exec rm -rf {} \;
43、查看进程,按内存从大到小排列
# ps -e -o "%C : %p : %z : %a"|sort -k5 -nr
44、查看http的并发请求数及其TCP连接状态
# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
45、获取IP地址
# ifconfig eth0|sed -n '2p'|awk '{print $2}'|cut -c 6-30
perl实现获取IP地址:
# ifconfig -a | perl -ne 'if ( m/^\s*inet (?:addr:)?([\d.]+).*?cast/ ) { print qq($1 ); exit 0; }'
46、获取内存大小
# free -m |grep "Mem" | awk '{print $2}'
47、查看CPU核心数
# cat /proc/cpuinfo |grep -c processor
48、查看磁盘使用情况
# df -h
49、查看有多少个活动的php-cgi进程
# netstat -anp | grep php-cgi | grep ^tcp | wc -l
50、查看硬件制造商
# dmidecode -s system-product-name
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
1.删除0字节文件
find -type f -size 0 -exec rm -rf {} \;
2.查看进程
按内存从大到小排列
ps -e -o "%C : %p : %z : %a"|sort -k5 -nr
3.按cpu利用率从大到小排列
ps -e -o "%C : %p : %z : %a"|sort -nr
4.打印说cache里的URL
grep -r -a jpg /data/cache/* | strings | grep "http:" | awk -F'http:' '{print "http:"$2;}'
5.查看http的并发请求数及其TCP连接状态:
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
6. sed -i '/Root/s/no/yes/' /etc/ssh/sshd_config sed在这个文里Root的一行,匹配Root一行,将no替换成yes.
7.1.如何杀掉mysql进程:
ps aux |grep mysql |grep -v grep |awk '{print $2}' |xargs kill -9 (从中了解到awk的用途)
killall -TERM mysqld
kill -9 `cat /usr/local/apache2/logs/httpd.pid` 试试查杀进程PID
8.显示运行3级别开启的服务:
ls /etc/rc3.d/S* |cut -c 15- (从中了解到cut的用途,截取数据)
9.如何在编写SHELL显示多个信息,用EOF
cat << EOF
+--------------------------------------------------------------+
| === Welcome to Tunoff services === |
+--------------------------------------------------------------+
EOF
10. for 的巧用(如给mysql建软链接)
cd /usr/local/mysql/bin
for i in *
do ln /usr/local/mysql/bin/$i /usr/bin/$i
done
11. 取IP地址:
ifconfig eth0 |grep "inet addr:" |awk '{print $2}'|cut -c 6- 或者
ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
12.内存的大小:
free -m |grep "Mem" | awk '{print $2}'
13.
netstat -an -t | grep ":80" | grep ESTABLISHED | awk '{printf "%s %s\n",$5,$6}' | sort
14.查看Apache的并发请求数及其TCP连接状态:
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
15.因为同事要统计一下服务器下面所有的jpg的文件的大小,写了个shell给他来统计.原来用xargs实现,但他一次处理一部分,搞的有多个总和....,下面的命令就能解决啦.
find / -name *.jpg -exec wc -c {} \;|awk '{print $1}'|awk '{a+=$1}END{print a}'
CPU的数量(多核算多个CPU,cat /proc/cpuinfo |grep -c processor)越多,系统负载越低,每秒能处理的请求数也越多。
---------------------------------------------------------------------------------------
16 CPU负载 # cat /proc/loadavg
检查前三个输出值是否超过了系统逻辑CPU的4倍。
18 CPU负载 #mpstat 1 1
检查%idle是否过低(比如小于5%)
19 内存空间 # free
检查free值是否过低 也可以用 # cat /proc/meminfo
20 swap空间 # free
检查swap used值是否过高 如果swap used值过高,进一步检查swap动作是否频繁:
# vmstat 1 5
观察si和so值是否较大
21 磁盘空间 # df -h
检查是否有分区使用率(Use%)过高(比如超过90%) 如发现某个分区空间接近用尽,可以进入该分区的挂载点,用以下命令找出占用空间最多的文件或目录:
# du -cks * | sort -rn | head -n 10
22 磁盘I/O负载 # iostat -x 1 2
检查I/O使用率(%util)是否超过100%
23 网络负载 # sar -n DEV
检查网络流量(rxbyt/s, txbyt/s)是否过高
24 网络错误 # netstat -i
检查是否有网络错误(drop fifo colls carrier) 也可以用命令:# cat /proc/net/dev
25 网络连接数目 # netstat -an | grep -E “^(tcp)” | cut -c 68- | sort | uniq -c | sort -n
26 进程总数 # ps aux | wc -l
检查进程个数是否正常 (比如超过250)
27 可运行进程数目 # vmwtat 1 5
列给出的是可运行进程的数目,检查其是否超过系统逻辑CPU的4倍
28 进程 # top -id 1
观察是否有异常进程出现
29 网络状态 检查DNS, 网关等是否可以正常连通
30 用户 # who | wc -l
检查登录用户是否过多 (比如超过50个) 也可以用命令:# uptime
31 系统日志 # cat /var/log/rflogview/*errors
检查是否有异常错误记录 也可以搜寻一些异常关键字,例如:
# grep -i error /var/log/messages
# grep -i fail /var/log/messages
32 核心日志 # dmesg
检查是否有异常错误记录
33 系统时间 # date
检查系统时间是否正确
34 打开文件数目 # lsof | wc -l
检查打开文件总数是否过多
35 日志 # logwatch –print 配置/etc/log.d/logwatch.conf,将 Mailto 设置为自己的email 地址,启动mail服务(sendmail或者postfix),这样就可以每天收到日志报告了。
缺省logwatch只报告昨天的日志,可以用# logwatch –print –range all 获得所有的日志分析结果。
可以用# logwatch –print –detail high 获得更具体的日志分析结果(而不仅仅是出错日志)。
36.杀掉80端口相关的进程
lsof -i :80|grep -v "PID"|awk '{print "kill -9",$2}'|sh
37.清除僵死进程。
ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9
38.tcpdump 抓包 ,用来防止80端口被人攻击时可以分析数据
# tcpdump -c 10000 -i eth0 -n dst port 80 > /root/pkts
39.然后检查IP的重复数 并从小到大排序 注意 "-t\ +0" 中间是两个空格
# less pkts | awk {'printf $3"\n"'} | cut -d. -f 1-4 | sort | uniq -c | awk {'printf $1" "$2"\n"'} | sort -n -t\ +0
40.查看有多少个活动的php-cgi进程
netstat -anp | grep php-cgi | grep ^tcp | wc -l
chkconfig --list | awk '{if ($5=="3:on") print $1}'
41.kudzu查看网卡型号
kudzu --probe --class=network
匹配中文字符的正则表达式: [\u4e00-\u9fa5]
评注:匹配中文还真是个头疼的事,有了这个表达式就好办了
匹配双字节字符(包括汉字在内):[^\x00-\xff]
评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
匹配空白行的正则表达式:\n\s*\r
评注:可以用来删除空白行
匹配HTML标记的正则表达式:<(\S*?)[^>]*>.*?</\1>|<.*? />
评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力
匹配首尾空白字符的正则表达式:^\s*|\s*$
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
评注:表单验证时很实用
匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*
评注:网上流传的版本功能很有限,上面这个基本可以满足需求
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
评注:表单验证时很实用
匹配国内电话号码:\d{3}-\d{8}|\d{4}-\d{7}
评注:匹配形式如 0511-4405222 或 021-87888822
匹配腾讯QQ号:[1-9][0-9]{4,}
评注:腾讯QQ号从10000开始
匹配中国邮政编码:[1-9]\d{5}(?!\d)
评注:中国邮政编码为6位数字
匹配身份证:\d{15}|\d{18}
评注:中国的身份证为15位或18位
匹配ip地址:\d+\.\d+\.\d+\.\d+
评注:提取ip地址时有用
匹配特定数字:
^[1-9]\d*$ //匹配正整数
^-[1-9]\d*$ //匹配负整数
^-?[1-9]\d*$ //匹配整数
^[1-9]\d*|0$ //匹配非负整数(正整数 + 0)
^-[1-9]\d*|0$ //匹配非正整数(负整数 + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ //匹配正浮点数
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ //匹配负浮点数
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ //匹配浮点数
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ //匹配非负浮点数(正浮点数 + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ //匹配非正浮点数(负浮点数 + 0)
评注:处理大量数据时有用,具体应用时注意修正
匹配特定字符串:
^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串
评注:最基本也是最常用的一些表达式