1. 程式人生 > >Linux server quick cheat sheet

Linux server quick cheat sheet

系統管理:

  1.  shell命令優先查詢順序:

    1. ./  

    2. /usr/local/bin 

    3. /usr/bin 

    4. /bin 

    5. /sbin

  2. 恢復模式:開機時按cms+R 

  3. ps / kill: (ps快速參考)

    1. ps -e  

          show all process by  PID | TTY  name/command

    2. ps -ax : list all processes with a tty(x) 檢視哪些tty在執行

    3. ps -au list user information

    4. ps -eLF 顯示所有執行緒(上述方法不顯示有些執行緒)

    5. kill [signal, -9/-SIGKILL by default]  pid     

          kill by pid

    6. pkill [-g pgrep ] name       kill process by name    

    7. killall name                        kill processed by name

  4. renice:

    1. 調整優先順序PRI,renice將一個offset加到PRI上,

    2. PRI越小則優先順序越高。

  5. who: 檢視活躍使用者名稱|tty名|登入ip

  6. sshd:

    1. xos中,須修改 etc/ssh/sshd_config, 宣告好金鑰、協議、安全、連線數等。再將公鑰新增到.ssh/known_hosts 

      否則每次都要新生成所有key,然後新增已知hosts

    2. 必須全名呼叫   

  7. sudo -u root ‘cp xxx yyy’ 以root身份執行字串命令

  8. mount: 檢視磁碟掛載到哪裡

  9. top:程序佔用資源監控

  10. htop:top高階版

  11. iotop:帶磁碟讀寫監控的top

  12. iostat:專門監控磁碟讀寫

  13. vmstat:虛擬記憶體用量存量

  14. isof:is_opened_file 列出所有被開啟的檔案資源

  15. tcpdump:監控TCP/IP包

  16. netstat:監控TCP/IP連線 

    1. netstat -tlunp | grep port 埠被誰佔用

    2. tulnp:(tcp udp listen numeric program)

  17. iftop/nethogs:監控程式的網路吞吐

  18. psacct/acct:監控其他使用者

  19. suricata:網路安全軟體,OISF開源網路安全基金會開發

  20. ext3grep:  rm * 恢復命令 mysql binlog

  21. write username tty 傳送給user訊息 crtl+D 或 ctrl+C 結束

  22. wall :  write to all users on server

  23. rwall : write to all users on network   [danger!]

  24. talk [user]: live chat with [user]

  25. lsb_release 檢視發行版名稱

  26. lspci

  27. lscpu

  28. Ctrl+Shift+C=複製,Ctrl+Shift+V=貼上

  29. 編輯當前行的命令:

    - ctrl-w刪除當前命令的一個單詞,

    - ctrl-u刪除到行首,ctrl-k刪除行尾,

    - ctrl-a移動到行首,ctrl-e移動到行尾

    - ctrl-x ctrl-e 可以呼叫自己定義的編輯器來編輯當前命令列

  30. 控制任務前後臺

    jobs 檢視後臺任務編號

    ctrl z 將任務暫停,回到bash

    fg $no 將no號任務放到前臺

    bg $no 放到後臺

    disown $no 將任務與session脫鉤,關閉term後不會結束任務

    nohup 後臺執行,不與term掛鉤.

  31. 系統CPU 網路 硬碟監控:blog

  32. file 命令判斷so檔案x64/32位數

  33. free -m  檢視記憶體  MB單位   或者直接看檔案: /proc/meminfo

  1. $1

  2. [email protected] param list  Self_name Excluded

  3. $*  param string Self_name Excluded

  4. $#   param number  Self_name Excluded

  5. $?  last return value

  6. -a file exists. 

  7. -b file exists and is a block special file. 

  8. -c file exists and is a character special file. 

  9. -d file exists and is a directory. 

  10. -e file exists (just the same as -a). 

  11. -f file exists and is a regular file. 

  12. -g file exists and has its setgid(2) bit set. 

  13. -G file exists and has the same group ID as this process. 

  14. -k file exists and has its sticky bit set. 

  15. -L file exists and is a symbolic link. 

  16. -n string length is not zero. 

  17. -o Named option is set on. 

  18. -O file exists and is owned by the user ID of this process. 

  19. -p file exists and is a first in, first out (FIFO) special file or named pipe. 

  20. -r file exists and is readable by the current process. 

  21. -s file exists and has a size greater than zero. 

  22. -S file exists and is a socket. 

  23. -t file descriptor number fildes is open and associated with a 

  24. terminal device. 

  25. -u file exists and has its setuid(2) bit set. 

  26. -w file exists and is writable by the current process. 

  27. -x file exists and is executable by the current process. 

  28. -z string length is zero.

  29. bad interpreter 錯誤:由於windows檔案上傳到linux上導致的,在vim中: set ff=unix 然後儲存即可解決。

  30. 重定向:0 input 1 output 2 err .    echo ’sdf’>&1

  31. #> . script.sh  相當於 source script.sh

  32. “str1”=="str2"

    1. single user

    2. multi user

    3. multi user with networking

    4. -

    5. level 3 with GUI

    6. reboot

shell快捷鍵:

  1. - Ctrl+A or Home: Go to the beginning of the line.

  2. - Ctrl+E or End: Go to the end of the line.

  3. - Alt+B: Go left (back) one word.

  4. - Ctrl+B: Go left (back) one character.

  5. - Alt+F: Go right (forward) one word.

  6. - Ctrl+F: Go right (forward) one character.

  7. - Ctrl+XX: Move between the beginning of the line and the current position of the cursor. This allows you to press Ctrl+XX to return to the start of the line, change something, and then press Ctrl+XX to go back to your original cursor position. To use this shortcut, hold the Ctrl key and tap the X key twice.

  8. 更多

vim:

  1. 多行復制:9,15 copy 16  或 :9,15 co 16

  2. 執行shell命令:   :!command       :!bash     

  3. undo: u

  4. ?向上查詢

  5. /字串 全文查詢,再輸入/向下查詢

  6. :colorschem darkblue/delek/elflord    /usr/share/vim/vim72/colors/有各類配色方案名

  7. 配置放在.vimrc裡

  8. set hlsearch (high light search results)

  9. :noh 清除上次搜尋的highlight

  10. 選擇:v,選整行:V,Ctrl+v 矩形選取

  11. Cut(del):d    Copy:y    Paste:P/p  游標前/後插入

  12. Ctrl+N 自動E補全

make:

     make all VAR=value 可以直接向Makefile中定義變數,$(VAR)使用     

     makefile 中,命令前加@符號,可不列印此命令本身,精簡輸出。

編譯與連結:

ar rcs fname *.o

ranlib  更新Lib符號表

編譯選項 amd64 fPIC

strings 從二進位制程式中查詢字串

nm 列出二進位制檔案的符號表(一般用於c語言庫)

c++filt: c++和java編譯時會給符號加Namespace和其他修飾,並二進位制化符號名。該程式可將其解碼,使其可讀。

組合語言編譯器: nasm 和 as 或 gas。

nasm -f elf hello.asm

ld -m elf_i386 -s -o hello hello.o 連結器會按檔案列出的順序排列程式碼. -arch i386

普通的ld選項生成程式碼會從大致7kb位置編碼非常討厭,可以用 -r (merge objs only)讓從0開始編碼。如下

ld -static -r a.o b.o -o c.o

ld 的strip(精簡header)功能被各種不支援了,sb麼?

mac 反彙編 otool -tVj  (j:print opcode)

gcc -m32(32bit) -o(out name) -c(obj,Don;t link)

MACOS下,若不指明目標平臺,則全編譯成Mac-O object格式,必須如下指定 target引數

    • cc -m32 add.c -c -O0 -target i386   生成elf格式

    • cc -m32 add.c -c -O0 -arch i386 不好使,生成mac-o格式

GNU-binutils有各種看二進位制檔案的工具比如:

    • readelf

    • ar

    • objdump

MacOS上支援ELF的linux交叉編譯器: [專案網址

遠端操縱:

rz 上傳  escape binary y(overwrite)

sz 下載

ssh:  -qNfD local_port remote_addr 

    1. -D bind ssh with local socket port

    2. -q quiet mode

    3. -n direct stdin to /dev/null

    4. -N not exe any command

    5. -f background

在目錄中查詢檔案內容:

find ./ -xtype f -exec grep -in 'passwd' {} \;

讓grep顯示檔名:find ./ -xtype f -exec grep -Hin 'passwd' {} \;

grep ‘patternRE’ files

          

          grep 'abc.*def'

ubuntu下各種顏色的含義

判斷是否在screen裡: $TERM 變數

判斷是否在ssh裡:查詢 SSH_CLIENT or SSH_TTY is defined

本地程式:

  • python:

    1. py2 py3 區別:commands->subprocess 

    2. osx linux 區別:osx-> bytes=str   linux->bytes=bytes(str,encoding) 

    3. get_attr() 可根據名字獲得包內物件

    4. __import__(pkg ,fromlist=[‘xxx'])

      1. 該方法是為實現import語句而開發的。

      2. 當 fromlist 非空,模擬 from pkg import fromlist   返回pkg的最右包,

        因為import 語法要從最右包中逐個提取fromlist,在上下文中生成fromlist變數。

      3. 當 fromlist 為空,模擬 import pkg,直接返回pkg最左包

        因為若你 import a.b.c ,呼叫時仍需全稱a.b.c,所以僅將最左包新增進來即可。

    5. 用pydoc.locate() 能很好地達到(4)的效果,locate依賴safeimport()

    6. @classmethod 靜態函式,訪問靜態成員變數

    7. @staticmethod 固態函式,僅借用類路徑,不能訪問類內成員

    8. 找包的安裝位置: python -c "import numpy; print numpy.__file__"

python目錄相關:遍歷、判斷

  1. (當前目錄名,當前目錄下資料夾列表,當前目錄檔案列表)=os.walk()

  2. glob.glob( path + os.sep + '*’ ) 萬用字元

  3. 判斷是否目錄 os.path.isdir()

  4. 檢視當前變數:dir() locals() globals()

git

  1. 遠端倉庫如果用git init建造,則會因為working tree有東西,而拒絕push

  2. 強制更新他需要修改.git/config. 加入:

  3. [receive]

  4.      denyCurrentBranch = ignore

  5. 用git init - - bare 建造,則不會有此問題,因為bare庫沒有working dir,只有一個.git隱藏庫

  6. 檢視修改的message時間線: git log

    1. 檔案修改歷史 git log fname

    2. 檔案修改diff歷史 git log -p fname

  7. 檢視檔案差異 git diff:

    1. add暫存之前:git diff [fname]

    2. add之後:git diff --cached [fname]

    3. commit之間:git diff ffd09:fname  ffd10:fname

  8. get some files from other branch:

    git checkout other_branch file1 file2 file3

  9. show tree like branches:                git log —graph —decorate —oneline

  10. fetch handful files from remote:

    1.      git fetch

    2.      git checkout origin/master  - -  file1 file2 file3

  11. git add -u : update tracked index according to working tree   files not tracked will be ignored.

  12. merge: 

  13. strategy:

      when X Y have unique ancestor, do three way merge.

      Otherwise, merge the ancestors A1 A2 Ax.. first, or recursively so.

共享終端:url

  • 邀請方:kibitz -tty pts/3 root 
  • asking root to type: kibitz -3723
  • 接收方:kibitz -3723
  • 該終端實際上執行在邀請方的程序中

路由: