- 論壇徽章:
- 2
|
本帖最后由 122285969 于 2011-05-08 16:17 編輯
學(xué)習(xí)Linux命令也有些時日了,今天把這份簡單總結(jié)分享給大家
如果你是正在學(xué)習(xí)Linux命令的朋友,希望能給你帶來些許幫助
有問題才會有方向,解決問題就是我們的方向
所以讓我們帶著下面6個問題出發(fā)……
當(dāng)前用戶直接能使用的命令都存在哪里呢?
當(dāng)前用戶直接能使用的命令一共有多少個?
我記不清命令拼寫該怎么處理呢?
我怎么知道一個命令是干什么的?
我怎么知道一個命令如何使用呢?
當(dāng)前目錄的可執(zhí)行文件怎么不能直接執(zhí)行?
我們按照上面的順序一一來解答:
當(dāng)前用戶直接能使用的命令都存在哪里呢?
我們先看看root用戶:- [root@Centos ~]# whoami
- root
復(fù)制代碼 PS:當(dāng)前用戶root- [root@Centos ~]# echo $PATH
- /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
復(fù)制代碼 PS:Linux所有可以直接執(zhí)行的命令都是存在 PATH 環(huán)境變量中以“:”分隔的路徑下的- [root@Centos ~]# echo $PATH | sed 's/:/\n/g' | cat -n
- 1 /usr/kerberos/sbin
- 2 /usr/kerberos/bin
- 3 /usr/local/sbin
- 4 /usr/local/bin
- 5 /sbin
- 6 /bin
- 7 /usr/sbin
- 8 /usr/bin
- 9 /root/bin
復(fù)制代碼 PS:默認(rèn)root用戶所有可以直接執(zhí)行的命令,就存在這9個目錄下
我們再看看一個普通用戶的情況:- [ctu@Centos ~]$ whoami
- ctu
復(fù)制代碼 PS:當(dāng)前用戶ctu- [ctu@Centos ~]$ echo $PATH
- /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/ctu/bin
- [ctu@Centos ~]$ echo $PATH | awk -F ':' '{for (i=1; i<=NF; i++) {printf("%s\n",$i)}}' | cat -n
- 1 /usr/kerberos/bin
- 2 /usr/local/bin
- 3 /bin
- 4 /usr/bin
- 5 /home/ctu/bin
復(fù)制代碼 PS:普通用戶默認(rèn)可以直接執(zhí)行的命令,就存在這個5個目錄下。我們可以看到,root跟普通用戶的默認(rèn) PATH 環(huán)境變量的值并不相同,沒在各自環(huán)境變量中指定路徑的命令,就只能用絕對路徑和相對路徑來執(zhí)行了。
當(dāng)前用戶直接能使用的命令一共有多少個?
我們先寫一個測試腳本:- [root@Centos ~]# cat -n comm_sum_1.sh
- 1 #!/bin/sh
- 2
- 3 sum=0
- 4 count=0
- 5
- 6 echo -e "I'am $USER !\n"
- 7
- 8 for comm_path in `echo $PATH | sed 's/:/\n/g'`
- 9 do
- 10 count=`ls $comm_path 2>/dev/null | wc -l`
- 11 echo -e "$count\t: $comm_path"
- 12 sum=$[sum+count]
- 13 done
- 14
- 15 echo -e "\n$sum\t: comm_sum"
復(fù)制代碼 我們以root用戶運行一下試試看吧:- [root@Centos ~]# . comm_sum_1.sh
- I'am root !
- 14 : /usr/kerberos/sbin
- 20 : /usr/kerberos/bin
- 0 : /usr/local/sbin
- 0 : /usr/local/bin
- 264 : /sbin
- 111 : /bin
- 348 : /usr/sbin
- 1111 : /usr/bin
- 0 : /root/bin
- 1868 : comm_sum
復(fù)制代碼 PS:提示我們root用戶可以直接使用的命令一共有 1868 個? 真的是這樣嗎?
我們再寫一個腳本,這次加入了可執(zhí)行權(quán)限的判斷,并把不能直接執(zhí)行的命令記錄在 no_allow_comm.txt 文件中- [root@Centos ~]# cat -n comm_sum_2.sh
- 1 #!/bin/sh
- 2
- 3 sum=0
- 4
- 5 echo -e "I'am $USER !\n"
- 6
- 7 for comm_path in `echo $PATH | sed 's/:/\n/g'`
- 8 do
- 9 count=0
- 10 for file in `ls $comm_path 2>/dev/null`
- 11 do
- 12 [ -x "$comm_path/$file" ] && count=$[count+1] || echo "$comm_path/$file" >> no_allow_comm.txt
- 13 done
- 14 echo -e "$count\t: $comm_path"
- 15 sum=$[sum+count]
- 16 done
- 17
- 18 echo -e "\n$sum\t: comm_sum"
- 19
復(fù)制代碼 我們以腳本 comm_sum_2.sh 來檢測試試看:- [root@Centos ~]# . comm_sum_2.sh
- I'am root !
- 14 : /usr/kerberos/sbin
- 20 : /usr/kerberos/bin
- 0 : /usr/local/sbin
- 0 : /usr/local/bin
- 264 : /sbin
- 111 : /bin
- 348 : /usr/sbin
- 1107 : /usr/bin
- 0 : /root/bin
- 1864 : comm_sum
復(fù)制代碼 PS:?這個跟我們上次檢測的結(jié)果有出入了- [root@Centos ~]# cat -n no_allow_comm.txt
- 1 /usr/bin/ecryptfs-dot-private
- 2 /usr/bin/huge_page_setup_helper.pyc
- 3 /usr/bin/huge_page_setup_helper.pyo
- 4 /usr/bin/pygtk-demo
- [root@Centos ~]# ecryptfs-dot-private
- -bash: /usr/bin/ecryptfs-dot-private: 權(quán)限不夠
- [root@Centos ~]# ls -l /usr/bin/ecryptfs-dot-private
- -rw-r--r-- 1 root root 1154 2009-09-04 /usr/bin/ecryptfs-dot-private
復(fù)制代碼 PS:我們再次檢測的時候,提示一共有 1864 個了。還有4個命令,確實不可以直接執(zhí)行。
用一個普通用戶來試試看:- [ctu@Centos ~]$ . comm_sum_1.sh
- I'am ctu !
- 20 : /usr/kerberos/bin
- 0 : /usr/local/bin
- 111 : /bin
- 1111 : /usr/bin
- 0 : /home/ctu/bin
- 1242 : comm_sum
復(fù)制代碼 PS:腳本1檢測的結(jié)果是 1242 個- [ctu@Centos ~]$ . comm_sum_2.sh
- I'am ctu !
- 20 : /usr/kerberos/bin
- 0 : /usr/local/bin
- 111 : /bin
- 1105 : /usr/bin
- 0 : /home/ctu/bin
- 1236 : comm_sum
復(fù)制代碼 PS:腳本2檢測的結(jié)果是 1236 個,一樣有出入- [ctu@Centos ~]$ cat -n no_allow_comm.txt
- 1 /usr/bin/amtu
- 2 /usr/bin/aulastlog
- 3 /usr/bin/ecryptfs-dot-private
- 4 /usr/bin/huge_page_setup_helper.pyc
- 5 /usr/bin/huge_page_setup_helper.pyo
- 6 /usr/bin/pygtk-demo
- [ctu@Centos ~]$ amtu
- -bash: /usr/bin/amtu: 權(quán)限不夠
- [ctu@Centos ~]$ ls -l /usr/bin/amtu
- -rwxr-x--- 1 root root 33412 2008-05-25 /usr/bin/amtu
復(fù)制代碼 PS:跟上面的結(jié)果是一樣的,也有些命令雖然是在 PATH 環(huán)境變量中,仍然不可以直接執(zhí)行的。
我記不清命令拼寫該怎么處理呢?
我們以命令 ifconfig 為例說明:
我要使用這個命令,但是我不記得怎么拼寫了,模糊地記得好像前面兩個字符是 if 吧?
那就輸入 if ,再按兩下 TAB 鍵吧。- [root@Centos ~]# if
- if ifconfig ifenslave ifrename
- ifcfg ifdown ifnames ifup
復(fù)制代碼 系統(tǒng)自動為我們找到了我們要使用的命令 ifconfig
但是 TAB 鍵的方法只適合我們記得前面幾個字符的情況。
要是我們只記得第3,4個字符為 co 呢? 或者我們只記得命令中含有 fig 呢?這樣的情況我們使用 TAB 鍵好像無能為力了。
顯然 TAB 鍵在這些情況下并不適合,要必要尋找新的方法來替代了(這并不是 TAB 鍵功能的缺陷)。
我們寫一個命令查找的shell腳本- [ctu@Centos ~]$ cat -n comm_find.sh
- 1 #!/bin/sh
- 2
- 3 read -p "Please enter a keyword : " comm_name
- 4
- 5 echo -e "I'am $USER !\n"
- 6
- 7 for comm_path in `echo $PATH | sed 's/:/\n/g'`
- 8 do
- 9 ls $comm_path 2>/dev/null | grep -iE "$comm_name" && echo -e "\n[ $comm_path ]\n"
- 10 done
復(fù)制代碼 我們測試下只記得第3,4個字符是 co 的情況:- [root@Centos ~]# . comm_find.sh
- Please enter a keyword : ^..co
- I'am root !
- ifconfig
- iwconfig
- ldconfig
- [ /sbin ]
- dbconverter-2
- lvconvert
- pwconv
- racoon
- racoonctl
- vgconvert
- [ /usr/sbin ]
- chcon
- cscope
- cscope-indexer
- fgconsole
- opcontrol
- piconv
- recountdiff
- rfcomm
- secon
- sfconvert
- [ /usr/bin ]
復(fù)制代碼 憑著我們的印象,相信你能找出來要使用的命令 ifconfig ,除非你從來就沒使用過。
我們再看看只記得含有 fig 的情況:- [root@Centos ~]# . comm_find.sh
- Please enter a keyword : fig
- I'am root !
- krb5-config
- [ /usr/kerberos/bin ]
- chkconfig
- ifconfig
- iwconfig
- ldconfig
- plipconfig
- vconfig
- [ /sbin ]
- authconfig
- authconfig-gtk
- authconfig-tui
- hciconfig
- iconvconfig
- iconvconfig.i686
- system-config-authentication
- system-config-lvm
- system-config-network
- system-config-network-cmd
- system-config-network-gui
- system-config-network-tui
- system-config-packages
- sys-unconfig
- timeconfig
- [ /usr/sbin ]
- authconfig
- authconfig-gtk
- authconfig-tui
- curl-config
- dateconfig
- gpg-error-config
- libgcrypt-config
- libusb-config
- nspr-config
- nss-config
- pkg-config
- scim-config-agent
- scrollkeeper-config
- system-config-authentication
- system-config-date
- system-config-display
- system-config-kdump
- system-config-keyboard
- system-config-language
- system-config-lvm
- system-config-network
- system-config-network-cmd
- system-config-rootpassword
- system-config-securitylevel
- system-config-securitylevel-tui
- system-config-soundcard
- system-config-time
- system-config-users
- xml2-config
- xmlsec1-config
- xslt-config
- [ /usr/bin ]
復(fù)制代碼 一樣可以憑著印象找出來的,這樣也就可以應(yīng)付所有忘記拼寫但使用過的命令的情況了。
我怎么知道一個命令是干什么的?
我們以 ping 命令為例說明:- [ctu@Centos ~]$ man ping
- PING(8) System Manager’s Manual: iputils PING(8)
- NAME
- ping, ping6 - send ICMP ECHO_REQUEST to network hosts
復(fù)制代碼- [ctu@Centos ~]$ info ping
- File: *manpages*, Node: ping, Up: (dir)
- PING(8) System Manager’s Manual: iputils PING(8)
- NAME
- ping, ping6 - send ICMP ECHO_REQUEST to network hosts
復(fù)制代碼 PS:man info命令給出了要查找命令的功能說明
我怎么知道一個命令如何使用呢?- [root@Centos ~]# ping
- Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]
- [-p pattern] [-s packetsize] [-t ttl] [-I interface or address]
- [-M mtu discovery hint] [-S sndbuf]
- [ -T timestamp option ] [ -Q tos ] [hop1 ...] destination
復(fù)制代碼 PS:不帶參數(shù)執(zhí)行一個命令,通常會給出該命令的簡單使用說明,但不是所有命令都會的- [ctu@Centos ~]$ man ping
- OPTIONS
- -a Audible ping.
- -A Adaptive ping. Interpacket interval adapts to round-trip
- time, so that effectively not more than one (or more, if
- preload is set) unanswered probes present in the net-
- work. Minimal interval is 200msec for not super-user.
- On networks with low rtt this mode is essentially equiv-
- alent to flood mode.
- -b Allow pinging a broadcast address.
- -B Do not allow ping to change source address of probes.
- The address is bound to one selected when ping starts.
- -c count
- Stop after sending count ECHO_REQUEST packets. With
- deadline option, ping waits for count ECHO_REPLY pack-
- ets, until the timeout expires.
- -d Set the SO_DEBUG option on the socket being used.
- Essentially, this socket option is not used by Linux
- kernel.
- -F flow label
- Allocate and set 20 bit flow label on echo request pack-
- ets. (Only ping6). If value is zero, kernel allocates
- random flow label.
復(fù)制代碼 PS:man命令會給出命令的詳細參數(shù)說明,還有info也一樣,通常info命令會有相應(yīng)的應(yīng)用舉例,按自己喜好來查找吧
當(dāng)前目錄的可執(zhí)行文件怎么不能直接執(zhí)行?
默認(rèn)情況下,Linux是不搜索當(dāng)前目錄的命令的- [root@Centos ~]# echo $PATH
- /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
- [root@Centos ~]# cat -n hp.sh
- 1 #!/bin/bash
- 2
- 3 echo "hello ping"
復(fù)制代碼 PS:hp.sh作用就是輸出“hello ping”- [root@Centos ~]# chmod +x hp.sh
復(fù)制代碼 PS:添加當(dāng)前用戶的可執(zhí)行權(quán)限- [root@Centos ~]# hp.sh
- -bash: hp.sh: command not found
復(fù)制代碼 PS:具有可執(zhí)行權(quán)限,也確實存在當(dāng)前目錄下,但是仍然提示命令找不到- [root@Centos ~]# ./hp.sh
- hello ping
復(fù)制代碼 PS:使用相對路徑,我們就可以執(zhí)行成功- [root@Centos ~]# export PATH=.:$PATH
復(fù)制代碼 PS:現(xiàn)在我們將當(dāng)前目錄加入到 PATH 環(huán)境變量中- [root@Centos ~]# echo $PATH
- .:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
- [root@Centos ~]# hp.sh
- hello ping
復(fù)制代碼 PS:不使用絕對路徑或者相對路徑,這次我們也可以執(zhí)行成功了- [root@Centos ~]# ping
- Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]
- [-p pattern] [-s packetsize] [-t ttl] [-I interface or address]
- [-M mtu discovery hint] [-S sndbuf]
- [ -T timestamp option ] [ -Q tos ] [hop1 ...] destination
復(fù)制代碼 PS:此時執(zhí)行的 ping 命令是系統(tǒng)自帶的- [root@Centos ~]# mv hp.sh ping
復(fù)制代碼 PS:我們將 hp.sh 也改名為 ping- [root@Centos ~]# ping
- hello ping
復(fù)制代碼 PS:這次執(zhí)行的ping命令就不再是系統(tǒng)自帶的了。命令搜索是按照 PATH 環(huán)境變量,以“:”分隔的路徑,從左到右逐個搜索的。因此當(dāng)我們將 hp.sh 腳本改名成 ping 命令時。這時候存在兩個 ping 命令了。而當(dāng)前目錄下的 ping 優(yōu)先于 /bin/ping 命令,也就被執(zhí)行了。
就寫到這里吧,希望大家多多支持。≈x謝!
--- THE END --- |
|