PAM Audit Logging: Shell TTY User Activity
In this article
To audit shell TTY user activity with PAM, you configure the pam_tty_audit module in your PAM stack to enable auditing for specific users or groups, then read the resulting audit records from the kernel audit log. This gives you a detailed, kernel-level record of every command typed in an interactive shell session, including the exact time, user, and terminal.
What Is PAM and Why Auditing Shell Activity Matters
Pluggable Authentication Modules (PAM) is the framework Linux systems use to handle authentication for services like login, SSH, and sudo. It sits between the application and the actual authentication mechanisms, allowing system administrators to insert modules that perform additional tasks—like enforcing password strength, locking accounts, or, in this case, recording what a user does in a terminal.
Auditing shell activity is crucial for security and compliance. If a user gains unauthorized access or a disgruntled employee performs malicious actions, you need a reliable record of exactly what commands were executed. Standard shell history files (.bash_history) are easy to modify or delete by the user, making them unreliable for forensic purposes. Kernel-level auditing via PAM captures the raw TTY input and output, which is far more tamper-resistant.
How pam_tty_audit Works
The pam_tty_audit module works by enabling or disabling TTY auditing for the current session. When enabled, the kernel records all data read from and written to the terminal device associated with the session. This includes every keystroke, command, and output displayed on the screen. The audit records are then sent to the kernel audit subsystem and can be viewed with tools like ausearch or aureport.
The module has two primary actions:
enable: Turns on auditing for the session. This is typically placed in thesessionstack of PAM.disable: Turns off auditing. Useful for specific services where you don't want to record activity, such as a file transfer daemon.
You can also specify which users or groups to audit using the user and group parameters. If you don't specify any, the module will audit all users (or none, depending on the enable or disable action).
Configuration Steps: Enabling TTY Auditing
To enable TTY auditing for all users on a Linux system, you need to edit the PAM configuration files. The exact file depends on the service you want to audit. For SSH sessions, you'd typically add the module to /etc/pam.d/sshd; for local login, /etc/pam.d/login.
- Open the PAM configuration file for the service you want to audit in a text editor with root privileges. For example:
sudo nano /etc/pam.d/sshd - Add the following line to the session section (usually near the top, after any existing session lines):
session required pam_tty_audit.so enable - Save the file and restart the service (e.g.,
sudo systemctl restart sshd). For login, you may need to reboot or just test with a new login.
To audit only specific users, add the user parameter with a comma-separated list:
session required pam_tty_audit.so enable user=alice,bob
To audit a group, use group=:
session required pam_tty_audit.so enable group=developers
If you want to audit all users except a few, you can use the disable action for those users in a separate line, but note that PAM processes lines in order, so you may need to place the disable line after the enable line.
Reading the Audit Logs
Once TTY auditing is enabled, the kernel generates audit records that are stored in the audit log, typically at /var/log/audit/audit.log. To view them, you use the ausearch tool.
To search for TTY audit records, use:
ausearch -m TTY
This will show all TTY audit events. Each record contains the user ID, terminal device, timestamp, and the data that was typed or displayed. For example, a record might look like:
type=TTY msg=audit(1612345678.123:456) : tty pid=1234 uid=1000 auid=1000 ses=1 major=4 minor=1 comm=bash data=ls -la
The data field contains the actual command or output. You can also use aureport --tty to get a summary report of TTY activity.
Note that the audit log can grow very quickly, so it's essential to configure log rotation and consider using a central log server for long-term storage.
Common Pitfalls and Troubleshooting
One common issue is that the module may not work if the kernel or audit daemon isn't properly configured. Ensure the audit daemon is running (sudo systemctl status auditd) and that the kernel has audit support enabled.
Another pitfall is that TTY auditing only captures data that passes through the terminal driver. If a program uses a pseudo-terminal (PTY) or bypasses the TTY, it may not be captured. Also, commands run in background or via nohup may not be associated with a TTY and thus won't be recorded.
If you're not seeing any audit records, check that the PAM line is in the correct section and that the service is actually using PAM. Some services, like cron, do not use PAM sessions by default.
Performance overhead is generally minimal, but on very busy systems with many concurrent sessions, the audit log can become a bottleneck. Consider auditing only the users or groups that need it.
Alternatives to PAM TTY Auditing
PAM TTY auditing is not the only way to monitor shell activity. Other approaches include:
- Shell history with timestamps: Configure
bashto append history with timestamps, but this is easily bypassed. - Process accounting: Tools like
acctorpsacctrecord command execution, but not the full TTY input/output. - Keylogger-like tools: Programs like
scriptcan record sessions, but they must be manually started. - Auditd's execve rule: You can use auditd to log every
execvesystem call, which captures command execution but not the full TTY stream.
PAM TTY auditing is the most comprehensive for capturing the exact terminal interaction, but it's also the most resource-intensive.
Frequently Asked Questions
Does pam_tty_audit log all commands typed in a shell?
Yes, when enabled, it logs every character typed into the terminal and every character output to the terminal. This includes commands, their output, and even passwords typed (which is a security concern, so be cautious).
Can I audit only certain users?
Yes, use the user= or group= parameters to specify which users or groups to audit. For example, session required pam_tty_audit.so enable user=alice.
How do I disable TTY auditing for a specific service?
Add a line with the disable action for that service's PAM file. For example, in /etc/pam.d/ftp, add session required pam_tty_audit.so disable.
What is the difference between TTY auditing and shell history?
Shell history is stored by the shell (e.g., bash) and can be easily edited or cleared by the user. TTY auditing is done by the kernel and records the raw terminal data, which is much harder to tamper with and provides a more complete picture of the session.
Will TTY auditing slow down my system?
There is a small performance cost, as every keystroke is captured and logged. On a typical system with few concurrent sessions, this is negligible. On a high-traffic server, you may notice some I/O overhead, so it's best to audit only the users that need it.
Security and Privacy Considerations
TTY auditing captures sensitive information, including passwords and other confidential data typed in the terminal. This means the audit logs themselves become a security risk. Ensure the audit log is only readable by root and is properly protected. Also, be aware of privacy regulations; in some jurisdictions, monitoring user activity without consent may be illegal. Always inform users that their sessions are being audited, and consult with legal counsel if necessary.
Additionally, the audit log can grow extremely large, so implement log rotation and consider sending logs to a remote server for secure storage. The default auditd configuration usually handles rotation, but you may need to adjust the size and number of logs based on your usage.