Linux server quick cheat sheet
系統管理:
-
shell命令優先查詢順序:
-
./
-
/usr/local/bin
-
/usr/bin
-
/bin
-
/sbin
-
-
恢復模式:開機時按cms+R
-
ps / kill: (ps快速參考)
-
ps -e
show all process by PID | TTY name/command
-
ps -ax : list all processes with a tty(x) 檢視哪些tty在執行
-
ps -au list user information
-
ps -eLF 顯示所有執行緒(上述方法不顯示有些執行緒)
-
kill [signal, -9/-SIGKILL by default] pid
kill by pid
-
pkill [-g pgrep ] name kill process by name
-
killall name kill processed by name
-
-
renice:
-
調整優先順序PRI,renice將一個offset加到PRI上,
-
PRI越小則優先順序越高。
-
-
who: 檢視活躍使用者名稱|tty名|登入ip
-
sshd:
-
xos中,須修改 etc/ssh/sshd_config, 宣告好金鑰、協議、安全、連線數等。再將公鑰新增到.ssh/known_hosts
否則每次都要新生成所有key,然後新增已知hosts
-
必須全名呼叫
-
-
sudo -u root ‘cp xxx yyy’ 以root身份執行字串命令
-
mount: 檢視磁碟掛載到哪裡
-
top:程序佔用資源監控
-
htop:top高階版
-
iotop:帶磁碟讀寫監控的top
-
iostat:專門監控磁碟讀寫
-
vmstat:虛擬記憶體用量存量
-
isof:is_opened_file 列出所有被開啟的檔案資源
-
tcpdump:監控TCP/IP包
-
netstat:監控TCP/IP連線
-
netstat -tlunp | grep port 埠被誰佔用
-
tulnp:(tcp udp listen numeric program)
-
-
iftop/nethogs:監控程式的網路吞吐
-
psacct/acct:監控其他使用者
-
suricata:網路安全軟體,OISF開源網路安全基金會開發
-
ext3grep: rm * 恢復命令 mysql binlog
-
write username tty 傳送給user訊息 crtl+D 或 ctrl+C 結束
-
wall : write to all users on server
-
rwall : write to all users on network [danger!]
-
talk [user]: live chat with [user]
-
lsb_release 檢視發行版名稱
-
lspci
-
lscpu
-
Ctrl+Shift+C=複製,Ctrl+Shift+V=貼上
-
編輯當前行的命令:
- ctrl-w刪除當前命令的一個單詞,
- ctrl-u刪除到行首,ctrl-k刪除行尾,
- ctrl-a移動到行首,ctrl-e移動到行尾
- ctrl-x ctrl-e 可以呼叫自己定義的編輯器來編輯當前命令列
-
控制任務前後臺
jobs 檢視後臺任務編號
ctrl z 將任務暫停,回到bash
fg $no 將no號任務放到前臺
bg $no 放到後臺
disown $no 將任務與session脫鉤,關閉term後不會結束任務
nohup 後臺執行,不與term掛鉤.
-
系統CPU 網路 硬碟監控:blog
-
file 命令判斷so檔案x64/32位數
-
free -m 檢視記憶體 MB單位 或者直接看檔案: /proc/meminfo
-
$1
-
[email protected] param list Self_name Excluded
-
$* param string Self_name Excluded
-
$# param number Self_name Excluded
-
$? last return value
-
-a file exists.
-
-b file exists and is a block special file.
-
-c file exists and is a character special file.
-
-d file exists and is a directory.
-
-e file exists (just the same as -a).
-
-f file exists and is a regular file.
-
-g file exists and has its setgid(2) bit set.
-
-G file exists and has the same group ID as this process.
-
-k file exists and has its sticky bit set.
-
-L file exists and is a symbolic link.
-
-n string length is not zero.
-
-o Named option is set on.
-
-O file exists and is owned by the user ID of this process.
-
-p file exists and is a first in, first out (FIFO) special file or named pipe.
-
-r file exists and is readable by the current process.
-
-s file exists and has a size greater than zero.
-
-S file exists and is a socket.
-
-t file descriptor number fildes is open and associated with a
-
terminal device.
-
-u file exists and has its setuid(2) bit set.
-
-w file exists and is writable by the current process.
-
-x file exists and is executable by the current process.
-
-z string length is zero.
-
bad interpreter 錯誤:由於windows檔案上傳到linux上導致的,在vim中: set ff=unix 然後儲存即可解決。
-
重定向:0 input 1 output 2 err . echo ’sdf’>&1
-
#> . script.sh 相當於 source script.sh
-
“str1”=="str2"
-
-
single user
-
multi user
-
multi user with networking
-
-
-
level 3 with GUI
-
reboot
-
shell快捷鍵:
-
- Ctrl+A or Home: Go to the beginning of the line.
-
- Ctrl+E or End: Go to the end of the line.
-
- Alt+B: Go left (back) one word.
-
- Ctrl+B: Go left (back) one character.
-
- Alt+F: Go right (forward) one word.
-
- Ctrl+F: Go right (forward) one character.
-
- 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.
vim:
-
多行復制:9,15 copy 16 或 :9,15 co 16
-
執行shell命令: :!command :!bash
-
undo: u
-
?向上查詢
-
/字串 全文查詢,再輸入/向下查詢
-
:colorschem darkblue/delek/elflord /usr/share/vim/vim72/colors/有各類配色方案名
-
配置放在.vimrc裡
-
set hlsearch (high light search results)
-
:noh 清除上次搜尋的highlight
-
選擇:v,選整行:V,Ctrl+v 矩形選取
-
Cut(del):d Copy:y Paste:P/p 游標前/後插入
-
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
-
-
-D bind ssh with local socket port
-
-q quiet mode
-
-n direct stdin to /dev/null
-
-N not exe any command
-
-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:
-
-
py2 py3 區別:commands->subprocess
-
osx linux 區別:osx-> bytes=str linux->bytes=bytes(str,encoding)
-
get_attr() 可根據名字獲得包內物件
-
__import__(pkg ,fromlist=[‘xxx'])
-
該方法是為實現import語句而開發的。
-
當 fromlist 非空,模擬 from pkg import fromlist 返回pkg的最右包,
因為import 語法要從最右包中逐個提取fromlist,在上下文中生成fromlist變數。
-
當 fromlist 為空,模擬 import pkg,直接返回pkg最左包
因為若你 import a.b.c ,呼叫時仍需全稱a.b.c,所以僅將最左包新增進來即可。
-
-
用pydoc.locate() 能很好地達到(4)的效果,locate依賴safeimport()
-
@classmethod 靜態函式,訪問靜態成員變數
-
@staticmethod 固態函式,僅借用類路徑,不能訪問類內成員
-
找包的安裝位置: python -c "import numpy; print numpy.__file__"
-
python目錄相關:遍歷、判斷
-
(當前目錄名,當前目錄下資料夾列表,當前目錄檔案列表)=os.walk()
-
glob.glob( path + os.sep + '*’ ) 萬用字元
-
判斷是否目錄 os.path.isdir()
-
檢視當前變數:dir() locals() globals()
git
-
遠端倉庫如果用git init建造,則會因為working tree有東西,而拒絕push
-
強制更新他需要修改.git/config. 加入:
-
[receive]
-
denyCurrentBranch = ignore
-
用git init - - bare 建造,則不會有此問題,因為bare庫沒有working dir,只有一個.git隱藏庫
-
檢視修改的message時間線: git log
-
檔案修改歷史 git log fname
-
檔案修改diff歷史 git log -p fname
-
-
檢視檔案差異 git diff:
-
add暫存之前:git diff [fname]
-
add之後:git diff --cached [fname]
-
commit之間:git diff ffd09:fname ffd10:fname
-
-
get some files from other branch:
git checkout other_branch file1 file2 file3
-
show tree like branches: git log —graph —decorate —oneline
-
fetch handful files from remote:
-
git fetch
-
git checkout origin/master - - file1 file2 file3
-
-
git add -u : update tracked index according to working tree files not tracked will be ignored.
-
merge:
-
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
- 該終端實際上執行在邀請方的程序中