CentOS常用命令

[安装epel源]

centos7:

# rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
或
# yum install epel-release

centos6:

#rpm -Uvh https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

[查看使用磁盘空间大的目录]

du -h --max-depth=1 |sort

[xargs使用-杀进程]

ps aux |grep -v grep |grep vsftpd|awk -F" " '{print $2}'|head -1|xargs kill -9

[xargs使用-删除log]

ls *.log.* | xargs rm -f

[exec使用-拷贝某个目录] 

#find ./ -maxdepth 3 -type d -name 2018 -exec rsync -Rav {} /data/FTP/ \;

#find ./ -maxdepth 3 -type d -name 2018 -exec rsync -av {} /data/FTP/ \;

[ntpdate常用地址] 

#ntpdate -d cn.pool.ntp.org

[压缩解压]

tar:
压缩
tar -zcf ttt.tar.gz ttt/
向压缩包添加文件

解压
tar -zxf ttt.tar.gz 
tar -zxf ttt.tar.gz -C /usr/loca/src/

zip:
压缩
zip -r filename.zip filename/
向压缩包添加文件
zip -m myfile.zip /usr/local/src/addfilename.txt

解压
unzip filename.zip
unzip filename.zip -d /usr/local/src/

[变量设置]

(1)mysql设置
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile

(2)JAVA设置
#JAVA
JAVA_HOME=/usr/local/java/jdk1.8.0_65
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH


[tar命令另类用法]

# tar -zcf www.sb.com.tar.gz $(ls | grep -v www)


[strace命令]

# top //找出CPU使用率高的进程PID
# strace -p PID //跟踪进程
# ll /proc/PID/fd //查看该进程在处理哪些文件
将有可疑的PHP代码修改之,如:file_get_contents没有设置超时时间。


[linux更改时区]

#ll /etc/localtime
#mv /etc/localtime /etc/localtime.bak
# ln -s /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
# date
# ntpdate 202.120.2.101


[更新同步]

rsync -av /r2/wangs/update_6/version_619/homed s1:/usr/local/
rsync -av /var/www/html/webFront/webFront /var/www/html/application/
把test1下的所有文件覆盖到test2下
# rsync -av test1/* test2/
把/tem/test  覆盖到  /usr/local/test
# rsync -av /tmp/test /usr/local/


[AWK用法]

# awk -F: '/root:x/ {print $1} /mysql:/ {print $1}' /etc/passwd |tr '\n' ' '

查看某个进程使用情况
#top -Hp $(ps aux | grep nginx | grep -v grep | sed 's/[\t ]\+/ /g' | cut -d ' ' -f 2|awk '{q=p;p=$0}NR>1{ORS=","; print q}END{ORS=""; print p}';echo)


[查找替换]

(1)find方法查找
find ./ -name "*.html" -exec sed -n  "/homed.me/p" {} \;
find ./ -name "*.html" -exec sed -i  "s/homed.me/bfgd.com.cn/g" {} \;
find ./ -name "*.js" -exec sed -i "s/homed.me/bfgd.com.cn/g" {} \;

(2)查找替换3位IP
查找3位IP段
#grep -nr "[1-9][0-9][0-9]\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.\*" *
#find ./ -type f -exec sed -n  "/[1-9][0-9][0-9]\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.\*/p" {} \;

替换3位ip
#find ./ -type f -exec sed -n  "s/[1-9][0-9][0-9]\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.\*/172\.18\.75\.\*/g" {} \;

(3)查找替换4位IP
查找4位IP
#grep -nr "[1-9][0-9][0-9]\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" *
#find ./ -type f -exec sed -n  "/[1-9][0-9][0-9]\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/p" {} \;

替换4位IP
#find ./ -type f -exec sed -n  "s/[1-9][0-9][0-9]\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/172\.18\.75\.167/p" {} \;

[对比两个文件,删除相同部分]

#awk '{print $0}' 1.txt 2.txt |sort|uniq -u > 3.txt


[data命令]

[root@localhost sh]# cat date.sh 
#!/bin/bash
date01=`date +%Y%m%d`
date02=`date '+%F %T'`
lastdate01=`date +%Y%m%d --date="-1 day"`
lastdate02=`date -d '-1 day' +%Y%m%d`
lastdate03=`date -d last-day +%Y%m%d`

echo "$date01"
echo "$date02"
echo "$lastdate01"
echo "$lastdate02"
echo "$lastdate03"

[root@localhost sh]# sh date.sh 
20190725
2019-07-25 10:32:28
20190724
20190724
20190724
[root@localhost sh]#


[修改系统最大连接数]

#vim /etc/security/limits.conf
*    soft    noproc    65535
*    hard    noproc    65535
*    soft    nofile    65535
*    hard    nofile    65535
修改后必须重启


[脚本调试]

Shell提供了一些用于调试脚本的选项:
-n,读一遍脚本中的命令但不执行,用于检查脚本中的语法错误。
-v,一边执行脚本,一边将执行过的脚本命令打印到标准错误输出。
-x,提供跟踪执行信息,将执行的每一条命令和结果依次打印出来。


[文件挂载]

常用挂载iso命令
#mount -o,loop /data/iso/CentOS-6.5-x86_64-bin-DVD1.iso /var/ftp/cdrom/

挂载Linux文件nfs共享:
#mount -t nfs -o rw 192.168.1.2:/usr/www /usr/www
挂载ISO镜像:
#mount -o loop -t iso9660 /usr/mydisk.iso /home/ping.bao/cd
挂载USB移动硬盘:
#mount -t ntfs /dev/sdc1 /root/usb
挂载CDROM:
#mount /dev/cdrom /home/ping.bao/cd


[分区查看]

#blkid -o list
#lsblk


[用户组]

#usermod -G new test  test加入new组

[随即生成16位密码]

#cat /dev/urandom | tr -dc a-zA-Z0-9#@^_ | head -c 16;echo

[系统层面问题排查相关命令]

1. 日志message查询

内存:page allocation failure
磁盘:dev sd

2. 硬盘资源
#iostat -d -x -k 1 10 

3. bond绑定查看网卡
# cat /proc/net/bonding/bond1


4. 查看网速
iftop


5. 排查网络连通性
iperf3

目标服务器启动(172.18.0.101):
iperf3 -s -D -4 


测试服务器测试命令(172.18.0.102):
# iperf3 -c 172.18.0.101 -4 -i 1 -t10 -b 2g -w 256k    ;写数据服务器测收带宽;正向带宽
# iperf3 -c 172.18.0.101 -4 -i 1 -t10 -b 2g -w 256k -R  ;写数据服务器测发带宽;反向带宽

7. strace命令
# top //找出CPU使用率高的进程PID
# strace -p PID //跟踪进程
# ll /proc/PID/fd //查看该进程在处理哪些文件
将有可疑的PHP代码修改之,如:file_get_contents没有设置超时时间。

8. lsof命令

// 文件相关
#lsof   /filepath/file

// 进程相关
#lsof -c mysql

//用户相关
#lsof -u www
#lsof -u mysql -c mysql

// 网络连接相关
#lsof  -i tcp
#lsof -i:3306
#lsof -i tcp:3306

9. sar命令
//查看硬盘使用情况
# sar -d 
实时 
# sar -d 1


//查看网卡流量 
全天
# sar -n DEV
实时
# sar -n DEV 1

同理:
//查看cpu
# sar -u
//查看内存
# sar -r


10. ss命令
//统计所有TCP连接
## ss -s

//查看当前TCP连接状态
## ss -atp

常用参数
-a, --all            display all sockets
-l, --listening      display listening socket
-t, --tcp            display only TCP sockets
-u, --udp            display only UDP sockets
-p, --processes      show process using socket
-s, --summary        show socket usage summary
-n, --numeric        don't resolve service names
-r, --resolve       resolve host names

[硬件信息查看] 

查看CPU相关信息
# cat /proc/cpuinfo
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 63
model name	: Intel(R) Xeon(R) CPU E5-2697 v3 @ 2.60GHz
stepping	: 2
microcode	: 0x37
cpu MHz		: 2600.000
cache size	: 35840 KB
physical id	: 0
siblings	: 14
core id		: 0
cpu cores	: 14
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 15
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb xsaveopt pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid
bogomips	: 5187.48
clflush size	: 64
cache_alignment	: 64
address sizes	: 46 bits physical, 48 bits virtual
power management:

详解:
vendor_id :CPU制造商      
cpu family :CPU产品系列代号
model   :CPU属于其系列中的哪一代的代号
model name:CPU属于的名字及其编号、标称主频
stepping   :CPU属于制作更新版本
cpu MHz   :CPU的实际使用主频
cache size   :CPU二级缓存大小
physical id   :单个CPU的标号
siblings       :单个CPU逻辑物理核数
core id        :当前物理核在其所处CPU中的编号,这个编号不一定连续
cpu cores    :该逻辑核所处CPU的物理核数
apicid          :用来区分不同逻辑核的编号,系统中每个逻辑核的此编号必然不同,此编号不一定连续
fpu             :是否具有浮点运算单元(Floating Point Unit)
fpu_exception  :是否支持浮点计算异常
cpuid level   :执行cpuid指令前,eax寄存器中的值,根据不同的值cpuid指令会返回不同的内容
wp             :表明当前CPU是否在内核态支持对用户空间的写保护(Write Protection)
flags          :当前CPU支持的功能
bogomips   :在系统内核启动时粗略测算的CPU速度(Million Instructions Per Second)
clflush size  :每次刷新缓存的大小单位
cache_alignment :缓存地址对齐单位
address sizes     :可访问地址空间位数


查看内存
# dmidecode |grep -A16 "Memory Device$"


常用命令:

# 查看物理CPU个数
cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l

# 查看每个物理CPU中core的个数(即核数)
cat /proc/cpuinfo| grep "cpu cores"| uniq

# 查看逻辑CPU的个数
cat /proc/cpuinfo| grep "processor"| wc -l

PS:
# 总核数 = 物理CPU个数 X 每颗物理CPU的核数 
# 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数


查看网卡buffer size
# ethtool -g eth0

修改网卡buffer size
# ethtool -G eth0 rx 2048
# ethtool -G eth0 tx 2048

[历史命令显示时间] 

vim /etc/profile
// 如下位置加入HISTTIMEFORMAT="%F %T "
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTTIMEFORMAT="%F %T "
HISTSIZE=1000

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值