linux系統(tǒng)維護(hù)相關(guān)命令

基礎(chǔ)命令

fdisk -l? 查看磁盤物理硬盤

df -hal ? 查看已掛載的硬盤空間使用情況

smartctl -H /dev/sda ?檢查硬盤壞道


linux下的日期格式化:

date -d '-30 day' +"%Y%m%d"


讓python支持https:

yum install openssl-devel

yum install openssl?

編譯python


查看內(nèi)核參數(shù):

sysctl -a


添加用戶

useradd -d /data/home/tom?-s /bin/sh -g group –G root tom

groupadd test1?


查找io過高的進(jìn)程:

watch -n 1 "(ps aux | awk '\$8 ~ /D/ ?{ print \$0 }')" ? ? # stat D ? ?Uninterruptible sleep (usually IO)


防火墻設(shè)置:

vi /etc/sysconfig/iptablesservice iptables restart

添加白名單:

? ? iptables -I INPUT -s 1.1.1.1 -j ACCEPT

? ? 去掉白名單:

? ? iptables -D INPUT -s 1.1.1.1?-j ACCEPT

增加黑名單:

? ? iptables -I INPUT -s 1.1.1.1?-j DROP? // -s 表示來源

去掉黑名單:

iptables -D INPUT -s 1.1.1.1 -j DROP? // 刪除

? ? iptables -I INPUT -p tcp -dport 80 -s 124.115.0.0/24 -j DROP // 添加

? ? iptables -D INPUT -p tcp -s 1.1.1.1 --dport 60020 -j DROP

? ? iptables -I INPUT -p tcp -s? 1.1.1.1 --dport 60020 -j DROP

? ? iptables -L


網(wǎng)卡設(shè)置:

vi?/etc/sysconfig/network-scripts/ifcfg-eth0

service network restart (或者用ifup)


批量遠(yuǎn)程操作:

基于sshpass(開源代碼,網(wǎng)上可以找到,本機(jī)設(shè)置StrictHostKeyChecking no),參考http://blog.csdn.net/songbohr/article/details/5610789


常用的性能檢測命令:

iostat vmstat top mpstat ?netstat lsof ?gprof(編譯選項(xiàng))strace ?opcontrol(這個單獨(dú)列)


軟件安裝卸載:

yum -y install(remove)

rpm


查找文件:

find / -name "*." -exec {} \;

find / -name "" | xargs ...

find /*/kankan/ -name "*.flv" -size +100M|xargs ls -lh

find ./ -name "*.log"?-mtime +7 -type f | xargs rm -f ? #查找超過7天的文件并刪除

find . -type f -name "*.txt" | xargs -i?cp{}? /tmp/k/ ? // xargs指定參數(shù)的位置,就是 {} 處

find . -type f -name "*.txt" | xargs -I {} cp {}? /tmp/n/ ? //同上,可以自己定義替換符?ls *.zip | xargs -n1 unzip? // xargs指定參數(shù)個數(shù)


遠(yuǎn)程拷貝文件:

scp ?/ ?nc


流量查看:

sar -n DEV 1 100 ? (每1秒 100個樣本)

iftop ?(http://www.ex-parrot.com/~pdw/iftop/

iptables:

iptables -nvL


shell下的循環(huán):

arr=("a" "b" "c");for i in ${arr[@]};do echo $i;done

for i in `grep -F "tgp#" *|awk -F ":" '{print $1}'|sort|uniq` ;?

do?

? ? ? ? echo $i

sed -i -e "s#set passwd [ eval exec "curl -s \"http://127.0.0.1:54321/\""]

spawn#set passwd [ eval exec \"curl -s \\\\\"http://127.0.0.1:54321/\\\\\"\"]\nset passwd [ eval exec "curl -s \"http://127.0.0.1:54321/\""]

spawn#g" ./${i}

? ? ? ? sed -i -e "s:send \"old_pass:send \"\$passwd:g" ./${i}?

done

#!/bin/env sh

for ((i=0;i<17;++i))

do

? ? echo $i

? ? start_pos=`expr ${i} '*' 10000`

? ? end_pos=`expr ${start_pos} + 10000`

? ? echo "select ip from table order by ip limit ${start_pos},10000;" |xargs -0 mysql -uuser?-hdbhost -DDatabase -ppassword -e? > ./${start_pos}_${end_pos}.txt

done

sed -i -e "s:^$str.*:new_line:g"

source(.) 命令

表示在當(dāng)前shell的上下文里面執(zhí)行, 如果直接運(yùn)行腳本,會fork一個新進(jìn)程,就不會改變當(dāng)前shell環(huán)境


nc命令:

服務(wù)端監(jiān)聽

nc -l -p 12134 < /svr_file ? (有些版本不用指定 -p)

客戶端:

nc svr_ip 12134 > /local_file


strace的一個例子:

strace -c -e trace=network -p 7697

strace -f 啟動進(jìn)程? // 可以跟蹤到子進(jìn)程


samba:

1.安裝

yum -y install samba

2.配置

/etc/samba/smb.conf

[global]

? ? ? ? workgroup = WORKGROUP

? ? ? ? printing = cups

? ? ? ? printcap name = cups

? ? ? ? printcap cache time = 750

? ? ? ? cups options = raw

? ? ? ? map to guest = Bad User

? ? ? ? include = /etc/samba/dhcp.conf

? ? ? ? logon path = \\%L\profiles\.msprofile

? ? ? ? logon home = \\%L\%U\.9xprofile

? ? ? ? logon drive = P:

? ? ? ? usershare allow guests = Yes

? ? ? ? add machine script = /usr/sbin/useradd ?-c Machine -d /var/lib/nobody -s /bin/false %m$

? ? ? ? domain logons = Yes

? ? ? ? domain master = Yes

? ? ? ? local master = Yes

? ? ? ? os level = 65

? ? ? ? preferred master = Yes

? ? ? ? security = user

? ? ? ? null password = yes

? ? ? ? guest account = root

[admin]

? ? ? ? comment =?

? ? ? ? inherit acls = Yes

? ? ? ? path = /home/share

? ? ? ? read only = No

? ? ? ? public = Yes

? ? ? ? guest ok = yes

3. 啟動服務(wù)

service smb restart


shell下求差集的3個方法

sort B B A | uniq -u ? ? ? ? ? ?//這個是如果A中有,B中沒有,則肯定只會出現(xiàn)一次

grep -F -f listb lista -v ? ? ? //這個是將文件listb作為一個表達(dá)式去lista中查找

comm a.txt b.txt


替換linux下文件名中的空格

find ./ -name "*.mp3" -exec rename " " "_" {} \;


共享內(nèi)存:

ipcs: 檢查共享內(nèi)存

ipcrm:刪除共享內(nèi)存的某個條目


查看所有進(jìn)程相關(guān)信息:

ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid'

ps H -eo user,pid,ppid,tid,time,%cpu,cmd --sort=%cpu | grep -v grep|grep regps -eo pid,lstart,etime | grep 目標(biāo)進(jìn)程id

ps -eo lstart,pid,args?


sed使用的幾個例子:

sed -e 's:dat:dat/lol:g' succeed_files_lol.txt > succeed_files_lol_replace.txt

sed -i -e 's:dat:dat/lol:g' succeed_files_lol.txt ?# -i 表示在文件內(nèi)直接替換

sed -in-place -e 's:dat:dat/lol:g' succeed_files_lol.txt?sed -n '6p' /usr/local/bin/caiji_mgr.log_tmp.3 ?顯示6行

find /usr/local/vpn/ -name "*.py"|xargs sed -i -e 's:c1405:twin13014:g'

sed -i -e 's:\:ujson:g' input_out_file ?#全字匹配

sed -i -e '/spawn/a test_sed_insert hahah' temp.txt ?#在匹配到spawn開頭后,添加一行test_sed_insert hahah

ip=`/sbin/ifconfig|grep -F "255.255.254.0"|awk '{print $2}'`; line=`echo "\"agentip\":\"$ip\","`; echo $line; sed -i -e "/\"btserver_is_bridge/a ${line}" /usr/local/gse/gseagent/conf/gse.conf ? 在文件中插入一行

sed -i -e "${md5lineno}{s:${md5src}:${md5}:g}" ${yaml}? ?#在指定的行數(shù)位置 進(jìn)行替換

cat /proc/sys/fs/file-nr ? #查看系統(tǒng)文件句柄數(shù)

cat /proc/sys/fs/file-max ?#系統(tǒng)最大文件句柄數(shù)


ssh的端口映射:

ssh -N -f -L $local_port:$dest_mms root\@$remote


tcpdump的例子:

tcpdump -i any ?-X -s 0 tcp and port 10089 and tcp[32:2]==0xcf00

-i any 抓取所有網(wǎng)卡

-s 0 表示顯示整個包的長度(自適應(yīng))

-X 表示 16進(jìn)制吧

-nn 表示端口號用數(shù)字顯示,不轉(zhuǎn)義?tcp[32:2]==0xcf00 ?表示從tcp包頭開始算起,32到34字節(jié)

tcpdump ip[2:2] > 100? // ip包長度大于100

tcpdump -i any -nn host XX and \(port 3089 or port 7008 or port 9000\)

tcpdump -i any 'port 10020 and ip[2:2] > 1000'


收集進(jìn)程內(nèi)存信息的腳本:

setsid watch -n 1800 "(date >> /home/root1/test_watch_mem.txt;pmap 19509|awk '{num[\$2]++}END{for(m in num)printf(\"%s %d\\n\",m,num[m])}' >>/home/root1/test_watch_mem.txt)"


綁定網(wǎng)卡中斷:

將網(wǎng)卡與cpu綁定:

1.首先查看/proc/interrupts文件,找到每個網(wǎng)卡對應(yīng)的中斷號(第一列的數(shù)字);2.修改相應(yīng)/proc/irq/中斷號/smp_affinity的值(16進(jìn)制數(shù)),只將對應(yīng)cpu的那一位置1(從最右邊開始計(jì)算),例如echo 04 > smp_affinity,即將該網(wǎng)卡綁定到cpu2上(第3個cpu,因?yàn)樗饕枏?開始)

如果網(wǎng)卡是?PCI-MSI ?或者?PCI-MSIX的,當(dāng)中斷不均衡時,可以手工綁定中斷到不同cpu,比如:

echo "2" > /proc/irq/90/smp_affinity ? 表示將 90 中斷綁定到2號cpu

“3” 二進(jìn)制11,表示1和2個cpu

“f” ?1111,表示1/2/3/4 cpu?

如果綁定要生效,需要關(guān)閉服務(wù):

service irqbalance stop?

綁定網(wǎng)卡的shell腳本:

#!/bin/sh

process_list=`ps -eaf|grep xl_proxy|grep -v grep|awk '{print $2}'`

cpu_index=0

for pid in ${process_list}

do

? ? ? ? echo "pid=${pid},cpu_index=${cpu_index},cmd=taskset -cp ${cpu_index} ${pid}"

? ? ? ? taskset -cp ${cpu_index} ${pid}

? ? ? ? cpu_index=`expr $cpu_index + 1`

? ? ? ? cpu_index=`expr $cpu_index % 8`

done


郵件字符集:

/usr/local/monitor-base/bin/sendEmail -o message-charset=zh_CN.GB18030?message-charset= 需要和系統(tǒng)的字符集一致,不然會產(chǎn)生編碼的亂碼


linux下遠(yuǎn)程批量操作:

http://sourceforge.net/projects/sshpass/files/latest/download?;

基于sshpass的腳本(go):

#!/bin/sh

id=$1

remote_cmd=$2

list_file="./1111.txt"

echo "list_file=$list_file"

ip=`grep "^$id" $list_file|head -n 1|awk '{print $1}'`

pass=`grep "^$id" $list_file|head -n 1|awk '{print $3}'`

user=`grep "^$id" $list_file|head -n 1|awk '{print $2}'`

echo "ip=$ip,user=$user,pass=$pass"

./sshpass -p "$pass" ssh ?$user@$ip "$remote_cmd"

遠(yuǎn)程命令需要按下面的方式傳進(jìn)去,不然sshpass不能解釋

echo ${remote_cmd}|xargs

./sshpass -p "$pass" ssh ?$user@$ip ?-o StrictHostKeyChecking=no

其中1111.txt的內(nèi)容:ip user ?pass ? desc


shell調(diào)試:

sh -x??


linux下上傳/下載文件

sz ?/ ?rz

// 加上這些參數(shù),避免出現(xiàn)亂碼

rz -y -e -b -O -Z

為了方便,可以在 .bash_profile里面添加:

alias myrz="rz -y -e -b -O -Z"


網(wǎng)絡(luò)tcp連接情況(比netstat快):

/usr/sbin/ss -tnoemi


linux設(shè)置tcp心跳:

# echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time ? ?#300秒沒有數(shù)據(jù),啟動檢查

# echo 30 > /proc/sys/net/ipv4/tcp_keepalive_intvl ? ? ?#每30秒檢查一次,有響應(yīng)了則停止

# echo 3?> /proc/sys/net/ipv4/tcp_keepalive_probes ?#檢查次數(shù),如果次數(shù)用完,則認(rèn)為連接斷開


使用curl命令上傳文件(模仿表單形式):

curl -F auth_appid=tgppic -F fileid=604a0b50d9d64d9592753b9aa7371fcd -F deal_flag=1 ?-F data=@test.jpg "http://ip:8080/path"


將tab替換成4個空格:

find ./ -name "*.py"|xargs sed -i "s:\t: ? ?:g"


域名分析:

dig domain


刪除日志:

35 12 * * * find /data/home/ -mtime +7 -type f -name "*.log" | xargs rm -f


shell的xargs的問題:

cmd="df -h|awk '{print $5}'|grep -v Use|awk -F'%' '{print $1}'"

echo "$cmd"|xargs echo ? ? ----?error?。。?/p>

結(jié)果為:

df -h|awk {print }|grep -v Use|awk -F% {print }

echo "$cmd"|xargs -0?echo ? ?----?正確?。。?, 當(dāng)有特殊字符時,需要帶上參數(shù) -0


用grep搜索點(diǎn)號:

grep -F "10.10" ? (用grep -e 時,點(diǎn)號會轉(zhuǎn)義)

搜索ip(正則)

grep "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" -REnoa * -r

更簡單的搜索ip的方法:

grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" target_file


防止echo轉(zhuǎn)義:

a="grep tom ./*.log"

echo $a ? ? ? ?#不加引號,會導(dǎo)致*號展開

echo "${a}" ?#加引號,防止通配符展開

echo -E "\n"? #特殊字符不進(jìn)行轉(zhuǎn)義


環(huán)境變量:

/etc/profile --全局環(huán)境變量

/etc/init.d/sshd --ssh繼承

/etc/init.d/cron --crontab繼承

可以在cront腳本里面添加:

. /etc/profile

. ~/.bash_profile?


core文件的生成規(guī)則:

cat /proc/sys/kernel/core_pattern


查看指定進(jìn)程的實(shí)時cpu占用:

/usr/bin/top -p ${pid} -bcn 1|grep lbs_conn|awk '{print $(NF-4)}'|cut -d . -f 1

/usr/bin/top -p ${pid} -bcn 1|grep lbs_conn|awk '{print $9}'|cut -d . -f 1


shell基礎(chǔ)知識(條件判斷):

文件表達(dá)式

-e filename 如果 filename存在,則為真

-d filename 如果 filename為目錄,則為真?

-f filename 如果 filename為常規(guī)文件,則為真

-L filename 如果 filename為符號鏈接,則為真

-r filename 如果 filename可讀,則為真?

-w filename 如果 filename可寫,則為真?

-x filename 如果 filename可執(zhí)行,則為真

-s filename 如果文件長度不為0,則為真

-h filename 如果文件是軟鏈接,則為真

filename1 -nt filename2 如果 filename1比 filename2新,則為真。

filename1 -ot filename2 如果 filename1比 filename2舊,則為真。

整數(shù)變量表達(dá)式

-eq 等于

-ne 不等于

-gt 大于

-ge 大于等于

-lt 小于

-le 小于等于

字符串變量表達(dá)式

If ?[ $a = $b ] ? ? ? ? ? ? ? ? 如果string1等于string2,則為真

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 字符串允許使用賦值號做等號

if ?[ $string1 != ?$string2 ] ? 如果string1不等于string2,則為真 ? ? ??

if ?[ -n $string ?] ? ? ? ? ? ? 如果string 非空(非0),返回0(true) ?

if ?[ -z $string ?] ? ? ? ? ? ? 如果string 為空,則為真

if ?[ $sting ] ? ? ? ? ? ? ? ? ?如果string 非空,返回0 (和-n類似)?

? ? 邏輯非 ! ? ? ? ? ? ? ? ? ??條件表達(dá)式的相反

if [ ! 表達(dá)式 ]

if [ ! -d $num ] ? ? ? ? ? ? ??如果不存在目錄$num

? ? 邏輯與 –a ? ? ? ? ? ? ? ? ? 條件表達(dá)式的并列

if [ 表達(dá)式1 ?–a ?表達(dá)式2 ]

? ? 邏輯或 -o ? ? ? ? ? ? ? ? ? 條件表達(dá)式的或

if [ 表達(dá)式1 ?–o 表達(dá)式2 ]


交換區(qū)的處理:

swapoff -a

dd if=/dev/zero of=/data/swapfile1bs=1024 count=2097152 ?#用zero填充目標(biāo)文件,塊大小是1024,塊個數(shù)是2097152

mkswap /data/swapfile1

swapon /data/swapfile1

cat /proc/swaps


shell下求字符串子串:

a="test.txt.dat"

echo "${a: -4}" ? 輸出為 ?.dat

echo "${a:0:4}" ?輸出為 test


簡單的加密解密文件:

加密腳本:

#! /usr/bin/expect

set src_file [lindex $argv 0]

set dst_file [lindex $argv 1]

spawn openssl enc -aes-256-cbc -in $src_file -out $dst_file?

expect {

"*assword:"

{

send "加密口令\n"

}

}

expect {

"*assword:"

{

send "加密口令\n"

}

}

interact

解密腳本:

#! /usr/bin/expect

set src_file [lindex $argv 0]

set dst_file [lindex $argv 1]

spawn openssl enc -aes-256-cbc -d -in $src_file -out $dst_file?

expect {

"*assword:"

{

send "加密口令\n"

}

}

interact

openssl aes256 -in machine_list.txt -e -pass env:DATA_PSWD > machine_list.dat


shell中的變量問題:

管道會開啟子shell,則子shell中的變量賦值不會影響到父shell?

cat?$C?|?grep?-v?'^#'?|?while?read?LINE ? ?開啟了一個子shell,

do

?????if?[?"x$A"?=?"x1"?];?then

?????????export?B=$A

?????????echo?$B

?????fi

done

在子shell中對B重新賦值是不能影響到父shell的,所以最后echo?$B時值沒有改變。

while?read?LINE

do

???if

???B=$A

???fi

done<$C

這樣是可以重新賦值的


shell中的隨機(jī)數(shù):

echo?$RANDOM

http://blog.csdn.net/fdipzone/article/details/24329523


linux下分割和合并文件:

(dd if=XXX1.tar.gz;dd if=XXX2.tar.gz)? > big1.tar.gz

split -b 8G ./XXX.log


linux下測試mtu值:

ping -c 3 -s 1472 -M do 10.1.1.1


stat文件:

Access : 文件最近一次被訪問的時間

Modify: ?文件內(nèi)容最近一次被修改的時間

Change: 文件屬性最近一次被改變的時間


用dd快速生成100M的文件:

dd if=/dev/zero of=test bs=1M count=100

dd if=/dev/zero of=test bs=1M count=0 seek=100


重啟dns緩存服務(wù)

service?nscd?restart


mysql導(dǎo)出數(shù)據(jù)

只導(dǎo)出數(shù)據(jù)庫結(jié)構(gòu)

mysqldump -hXX -P3306 -utest -pXX -d dbname > ./test.sql

?導(dǎo)出結(jié)構(gòu)和數(shù)據(jù)

mysqldump -hXX-P3306 -utest -pXX dbname> ./test_with_data.sql

導(dǎo)入數(shù)據(jù)

mysql -hXX -P3306 -utest -pXX -Ddbname < ./test_with_data.sql

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容