浅谈 linux 帮助命令

前言

在使用linux的过程中,或多或少会遇到一些陌生的命令,亦或者遗忘了某些命令的参数。毕竟人非圣贤,能记住的,也就那些个常用的东西,不常用的,总会有所遗失。这时候,求助于系统帮助文档,是最快捷的解决途径,下面来介绍几种帮助命令的使用方法。

最方便的帮助命令:--help

对于中国人来说,最方便的莫过于中文帮助文档了。相对于man,info等英文帮助文档,–help会根据系统设定,显示与系统语言相同的帮助文档,大大方便了英文不好的童鞋。这也是最常用的帮助命令。

[root@COS6 ~]# passwd --help
用法: passwd [选项...] <帐号名称>
  -k, --keep-tokens       保持身份验证令牌不过期
  -d, --delete            删除已命名帐号的密码(只有根用户才能进行此操作)
  -l, --lock              lock the password for the named account (root only)
  -u, --unlock            unlock the password for the named account (root only)
  -e, --expire            expire the password for the named account (root only)
  -f, --force             强制执行操作
  -x, --maximum=DAYS      密码的最长有效时限(只有根用户才能进行此操作)
  -n, --minimum=DAYS      密码的最短有效时限(只有根用户才能进行此操作)
  -w, --warning=DAYS      在密码过期前多少天开始提醒用户(只有根用户才能进行此操作)
  -i, --inactive=DAYS     当密码过期后经过多少天该帐号会被禁用(只有根用户才能进行此操作)
  -S, --status            报告已命名帐号的密码状态(只有根用户才能进行此操作)
  --stdin                 从标准输入读取令牌(只有根用户才能进行此操作)

Help options:
  -?, --help              Show this help message
  --usage                 Display brief usage message
[root@COS6 ~]# 

最强大的帮助命令:man

man支持多级别帮助信息的查询,命令的查找,以及显示配置文件的帮助信息,堪称最强大的系统帮助命令。

The standard sections of the manual include:# man命令级别划分

1      User Commands # 用户命令的帮助

2      System Calls # 可被内核调用的函数的帮助

3      C Library Functions # 查看函数和函数库的帮助

4      Devices and Special Files # 查看特殊文件的帮助(主要是/dev目录下的文件)

5      File Formats and Conventions # 查看配置文件的帮助

6      Games et. Al.# 查看游戏的帮助

7      Miscellanea # 查看其他杂项的帮助

8      System Administration tools and Deamons # 查看管理员可用命令的帮助

或许很多人不理解为何会有分级别的帮助信息,举个简单的例子就可以很容易明白了,就以平时常用的passwd命令为例。
先用“man -f passwd”查看passwd所包含的级别。

[root@COS6 ~]# man -f passwd
passwd               (1)  - update user's authentication tokens
passwd               (5)  - password file
passwd [sslpasswd]   (1ssl)  - compute password hashes
[root@COS6 ~]# 

可以看到passwd包含两个级别:(1)为命令级别的帮助信息;(5)为配置文件级别的帮助信息。

分别打开就可以看到不同的帮助信息了:

[root@COS6 ~]# man 1 passwd
PASSWD(1)                       User utilities                       PASSWD(1)

NAME
       passwd - update user’s authentication tokens

SYNOPSIS
       passwd  [-k]  [-l]  [-u  [-f]]  [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactivedays] [-S] [--stdin]
       [username]

DESCRIPTION
       The passwd utility is used to update user’s authentication token(s).

       This task is achieved through calls to the Linux-PAM and Libuser API.  Essentially, it  initializes  itself  as  a
       "passwd"  service with Linux-PAM and utilizes configured password modules to authenticate and then update a user’s
       password.

       A simple entry in the global Linux-PAM configuration file for this service would be:

        #
        # passwd service entry that does strength checking of
        # a proposed password before updating it.
        #
        passwd password requisite pam_cracklib.so retry=3
        passwd password required pam_unix.so use_authtok
        #

       Note, other module types are not required for this application to function correctly.
...
[root@COS6 ~]# man 5 passwd
PASSWD(5)                  Linux Programmer’s Manual                 PASSWD(5)

NAME
       passwd - password file

DESCRIPTION
       Passwd is a text file, that contains a list of the system’s accounts, giving for each account some useful informa-
       tion like user ID, group ID, home directory, shell, etc.  Often, it also contains the encrypted passwords for each
       account.  It should have general read permission (many utilities, like ls(1) use it to map user IDs to usernames),
       but write access only for the superuser.

       In the good old days there was no great problem with this general  read  permission.   Everybody  could  read  the
       encrypted  passwords,  but  the  hardware  was  too  slow to crack a well-chosen password, and moreover, the basic
       assumption used to be that of a friendly user-community.  These days many people run some version  of  the  shadow
       password  suite,  where  /etc/passwd  has  "x"  instead of encrypted passwords, and the encrypted passwords are in
       /etc/shadow which is readable by the superuser only.

       If the encrypted password, whether in /etc/passwd or in /etc/shadow, is an empty string, login is allowed  without
       even  asking  for a password.  Note that this functionality may be intentionally disabled in applications, or con-
       figurable (for example using the "nullok" or "nonull" arguments to pam_unix.so).

       If the encrypted password in /etc/passwd is "*NP*" (without the quotes), the shadow record should be obtained from
       a NIS+ server.

       Regardless of whether shadow passwords are used, many sysadmins use an asterisk in the encrypted password field to
       make sure that this user can not authenticate him- or herself using a password.  (But see the Notes below.)

       If you create a new login, first put an asterisk in the password field, then use passwd(1) to set it.
...

当我们不知道具体命令,而只记得部分名字的时候,还可以用man -k来进行查找,比如这样就可以看到与passwd关键字相关的命令和配置文件的信息,包括帮助级别:

[root@COS6 ~]# man -k passwd
chpasswd             (8)  - update passwords in batch mode
fgetpwent_r [getpwent_r] (3)  - get passwd file entry reentrantly
getpwent_r           (3)  - get passwd file entry reentrantly
gpasswd              (1)  - administer /etc/group and /etc/gshadow
kpasswd              (1)  - change a user's Kerberos password
lpasswd              (1)  - Change group or user password
lppasswd             (1)  - add, change, or delete digest passwords
mkpasswd             (1)  - generate new password, optionally apply it to a user
pam_localuser        (8)  - require users to be listed in /etc/passwd
pam_passwdqc         (8)  - Password quality-control PAM module
passwd               (1)  - update user's authentication tokens
passwd2des [xcrypt]  (3)  - RFS password encryption
passwd               (5)  - password file
passwd [sslpasswd]   (1ssl)  - compute password hashes
saslpasswd2          (8)  - set a user's sasl password
smbpasswd            (5)  - The Samba encrypted password file
smbpasswd            (8)  - change a users SMB password
userpasswd           (1)  - A graphical tool to allow users to change their passwords
ypchfn [yppasswd]    (1)  - change your password in the NIS database
ypchsh [yppasswd]    (1)  - change your password in the NIS database
yppasswd             (1)  - change your password in the NIS database
[root@COS6 ~]# 

关于man常用参数就介绍到这,其他参数信息请参考man --help

最详细的帮助命令:info

info命令可以说跟windows使用的F1差不多,它是把所有的帮助信息集合成一个帮助文档,然后从文档中查找相关命令的信息,可以获得最全面的帮助信息。但是在一般的操作过程中很少使用,原因也就是因为信息太过于全面,导致关键信息难于查找。不过对于了解新接触的命令,还是很有帮助的。

[root@COS6 ~]# info ls

File: coreutils.info,  Node: ls invocation,  Next: dir invocation,  Up: Directory listing

10.1 `ls': List directory contents
==================================

The `ls' program lists information about files (of any type, including
directories).  Options and file arguments can be intermixed
arbitrarily, as usual.

   For non-option command-line arguments that are directories, by
default `ls' lists the contents of directories, not recursively, and
omitting files with names beginning with `.'.  For other non-option
arguments, by default `ls' lists just the file name.  If no non-option
argument is specified, `ls' operates on the current directory, acting
as if it had been invoked with a single argument of `.'.

   By default, the output is sorted alphabetically, according to the
locale settings in effect.(1) If standard output is a terminal, the
output is in columns (sorted vertically) and control characters are
output as question marks; otherwise, the output is listed one per line
and control characters are output as-is.

   Because `ls' is such a fundamental program, it has accumulated many
options over the years.  They are described in the subsections below;
within each section, options are listed alphabetically (ignoring case).
The division of options into the subsections is not absolute, since some
options affect more than one aspect of `ls''s operation.
...

关于系统常用的帮助命令就介绍到这,希望对大家在使用linux的过程中有所帮助。