Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
You can use the last command to get the current month and by default it'll keep one month prior as you can see the rotate in the conf file as rotate 1. Change this to keep more months.
To get an accurate count of the current month, just do something like this:
Code:
last | grep username | wc -l
This would give you the amount of times username logged in.
To specify another wtmp file or previous month log, just pass something like this:
Code:
last -f /var/log/wtmp.1 | grep username | wc -l
That would be the correct answer givin what they actually asked. They wanted an example of commands used to get a number of logins, not checking sucure.log for how long they keep logs around, etc.
last | head -$(( $(last | wc -l) - $(last -t 20081101000000 | wc -l) | grep -w bob | wc -l
However, this would only work for the current month. If you would want it for a different month, this isn't much help, though it could be modified I suppose ...
Which would give you the logins for October and not November. $a and $b could be modified to give you a varying array of dates. One month, two months, the past six months ... whatever. You could even use the date command to pull out the times that you want if it were scripted.
Last edited by Hobbletoe; 11-12-2008 at 12:02 PM.
Reason: Changed the final commands for just the count, and not to list all logins ...
You can use the last command to get the current month and by default it'll keep one month prior as you can see the rotate in the conf file as rotate 1. Change this to keep more months.
To get an accurate count of the current month, just do something like this:
Code:
last | grep username | wc -l
This would give you the amount of times username logged in.
To specify another wtmp file or previous month log, just pass something like this:
Code:
last -f /var/log/wtmp.1 | grep username | wc -l
That would be the correct answer givin what they actually asked. They wanted an example of commands used to get a number of logins, not checking sucure.log for how long they keep logs around, etc.
I tried this
user@ubuntu:~$ last | grep username | wc -l
last: /var/log/wtmp: No such file or directory
Perhaps this file was removed by the operator to prevent logging last info.
0
I'm not an Ubuntu user so they might not use wtmp like most other distro's. Find the file that logs user logins and substitute it, might have to change a few things around.
But using the command last, it refers to the wtmp so you might just have to touch the file or create it before it starts logging to it.
But that would be the first response to the interviewer? Which distribution is this on? You can impress them by saying Ubuntu wouldn't use wtmp but RHEL/Fedora/CentOS and others use wtmp for login logs. But I'd imagine them being a business, they're probably using Red Hat or a clone of it that most likely will have wtmp.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.