who 命令,可以显示出当前系统中已经登录的用户信息。
从 man who 中可知:
一般的,如果只使用 who ,则一般缺省的通过读:/var/run/utmp 该文件获得信息。/var/log/wtmp 是比较常用的FILE。
查看一下 utmp 的信息:(使用带有 -k 的 man 命令可以根据关键字搜索联机帮助)
man -k utmp
列出信息如下:endutent [] (3) - access utmp file entries
getutent [] (3) - access utmp file entries
getutid [] (3) - access utmp file entries
getutline [] (3) - access utmp file entries
libutempter (rpm) - A privileged helper for utmp/wtmp updates
login [] (3) - write utmp and wtmp entries
logout [] (3) - write utmp and wtmp entries
pututline [] (3) - access utmp file entries
sessreg [] (1) - manage utmp/wtmp entries for non-init clients
setutent [] (3) - access utmp file entries
sysvinit-tools (rpm) - Tools used for process and utmp management.
utmp [] (5) - login records
utmpname [] (3) - access utmp file entries
utmpx.h [] (0p) - user accounting database definitions
从上面可以看到 utmp 的描述在第 5 小节中。那再使用以下命令查看(不指定小节编号也可以查看描述信息,但是显示结果会慢一些)
man 5 utmp
此时可以看到 utmp 的结构(其他的一些信息省略),更详细的定义在/usr/include/bits/utmp.h 里:
[C++] 纯文本查看 复制代码
/* The structure describing an entry in the user accounting database. */
struct utmp
{
short int ut_type; /* Type of login. */
pid_t ut_pid; /* Process ID of login process. */
char ut_line[UT_LINESIZE]; /* Devicename. */
char ut_id[4]; /* Inittab ID. */
char ut_user[UT_NAMESIZE]; /* Username. */
char ut_host[UT_HOSTSIZE]; /* Hostname for remote login. */
struct exit_status ut_exit; /* Exit status of a process marked
as DEAD_PROCESS. */
/* The ut_session and ut_tv fields must be the same size when compiled
32- and 64-bit. This allows data files and shared memory to be
shared between 32- and 64-bit applications. */
#if __WORDSIZE == 64 && defined __WORDSIZE_COMPAT32
int32_t ut_session; /* Session ID, used for windowing. */
struct
{
int32_t tv_sec; /* Seconds. */
int32_t tv_usec; /* Microseconds. */
} ut_tv; /* Time entry was made. */
#else
long int ut_session; /* Session ID, used for windowing. */
struct timeval ut_tv; /* Time entry was made. */
#endif
int32_t ut_addr_v6[4]; /* Internet address of remote host. */
char __unused[20]; /* Reserved for future use. */
};
---------------------
struct exit_status {
short int e_termination; /* process termination status. */
short int e_exit; /* process exit status. */
};
上面的定义中,从 /usr/include/utmp.h 中看到还包含有 /usr/include/bits/utmp.h ,打开这个 utmp.h 看看,可见定义了 UT_NAMESIZE 还有 UT_HOSTSIZE..等定义,如下所示:
[C++] 纯文本查看 复制代码 #define UT_LINESIZE 32
#define UT_NAMESIZE 32
#define UT_HOSTSIZE 256
who 参数说明:
-b , --boot : 系统最近一次启动的时间:[root@centos ~]# who -b
system boot 2011-05-28 08:35
-d, --dead : 打印已经死亡的进程:[root@centos ~]# who -d
2011-05-28 08:35 356 id=si term=0 exit=0
2011-05-28 08:36 1470 id=l3 term=0 exit=0
pts/0 2011-05-28 08:48 0 id=/0 term=0 exit=0
-u, --users : 列出已经登录的用户:[root@centos ~]# who -u
root pts/0 2011-05-28 09:14 00:01 4749 (218.17.167.82)
root pts/1 2011-05-28 09:28 . 4870 (218.17.167.82)
-r, --runlevel : 打印当前系统运行的等级:[root@centos ~]# who -r
run-level 3 2011-05-28 08:35 last=S |