LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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

Reply
 
LinkBack Search this Thread
Old 11-12-2008, 10:24 AM   #1
sulekha
Member
 
Registered: Dec 2004
Location: India
Distribution: ubuntu 10.04 , centos 5.5 , Debian lenny, Freenas
Posts: 292

Rep: Reputation: 35
Question interview question


Hi all,

the following is the question which was asked in an interview

Give an example of a set of shell commands to tell how many times “bob” has logged on to the system this month ?


The answer i managed to google out was

Depends, checking secure.log depends on how often logrotate is running and how long it keeps things around.


Is there any other better answer ?
 
Old 11-12-2008, 10:29 AM   #2
amani
Senior Member
 
Registered: Jul 2006
Location: Kolkata, India
Distribution: 64-bit GNU/Linux, Kubuntu64, Fedora QA, Slackware,
Posts: 2,438

Rep: Reputation: Disabled
grep the authentication log on most distros.

A better way is to base it on syslogd configuration
 
Old 11-12-2008, 11:29 AM   #3
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,121

Rep: Reputation: 121Reputation: 121
On most systems, it's setup by default to log all users logins to wtmp and is rotated monthly by default in the logrotate.conf file:

Code:
/var/log/wtmp {
    monthly
    create 0664 root utmp
    rotate 1
}
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.
 
Old 11-12-2008, 12:00 PM   #4
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 17
Using last and a bit of bash trickery ...

Code:
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 ...

Code:
a=$(last -t 20081001000000 | wc -l)
b=$(last -t 20081101000000 | wc -l)
last -$a | tail -$(( $a - $b )) | grep -w bob | wc -l
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 ...
 
Old 11-12-2008, 11:15 PM   #5
sulekha
Member
 
Registered: Dec 2004
Location: India
Distribution: ubuntu 10.04 , centos 5.5 , Debian lenny, Freenas
Posts: 292

Original Poster
Rep: Reputation: 35
Quote:
Originally Posted by trickykid View Post
On most systems, it's setup by default to log all users logins to wtmp and is rotated monthly by default in the logrotate.conf file:

Code:
/var/log/wtmp {
    monthly
    create 0664 root utmp
    rotate 1
}
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


NB:- I use ubuntu 8.04.1
 
Old 11-13-2008, 09:33 AM   #6
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,121

Rep: Reputation: 121Reputation: 121
Quote:
Originally Posted by sulekha View Post
NB:- I use ubuntu 8.04.1
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.

Last edited by trickykid; 11-13-2008 at 09:35 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interview Question runnerpaul General 7 05-17-2007 09:03 PM
Interview poz Solaris / OpenSolaris 5 09-22-2006 10:03 AM
why dont you interview ..... floppywhopper LQ Suggestions & Feedback 3 05-23-2005 08:02 PM
Who to Interview? jeremy LQ Suggestions & Feedback 46 12-12-2004 10:56 AM
Interview With The Moderator class_struggle LQ Suggestions & Feedback 5 07-26-2004 07:34 AM


All times are GMT -5. The time now is 11:34 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration