服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - Linux常用命令last的使用方法详解

Linux常用命令last的使用方法详解

2022-02-12 19:27潇湘隐者 Linux

最近在学习linux命令,学习到了last命令,发现很多同学对last命令不是很熟悉,last命令的功能列出目前与过去登入系统的用户相关信息,下面这篇文章主要给大家介绍了关于Linux常用命令last的使用方法,需要的朋友可以参考借鉴。

前言

本文主要给大家介绍了关于linux常用命令last用法的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍吧。

命令简介:

该命令用来列出目前与过去登录系统的用户相关信息。指令英文原义:show listing of last logged in users

执行权限 :有些需要特殊权限

指令所在路径:/usr/bin/last

执行last指令时,它会读取位于/var/log目录下名称为wtmp的文件,并把该给文件的内容记录的登录系统的用户名单全部显示出来。默认是显示wtmp的记录,btmp能显示的更详细,可以显示远程登录,例如ssh登录。

utmp文件中保存的是当前正在本系统中的用户的信息。

wtmp文件中保存的是登录过本系统的用户的信息。

命令输出字段介绍:

   第一列:用户名

   第二列:终端位置。pts/0 (伪终端) 意味着从诸如ssh或telnet的远程连接的用户.tty (teletypewriter) 意味着直接连接到计算机或者本地连接的用户

   第三列:登录ip或者内核 。如果你看见:0.0 或者什么都没有,这意味着用户通过本地终端连接。除了重启活动,内核版本会显示在状态中。

   第四列:开始时间

   第五列:结束时间(still login in 还未退出 down 直到正常关机 crash 直到强制关机)

   第六列:持续时间

命令语法:

last [-r] [-num] [ -n num ] [-adiowx] [ -f file ] [ -t yyyymmddhhmmss ] [name...]  [tty...]

命令参数:

 

参数

长参数

描叙

-a

 

将登录系统的的主机名称或ip地址,显示在最后一行

-d

 

将ip地址转换成主机名称

-f

 

指定记录文件,默认是显示/var/log目录下的wtmp文件的记录,但/var/log目录下得btmp能显示的内容更丰富,可以显示远程登录,例如ssh登录 ,包括失败的登录请求。

-i

 

-i显示特定ip登录的情况。跟踪用 -i显示特定ip登录的情况。跟踪用

-o

 

read an old-type wtmp file (written by linux-libc5 applications).

-n

 

-n <显示行数>或-<显示行数>  设置显示多少行记录

-w

 

display full user and domain names in the output

-r

 

不显示登入系统的主机名称或ip(省略 hostname 的栏位)

-t

 

显示yyyymmddhhmmss之前的信息

-x

 

显示系统关闭、用户登录和退出的历史

 

使用示例:

1:查看last命令的帮助信息

?
1
2
3
4
5
6
7
[root@db-server ~]# man last
 
 
 
[root@db-server ~]# last -h
last: invalid option -- h
usage: last [-num | -n num] [-f file] [-t yyyymmddhhmmss] [-r] [-x] [-o] [-w] [username..] [tty..]

2:显示最后登录系统的n条记录

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@db-server ~]#last -10
root pts/1 :0.0 wed dec 18 09:54 still logged in
root pts/4 :0.0 wed dec 18 09:43 - 09:48 (00:04)
root pts/1 :0.0 wed dec 18 09:43 - 09:48 (00:05)
root pts/3 192.168.103.79 wed dec 18 09:41 - 12:40 (02:59)
root pts/4 :0.0 wed dec 18 09:28 - 09:30 (00:01)
root pts/3 :0.0 wed dec 18 09:27 - 09:30 (00:02)
root pts/2 192.168.103.29 wed dec 18 09:27 still logged in
root pts/1 :0.0 wed dec 18 09:27 - 09:42 (00:15)
root pts/2 :0.0 wed dec 18 09:23 - 09:25 (00:01)
root pts/1 :0.0 wed dec 18 09:22 - 09:25 (00:02)
 
wtmp begins wed dec 11 03:02:17 2013
 
[root@db-server ~]# last -n 10
root pts/1 :0.0 wed dec 18 09:54 still logged in
root pts/4 :0.0 wed dec 18 09:43 - 09:48 (00:04)
root pts/1 :0.0 wed dec 18 09:43 - 09:48 (00:05)
root pts/3 192.168.103.79 wed dec 18 09:41 - 12:40 (02:59)
root pts/4 :0.0 wed dec 18 09:28 - 09:30 (00:01)
root pts/3 :0.0 wed dec 18 09:27 - 09:30 (00:02)
root pts/2 192.168.103.29 wed dec 18 09:27 still logged in
root pts/1 :0.0 wed dec 18 09:27 - 09:42 (00:15)
root pts/2 :0.0 wed dec 18 09:23 - 09:25 (00:01)
root pts/1 :0.0 wed dec 18 09:22 - 09:25 (00:02)
 
wtmp begins wed dec 11 03:02:17 2013

3:将登录系统的主机名或ip地址显示在最后一行

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@db-server ~]# last -10 -a
root pts/1 wed dec 18 09:54 still logged in :0.0
root pts/4 wed dec 18 09:43 - 09:48 (00:04) :0.0
root pts/1 wed dec 18 09:43 - 09:48 (00:05) :0.0
root pts/3 wed dec 18 09:41 - 12:40 (02:59) 192.168.103.79
root pts/4 wed dec 18 09:28 - 09:30 (00:01) :0.0
root pts/3 wed dec 18 09:27 - 09:30 (00:02) :0.0
root pts/2 wed dec 18 09:27 still logged in 192.168.103.29
root pts/1 wed dec 18 09:27 - 09:42 (00:15) :0.0
root pts/2 wed dec 18 09:23 - 09:25 (00:01) :0.0
root pts/1 wed dec 18 09:22 - 09:25 (00:02) :0.0
 
wtmp begins wed dec 11 03:02:17 2013

4:不显示登入系统的主机名称或ip地址

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@db-server ~]# last -10 -r
root pts/1 wed dec 18 09:54 still logged in
root pts/4 wed dec 18 09:43 - 09:48 (00:04)
root pts/1 wed dec 18 09:43 - 09:48 (00:05)
root pts/3 wed dec 18 09:41 - 12:40 (02:59)
root pts/4 wed dec 18 09:28 - 09:30 (00:01)
root pts/3 wed dec 18 09:27 - 09:30 (00:02)
root pts/2 wed dec 18 09:27 still logged in
root pts/1 wed dec 18 09:27 - 09:42 (00:15)
root pts/2 wed dec 18 09:23 - 09:25 (00:01)
root pts/1 wed dec 18 09:22 - 09:25 (00:02)
 
wtmp begins wed dec 11 03:02:17 2013

5: 指定/var/log/btmp文件,查看登录系统的用户相关信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@db-server ~]# last -n 10 -f /var/log/btmp
root ssh:notty 192.168.136.163 fri oct 17 18:16 gone - no logout
root ssh:notty 192.168.136.163 fri oct 17 09:50 - 18:16 (08:26)
root ssh:notty 192.168.136.163 fri oct 17 09:50 - 09:50 (00:00)
root ssh:notty 192.168.40.218 tue jul 23 17:40 - 09:50 (450+16:10)
root ssh:notty 192.168.236.149 sun apr 14 01:34 - 17:40 (100+16:05)
root ssh:notty 192.168.178.147 fri mar 8 17:25 - 01:34 (36+08:08)
tomcat ssh:notty get185806.gfg1.e fri oct 26 16:48 - 17:25 (133+00:37)
root ssh:notty 192.168.193.3 mon oct 22 18:13 - 16:48 (3+22:34)
root ssh:notty 192.168.193.3 mon oct 22 18:13 - 18:13 (00:00)
devloper ssh:notty get185819.gfg1.e wed oct 17 17:22 - 18:13 (5+00:50)
 
btmp begins thu apr 12 14:30:06 2012

6: 将ip地址转换成主机名称

?
1
last -10 -d

Linux常用命令last的使用方法详解

7: 显示yyyymmddhhmmss(20150110093000)之前的信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@db-server ~]# last -10 -t 20150110093000
root pts/2 192.168.102.186 fri jan 9 15:35 - 17:27 (01:52)
root pts/2 192.168.102.134 thu jan 8 10:25 - 12:27 (02:02)
root pts/3 192.168.125.53 tue jan 6 23:59 - 00:09 (00:09)
root pts/2 192.168.125.53 tue jan 6 23:45 - 00:09 (00:23)
root pts/3 192.168.102.88 tue jan 6 15:23 - 16:20 (00:57)
root pts/2 192.168.102.88 tue jan 6 15:08 - 17:25 (02:16)
oracle pts/1 :2.0 tue jan 6 15:07 still logged in
reboot system boot 2.6.32-200.13.1. tue jan 6 15:07 (7+20:21)
root pts/2 192.168.102.88 tue jan 6 14:47 - down (00:17)
oracle pts/1 :2.0 tue jan 6 14:46 - down (00:18)
 
wtmp begins wed apr 11 16:31:10 2012

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://www.cnblogs.com/kerrycode/p/4223751.html

延伸 · 阅读

精彩推荐
  • LinuxLinux中环境变量配置的步骤详解

    Linux中环境变量配置的步骤详解

    Linux中环境变量包括系统级和用户级,系统级的环境变量是每个登录到系统的用户都要读取的系统变量,而用户级的环境变量则是该用户使用系统时加载的...

    Myths7882022-02-10
  • LinuxLinux上设置用户通过SFTP访问目录的权限的方法

    Linux上设置用户通过SFTP访问目录的权限的方法

    这篇文章主要介绍了Linux上设置用户通过SFTP访问目录的权限的方法,SFTP可以理解为使用SSH协议进行FTP传输的协议,因而同时要对OpenSSH进行相关设置,需要的朋...

    OSChina10022019-06-19
  • LinuxLinux lnmp下无法使用mail发邮件的两种解决方法

    Linux lnmp下无法使用mail发邮件的两种解决方法

    在配置了lnmp环境后,出现了mail函数不能发送邮件的问题,其实有两种方法,一是使用sendmail组件,而是使用postfix。下面为大家一一介绍下 ...

    Linux之家4042019-09-17
  • LinuxLinux常用的日志文件和常用命令

    Linux常用的日志文件和常用命令

    成功地管理任何系统的关键之一,是要知道系统中正在发生什么事。 Linux 中提供了异常日志,并且日志的细节是可配置的。Linux 日志都以明文形式存储,所...

    Linux教程网2632020-04-18
  • Linux详解Linux系统下PXE服务器的部署过程

    详解Linux系统下PXE服务器的部署过程

    这篇文章主要介绍了Linux系统下PXE服务器的部署过程,包括对PXE的API架构作了一个基本的简介,需要的朋友可以参考下...

    运维之道9812019-07-04
  • Linux确保Linux系统安全的前提条件 漏洞防护

    确保Linux系统安全的前提条件 漏洞防护

    Linux 作为开放式的操作系统受到很多程序员的喜爱,很多高级程序员都喜欢编写Linux操作系统的相关软件。这使得Linux操作系统有着丰富的软件支持,还有无...

    Linux之家2642020-04-11
  • Linux手把手教您在 Linux 上使用 GPG 加解密文件

    手把手教您在 Linux 上使用 GPG 加解密文件

    在本教程中,我将告诉你如何用 GPG 加密和解密文件。这是一个简单的教程,你可以在你的 Linux 系统上尝试所有的练习。这将帮助你练习 GPG 命令,并在你...

    Linux中国6962021-12-15
  • Linuxlinux中rmdir命令使用详解(删除空目录)

    linux中rmdir命令使用详解(删除空目录)

    今天学习一下linux中命令: rmdir命令。rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的 ...

    linux命令大全5372019-11-19