{"id":2873,"date":"2018-02-14T17:53:36","date_gmt":"2018-02-14T14:53:36","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-last-command\/"},"modified":"2018-02-14T17:53:36","modified_gmt":"2018-02-14T14:53:36","slug":"linux-last-command-tutorial-for-beginners-8-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-last-command-tutorial-for-beginners-8-examples\/","title":{"rendered":"Linux last Command Tutorial for Beginners (8 Examples)"},"content":{"rendered":"<p>If you are new to system administration, there&#8217;ll be times when you&#8217;ll have to monitor or access login related information. There are multiple tools that you&#8217;ll likely use for this purpose, with one of them being <strong>last<\/strong>. In this tutorial, we will discuss the basic features of this utility using some easy to understand examples.<\/p>\n<p>Before we jump on to the explanation part, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 16.04LTS machine.<\/p>\n<h2 id=\"linux-last-command\">Linux last command<\/h2>\n<p>The <strong>last<\/strong> command displays a list of last logged in users. Following is its syntax:<\/p>\n<p class=\"command\">last [options] [username&#8230;] [tty&#8230;]\n<p>Here is how the man page explains this tool:<\/p>\n<pre readability=\"12\"> last\u00a0 searches\u00a0 back through the \/var\/log\/wtmp file (or the file desig?<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nated by the -f option) and displays a list of all users logged in (and<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 out)\u00a0 since\u00a0 that\u00a0 file was created.\u00a0 One or more usernames and\/or ttys<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 can be given, in which case last will show only\u00a0 the\u00a0 entries\u00a0 matching<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 those\u00a0 arguments.\u00a0 Names of ttys can be abbreviated, thus last 0 is the<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 same as last tty0.<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 When catching a SIGINT signal (generated by the interrupt key,\u00a0 usually<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 control-C)\u00a0 or a SIGQUIT signal, last will show how far it has searched<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 through the file; in the case of the SIGINT signal last will then\u00a0 ter?<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 minate.<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 The\u00a0 pseudo user reboot logs in each time the system is rebooted.\u00a0 Thus<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 last reboot will show a log of all the reboots since the log\u00a0 file\u00a0 was<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 created.<\/p><\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a better idea on how last works.<\/p>\n<h2 id=\"q-how-to-use-last-command\">Q1. How to use last command?<\/h2>\n<p>The basic usage is very easy. All you have to do is to run the &#8216;last&#8217; command sans any options:<\/p>\n<p class=\"command\">last<\/p>\n<p><a class=\"fancybox\" id=\"img-last-basic-output\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/last-basic-output.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-last-command-tutorial-for-beginners-8-examples.png\" alt=\"How to use last command\" width=\"500\" height=\"275\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-customize-output-in-case-of-nonlocal-logins\">Q2. How to customize output in case of non-local logins?<\/h2>\n<p>By default, in case of non-local plugins, last command displays output in the following way:<\/p>\n<p><a class=\"fancybox\" id=\"img-last-non-local\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/last-non-local.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-last-command-tutorial-for-beginners-8-examples-1.png\" alt=\"How to customize output in case of non-local logins\" width=\"550\" height=\"121\" title=\"\"><\/a><\/p>\n<p><em>Note: In the screenshot, we have intentionally blurred some parts containing IP addresses.<\/em><\/p>\n<p>Now, if you want, you can slightly customize this output by moving the IP address related column to extreme right. This can be done using the <strong>-a<\/strong> command line option.<\/p>\n<p><a class=\"fancybox\" id=\"img-last-a-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/last-a-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-last-command-tutorial-for-beginners-8-examples-2.png\" alt=\"customize output of last command\" width=\"550\" height=\"116\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-make-last-read-a-different-file\">Q3. How to make last read a different file?<\/h2>\n<p>As already mentioned in the beginning of the tutorial, the last command reads the <strong>\/var\/log\/wtmp<\/strong> file to prepare its output. However, if you want, you can make the tool read a completely different file. This you can do using the <strong>-f<\/strong> command line option. Of course, you&#8217;ll have to pass the new file name (along with its path) as input to this option.<\/p>\n<p class=\"command\">last -f [new-file-path-and-name]\n<h2 id=\"q-how-to-make-last-print-complete-date-and-time-info\">Q4. How to make last print complete date and time info?<\/h2>\n<p>If you want last to produce complete date and time information in output, use the <strong>-F<\/strong> command line option.<\/p>\n<p class=\"command\">last -F<\/p>\n<p><a class=\"fancybox\" id=\"img-last-F-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/last-F-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-last-command-tutorial-for-beginners-8-examples-3.png\" alt=\"How to make last print complete date and time info\" width=\"550\" height=\"227\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-make-last-show-only-a-specific-number-of-lines\">Q5. How to make last show only a specific number of lines?<\/h2>\n<p>If you want to customize the number of lines the last command shows in output, you can do that using the <strong>-n<\/strong> command line option. Of course, you&#8217;ll have to pass a number to this option as input.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">last -n 3<\/p>\n<p><a class=\"fancybox\" id=\"img-last-n\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/last-n.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-last-command-tutorial-for-beginners-8-examples-4.png\" alt=\"How to make last show only a specific number of lines\" width=\"550\" height=\"84\" title=\"\"><\/a><\/p>\n<p>So you can see the output only contains 3 lines.<\/p>\n<h2 id=\"q-how-to-make-last-suppress-hostname-field-in-output\">Q6. How to make last suppress hostname field in output?<\/h2>\n<p>The third column in last&#8217;s output is hostname information. However, for some reason, if you want the tool to suppress this information, use the -R command line option.<\/p>\n<p class=\"command\">last -R<\/p>\n<p><a class=\"fancybox\" id=\"img-last-R-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/last-R-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-last-command-tutorial-for-beginners-8-examples-5.png\" alt=\"How to make last suppress hostname field in output\" width=\"525\" height=\"550\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-make-last-display-info-for-a-specific-time-period\">Q7. How to make last display info for a specific time period?<\/h2>\n<p>Suppose you want last to only display output based on time &#8211; say, only information from yesterday and today &#8211; then you ca use the <strong>-s<\/strong> and <strong>-t<\/strong> command line options.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">last -s yesterday -t today<\/p>\n<p><a class=\"fancybox\" id=\"img-last-s-t-options\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/last-s-t-options.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-last-command-tutorial-for-beginners-8-examples-6.png\" alt=\"How to make last display info for a specific time period\" width=\"500\" height=\"94\" title=\"\"><\/a><\/p>\n<pre readability=\"9\">Please note the options that take the time argument understand the following formats:<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 YYYYMMDDhhmmss<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 YYYY-MM-DD hh:mm:ss<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 YYYY-MM-DD hh:mm\u00a0\u00a0\u00a0\u00a0\u00a0 (seconds will be set to 00)<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 YYYY-MM-DD\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (time will be set to 00:00:00)<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 hh:mm:ss\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (date will be set to today)<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 hh:mm\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (date will be set to today, seconds to 00)<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 now<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 yesterday\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (time is set to 00:00:00)<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 today\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (time is set to 00:00:00)<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tomorrow\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (time is set to 00:00:00)<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 +5min<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -5days<\/p><\/pre>\n<h2 id=\"q-how-to-make-last-display-things-like-run-level-changes\">Q8. How to make last display things like run level changes?<\/h2>\n<p>To display information like run level changes and system shut down entries, use the <strong>-x<\/strong> command line option.<\/p>\n<p class=\"command\">last -x<\/p>\n<p><a class=\"fancybox\" id=\"img-last-x-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/last-x-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-last-command-tutorial-for-beginners-8-examples-7.png\" alt=\"How to make last display things like run level changes\" width=\"500\" height=\"445\" title=\"\"><\/a><\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Agreed, last isn&#8217;t one of those commands that you&#8217;ll use daily, but there will likely be days where this tool would be of great help. We have covered several major options here in this tutorial &#8211; should be enough to get you started. For more info, head to the utility&#8217;s <a href=\"https:\/\/linux.die.net\/man\/1\/last\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you are new to system administration, there&#8217;ll be times when you&#8217;ll have to monitor or access login related information. There are multiple tools that you&#8217;ll likely use for this purpose, with one of them being last. In this tutorial, we will discuss the basic features of this utility using some easy to understand examples. &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-2873","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=2873"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2873\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=2873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=2873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=2873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}