Bash .hushlogin,保留上次登录时间和主机


19

在公司中,当我登录到某些服务器时,将显示我的上次登录名和一个巨大的横幅:

me@my-laptop$ ssh the-server
Last login: Mon Feb  8 18:54:36 2016 from my-laptop.company.com 
************************************************************************
*                                                                      *
*       C O M P A N Y    I N F O R M A T I O N   S Y S T E M S         *
*                                                                      *
* !WARNING!         Your connection has been logged          !WARNING! *
*                                                                      *
* This system is for the use of authorized personnel only.             *
* Individuals using this *computer system without authorization,       *
* or in excess of their authority as determined by the Company         *
* Code of Ethics and  Acceptable Use Policy, are subject to having all *
* of their activities on this system monitored, recorded and/or        *
* terminated by system personnel.                                      *
* If such monitoring reveals possible evidence of criminal  activity,  *
* Company may provide said evidence to law enforcement officials,      *
* in compliance with its confidentiality obligations and all           *
* applicable national laws/regulations with regards to data privacy.   *
*                                                                      *
*      This device is maintained by Company Department                 *
*                  admin@company.com                                   *
************************************************************************
me@the-server$ 

当然,我不希望每次登录时都显示这个巨大的横幅,但我想保留上次登录时间和主机

如果我使用touch ~/.hushlogin该横幅,则不会显示该横幅,但也会丢失 最后的登录信息。实际上,什么都没有显示:

ssh the-server
me@the-server$

如何删除横幅,但保留上次登录时间和主机,如下所示:

 ssh the-server
 Last login: Mon Feb  8 18:54:36 2016 from my-laptop.company.com
 me@the-server$

Answers:


15

一种方法是将以下内容添加到中~/.ssh/rc,其中包含在ssh进入计算机时要运行的命令:

lastlog -u $USER | perl -lane 'END{print "Last login: @F[3..6] $F[8] from $F[2]"}'

该命令将从中获取您上次登录的时间,lastlogin然后对其进行格式化,使其看起来像原始版本。现在可以touch ~/.hushlogin,您仍然会看到该消息。


1
好一个。我最后去了,last -w | grep "$USER" | head -n1 | perl -lane 'END{print "Last login: @F[3..6] $F[8] from $F[2]"}'因为lastlog要截断我的主机名。
Xion345

1
@ Xion345可以使用last -w "$USER" | ...那里
Monty Harder

1
您可能还想知道/ etc / motd是否更改,因此还可以添加:cmp / etc / motd〜/ .hushlogin.motd || cat / etc / motd && cp / etc / motd〜/ .hushlogin.motd
rrauenza

12

有你的.bash_profile电话lastlog -u "$USER"让你的东西很接近。输出如下:

Username         Port     From             Latest
anthony          pts/7    192.168.XX.YY    Sun Feb  7 16:00:40 -0500 2016

我当然在哪里修改了IP地址。

last -w -n 1 获得类似的记录,但来自不同的数据库。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.