Linux系統伺服器維護常用命令集合

來源:互聯網
上載者:User

下面是本人在使用學習linux伺服器是的一些常用的伺服器維護常用命令集合,希望此命令對各位有所協助。

 

修改時區為東8區、上海

1 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
修改主機名稱

1 #需要改下邊這兩個檔案: 

2 vim /etc/hosts 

3 vim /etc/sysconfig/network
查看伺服器每IP的串連數,用於檢測DDOS

1 netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n 

2 2 199.87.229.20 

3 3 120.43.25.94 

4 3 218.30.103.142 

5 3 61.135.169.47 

6 4 110.81.18.140 

7 8 59.58.136.94 

8 20 192.151.148.226 

9 47 219.157.16.254
T人,把其他人從伺服器中踢出去

1 w #查看誰線上 

2  15:44:27 up 22 days,  7:16,  1 user,  load average: 0.02, 0.09, 0.10 

3 USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT 

4 root     pts/0    192.161.177.103  15:40    0.00s  0.03s  0.00s w 

5 pkill -KILL -t pts/0 #踢了這個終端(現在是T自己,^_^)
修改檔案的時間戳記

1 touch -c -t 0801010800 filename.c 

2 #將檔案的時間戳記設定為2008-01-01 8:00,格式為(YYMMDDhhmm)。
批量將 .conf 檔案重新命名為 .conf.bak

1 cd /etc/httpd/webSite/ 

2 for i in $(ls *.conf);do mv $i ${i/.conf/.conf.bak};done

3 # 因使用了ls命令,注意目前的目錄問題
一鍵安裝Apache+PHP+MySQL

1 yum -y install httpd php mysql mysql-server php-mysql httpd-manual mod_ssl mod_perl mod_auth_mysql php-mcrypt php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc mysql-connector-odbc mysql-devel libdbi-dbd-mysql
添加使用者並設定密碼

1 useradd -s /bin/nologin -m -d /var/www/website/new_user_name -g newUserGroupName newUserName 

2 echo `pwdBuilder newUserName`| passwd newUserName --stdin 

3 # 添加一個網站的使用者,禁止登入,家目錄為網站根目錄,根據pwdBuilder計算密碼
查看目前的目錄下的一級目錄個數

1 ls -l |grep "^d"|wc -l
vi 刪除空行

1 :g/^s*$/d 

2 # Dreamweaver可以將“rns*rn”以正則方式替換為“rn”
尋找目錄中的檔案中所含有指定字串的檔案

1 find ./ -name '*.conf' | xargs grep -in "baidu.com.demo.upall.cn"

2 # 註:“baidu.com.demo.upall.cn” 是要尋找的字串 

3 # 這個命令用於在大批的設定檔中快速定位到某網域名稱的設定檔名稱,因為: 

4 # 因特殊原因導致該使用者的設定檔名不符合規則或一個設定檔中有多個不相似的網域名稱 

5 #   i:不區分大小寫 

6 #   n:顯示所在行號
查看單個檔案(或檔案夾)大小

1 du -sh /var/log/
查看檔案夾中各檔案夾、檔案的大小(區分哪個檔案夾最大)

1 du -h --max-depth=1 /var/log/
刪除源碼中的^M

1 :%s/^M$//g #注意:^M的輸入方式,Ctrl+V+M
批量去除include及其下檔案的寫入權限

1 find /var/www -type d -name "include" -print0 | xargs -0 chmod ugo-w -R 

2 # 去除執行許可權可以看這篇文章:/?p=697
尋找當前檔案夾中包含字串“fsockopen”的所有檔案
其實尋找“udp://$”的話會精準一些,當然也要看實際情況,^_^。


1 find ./ -type f | xargs grep -sni "fsockopen"

2 # 或者(下邊這行協助理解“-sni”參數) 

3 find ./ -type f | xargs grep --silent --line-number --ignore-case "fsockopen"

4 # 如果找出的檔案全部可以刪除的話,可以用下邊這條命令來刪除: 

5 find ./ -type f | xargs grep -lsi "fsockopen" | xargs rm
查看網域名稱的MX記錄是否生效

1 [upall@linux ~]$ host -t mx upall.cn 

2 upall.cn mail is handled by 10 mx.upall.cn.
列出所有網站檔案夾中 zip 的檔案,並顯示其大小
經常會在伺服器上下載一些源碼包或者打包一些備份檔案之後忘記刪除,這個命令可以找出它們。


1 find /www/ -type f -name "*.zip" -print0 | xargs -0 du -h
刪除使用者及其檔案(刪除使用者登入目錄以及目錄中所有檔案)

1 userdel -r upall
查看指定連接埠啟動並執行程式

1 # 查看3428連接埠啟動並執行程式: 

2 lsof -i :3428 

3 # 關閉進程可以(9是終止訊號等級): 

4 killall -9 firefox
修改 /etc/aliases 後更新 /etc/aliases.db 資料庫

1 newaliases
禁用停用鎖定使用者、恢複啟用使用者

1 passwd -l upall #禁用使用者upall 

2 passwd -u upall #啟用使用者upall
清除UTF-8中的BOM頭

1 grep -r $'xEFxBBxBF' * |grep .php 

2 #或者直接配置vim:set nobomb
CentOS yum安裝Apache + PHP + MySQL + Tomcat

1 #yum -y install httpd php mysql mysql-server php-mysql httpd-manual mod_ssl mod_perl mod_auth_mysql php-mcrypt php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc mysql-connector-odbc mysql-devel libdbi-dbd-mysql
替換MySQL欄位中的字串

1 UPDATE `tableName` 

2 SET `fieldName` = replace (`fieldName`,'fromString','toString') 

3 WHERE `fieldName` LIKE '%fromString%'; 

4 - tableName 表的名字 

5 - fieldName 欄位名 

6 - fromString 需要替換的字串 

7 - toString 替換成的字串
將檔案複製到符合規則的目錄中(一個檔案到多個目錄)

1 # 複製檔案到路徑中包含“base/images”的目錄中: 

2 find . -type d -print | grep "/base/images" | awk '{print $1}' | xargs -I % cp ./logo.gif % 

3 # 複製檔案到所有目錄中: 

4 find . -type d -exec cp ./logo.gif {} ; 

5 # 複製檔案到路徑中包含“images”的目錄中(name參數值不能有“/”,若有則需要“grep來配合”): 

6 find . -type d -name "images" -exec cp ./logo.gif {} ;
<完>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.