在 Linux 中 man 的使用頻率應該是很高的,靈活運用它可以讓自己快速掌握一個不熟悉的命令。以下以 CentOS 6.8 為環境說明。
man - format and display the on-line manual pages——在線使用手冊的格式與展示,通俗點說就是命令的使用手冊。
1、用戶命令 2、系統調用 3、C 庫調用 4、設備檔案及特殊檔案 5、配置檔格式 6、遊戲 7、雜項 8、管理命令及守護進程 9、Linux 內核 API(CentOS 7 以後出現)
之所以分這九個章節,是因為有的命令分屬不同類。查看命令具體屬於哪個章節,需要使用:
whatis COMMAND
以 echo 為例:
#whatis echo echo (1) - display a line of text echo (1p) - write arguments to standard output echo [builtins] (1) - bash built-in commands, see bash(1)
可見 echo 屬於第一章節。下面從 man 1 echo 逐項解釋:
#man 1 echo
NAME 命令名稱及功能簡要說明
echo - display a line of text
SYNOPSIS 用法說明,包括可用的選項
echo [SHORT-OPTION]... [STRING]...
echo LONG-OPTION
<>:此符號內為必給內容
[]:此符號內為可選內容
|:此符號表示多選一
{}:此符號表示為分組
...:同一內容出現多次
DESCRIPTION 命令功能的詳盡說明,可能包括每一項的意義
Echo the STRING(s) to standard output.
-n do not output the trailing newline
-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)
--help display this help and exit
--version output version information and exit
AUTHOR 作者 Written by Brian Fox and Chet Ramey.
REPORTING BUGS 報告程式 bug 的方式
COPYRIGHT 版權聲明
SEE ALSO 另外參照有的命令中還有 OPTIONS(說明每一個選項的意義)、FILES(此命令相關的配置檔)、ARGUMENTS(命令的作用對象),大致就是上面這幾項。
Enter / return 向後翻一行 k 向前翻一行 Ctrl+d 向檔案尾部翻半屏 Ctrl+u 向檔案首部翻半屏 G 跳至最後一行 #G 跳轉至指定行(# 為行號)
/keyword:以 keyword 為關鍵字,從當前介面向檔案尾部搜尋,不區分大小寫;n 下一個、N 上一個。
?keyword:從當前介面向檔案首部搜尋,不區分大小寫;n 跟搜尋命令同方向、N 反方向。
退出 man 用 q。
例子:字符終端登入時,顯示當前登入終端號、主機名和當前時間。要求用 man 查找實現,提示是修改 /etc/issue。
# cat /etc/issue CentOS release 6.8 (Final) Kernel \r on an \m
man issue
這份手冊很短,沒有多少可用資訊,但最後的 SEE ALSO 提示我們可以查 motd、agetty、mingetty:
man motd NAME motd - message of the day DESCRIPTION The contents of /etc/motd are displayed by login(1) after a successful login but just before it executes the login shell. SEE ALSO login(1), issue(5)
motd 裡也沒有值得參考的信息,繼續看下一個:
man agetty
NAME agetty - alternative Linux getty
SYNOPSIS
agetty [-8ihLmnUw] [-f issue_file] [-l login_program] [-I init]
[-t timeout] [-H login_host] port baud_rate,... [term]
DESCRIPTION
agetty opens a tty port, prompts for a login name and invokes
the /bin/login command. It is normally invoked by init(8)...這份手冊內容很多,於是用 /issue 快速定位:
ISSUE ESCAPES The issue-file (/etc/issue or the file set with the -f option) may contain certain escape codes to display the system name, date and time etc. All escape codes consist of a backslash (\) immediately followed by one of the letters explained below. b Insert the baudrate of the current line. d Insert the current date. s Insert the system name, the name of the operating system. l Insert the name of the current tty line. m Insert the architecture identifier of the machine, eg. i486 n Insert the nodename of the machine, also known as the hostname. r Insert the release number of the OS, eg. 1.1.9. t Insert the current time. u Insert the number of current users logged in. v Insert the version of the OS, eg. the build-date etc. Example: This is \n.\o (\s \m \r) \t displays as: This is thingol.orcan.dk (Linux i386 1.1.9) 18:29:30
找到了:\l 解決顯示當前登入終端號,\n 解決主機名,\t 解決時間顯示。於是修改配置檔:
#vi /etc/issue CentOS release 6.8 (Final) Kernel \r on an \m \l \n time is \t
wq 保存退出,然後 logout,登入提示已如預期顯示。

問題解決。man 的使用需要大家在平時慢慢摸索,這樣才會越用越得心應手。
本文出自「張帆-IT的奇幻漂流」博客,請務必保留此出處 https://chawan.blog.51cto.com/9179874/1830374