There are lots of monitoring apps for servers, and they’ve done a lot to help adoption of Linux with sysadmins who aren’t used to the Terminal or who have a real need for graphical representations of data. However, Linux has been a multi-user system since the beginning, and UNIX long before that, so there are built-in tools that go back 40 years to help you monitor who’s logged into your server, who’s using resources, and for what.
You don’t have to be paranoid or even nosy to justify keeping a close watch on a server that’s been made your responsibility. The psacct package contains several commands to gather detailed reports about user status and activity.
accton
Not all commands in the acct
utilities require that you activate accounting, but many do. If you intend to use acct
commands, you should enable accounting with the accton
command.
To activate:
$ sudo accton on
By default, accounting records are stored in /var/account/pacct
. This file could feasibly become quite large, so use logrotate
or a similar tool to ensure proper log management.
To deactivate accounting:
$ sudo accton off
ac
The ac
command prints statistics about connection times. If you need to get an overview of how active users have been on a system, the --individual-totals
option provides that. It generates its report in hours based on logins and logouts recorded in the /var/log/wtmp
file.
The accounting file wtmp
is maintained by init(8)
and login(1)
but neither ac
or login
actually creates the file. If wtmp
doesn’t exist, then no report is generated, but you can point ac
to an alternate location using the --file
option. If a wtmp
file doesn’t exist on your system, you can create an empty wtmp
file to enable reporting on your system.
To get a report on login times for individual users:
$ ac --individual-totals
seth 20.16
larry 43.60
curly 10.32
moe 35.11
You can also get daily totals:
$ ac --daily-totals
Jan 20 total 22.61
Jan 21 total 73.60
Jan 22 total 84.00
Jan 23 total 100.69
Jan 24 total 18.24
Jan 25 total 2.43
Jan 27 total 35.36
Today total 62.13
lastcomm
The lastcomm
command displays the last commands issued on the system for a given user. If no user is specified, then a report on the current user is generated.
$ sudo lastcomm --strict-match --user curly --tty pts/2
basename curly pts/2 0.00 secs Tue Jan 28 15:41
ps curly pts/2 0.00 secs Tue Jan 28 15:41
bash F curly pts/2 0.00 secs Tue Jan 28 15:41
manpath curly pts/2 0.00 secs Tue Jan 28 15:41
bash F curly pts/2 0.00 secs Tue Jan 28 15:41
tclsh curly pts/2 0.00 secs Tue Jan 28 15:41
bash F curly pts/2 0.00 secs Tue Jan 28 15:41
bash F curly pts/2 0.00 secs Tue Jan 28 15:41
sed curly pts/2 0.00 secs Tue Jan 28 15:41
The commands listed by lastcomm
aren’t necessarily commands a user launched interactively. For instance, simply by logging in, a user spawns nearly 40 items in the output of lastcomm
, so it can be overwhelming. Coupled with grep
, though, it’s an easy way to get a sense of a user’s session history.
sa
The sa
command summarizes accounting information derived from the /var/account/pacct
file. If you’re auditing the activities of users, then the --print-users
option prints the user name before each command:
$ sudo sa --print-users
root 0.00 cpu 579k mem 0 io accton
root 0.03 cpu 64064k mem 0 io sudo
seth 0.00 cpu 56752k mem 0 io bash *
seth 0.00 cpu 54080k mem 0 io sed
seth 0.00 cpu 56752k mem 0 io bash *
larry 0.00 cpu 56752k mem 0 io bash *
curly 0.00 cpu 56752k mem 0 io bash *
moe 0.00 cpu 56752k mem 0 io bash *
seth 0.00 cpu 54080k mem 0 io ls
Alternatively, you can get just a summary for each user:
$ sudo sa --user-summary
1065 2169.59re 0.97cp 0avio 49373k
seth 812 1117.11re 0.83cp 0avio 58163k
root 199 1052.42re 0.14cp 0avio 21314k
larry 41 0.00re 0.00cp 0avio 19403k
curly 1 0.06re 0.00cp 0avio 6706k
moe 12 0.00re 0.00cp 0avio 25888k
[...]
The columns displayed, in addition to user names, report on CPU (real time and CPU time), I/O operations per command (average and total), and so on. They can be configured using options such as --sort-tio
for total I/O, --sort-cpu-avmem
to sort CPU time by average memory usage, and so on. All sorting filters can be reversed with the --reverse-sort
option.
User profiling
Combined with tools like who, w, and ps, you can get a sense for how users are spending system resources, which commands may be problematic, and what server upgrades could be useful in the future. Because the acct
suite is terminal-based, it can be scripted and utilized by other tools, allowing you to create a customized reporting mechanism.
[ Want to try out Red Hat Enterprise Linux? Download it now for free. ]
저자 소개
Seth Kenlon is a Linux geek, open source enthusiast, free culture advocate, and tabletop gamer. Between gigs in the film industry and the tech industry (not necessarily exclusive of one another), he likes to design games and hack on code (also not necessarily exclusive of one another).
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
오리지널 쇼
엔터프라이즈 기술 분야의 제작자와 리더가 전하는 흥미로운 스토리
제품
- Red Hat Enterprise Linux
- Red Hat OpenShift Enterprise
- Red Hat Ansible Automation Platform
- 클라우드 서비스
- 모든 제품 보기
툴
체험, 구매 & 영업
커뮤니케이션
Red Hat 소개
Red Hat은 Linux, 클라우드, 컨테이너, 쿠버네티스 등을 포함한 글로벌 엔터프라이즈 오픈소스 솔루션 공급업체입니다. Red Hat은 코어 데이터센터에서 네트워크 엣지에 이르기까지 다양한 플랫폼과 환경에서 기업의 업무 편의성을 높여 주는 강화된 기능의 솔루션을 제공합니다.