A secure and reliable utility for monitoring SSH connections to a server with Telegram notifications.
- Maximum user identification: IP address, key fingerprint, key comment, connection type
- Flexible notifications: Separate sound and silent messages for different connection types
- Reliability: Prevention of duplicate notifications during parallel sessions
- Retry logic: Automatic retries on network or Telegram API failures
- Flexible configuration: Configuration through config file
- Security: Minimal dependencies, works without SSH client modifications
- Linux server with OpenSSH
- Python 3.6+
- curl
- bash 4.0+
- Root privileges for installation
# Clone the repository
git clone https://github.com/B4DCATs/ssh-login-alert
cd ssh-login-alert
# Run the installation
sudo ./install.shAfter installation, the repository can be removed:
# After successful installation
cd ..
rm -rf ssh-login-alert- Files are copied to
/opt/ssh-alert/and/etc/ssh-alert/ - SSH integration is configured through
/etc/ssh/sshrc - Log rotation configuration is created
- Interactive Telegram setup is launched
- Configuration is tested
-
Copy files:
sudo mkdir -p /opt/ssh-alert /etc/ssh-alert sudo cp ssh-alert-enhanced.sh /opt/ssh-alert/ sudo cp key-parser.py /opt/ssh-alert/ sudo cp config.conf /etc/ssh-alert/ sudo cp logrotate.conf /etc/logrotate.d/ssh-alert sudo chmod +x /opt/ssh-alert/*.sh sudo chmod +x /opt/ssh-alert/*.py
-
Configure SSH:
sudo tee /etc/ssh/sshrc > /dev/null << 'EOF' #!/bin/bash # SSH Alert Integration if [ -n "${SSH_ALERT_DISABLED:-}" ]; then exit 0 fi /opt/ssh-alert/ssh-alert-enhanced.sh & EOF sudo chmod +x /etc/ssh/sshrc
-
Configure settings:
sudo nano /etc/ssh-alert/config.conf
Edit the file /etc/ssh-alert/config.conf:
# Telegram Bot Configuration
TELEGRAM_BOT_TOKEN="your_bot_token_here"
TELEGRAM_CHAT_ID="your_chat_id_here"
# Server Information
SERVER_NAME="server01"
SERVER_DOMAIN="example.com"
# Notification Settings
NOTIFY_INTERACTIVE_SESSIONS=true
NOTIFY_TUNNELS=false
NOTIFY_COMMANDS=false
DISABLE_NOTIFICATION_SOUND_FOR_TUNNELS=true
# Rate Limiting (seconds)
RATE_LIMIT_PER_IP=300
RATE_LIMIT_PER_KEY=60For maximum user identification, configure authorized_keys:
sudo ./setup-authorized-keys.shOr manually add SSH_USER to keys:
environment="SSH_USER=alice@example.com" ssh-rsa AAAAB3NzaC1yc2E... alice@laptop
For automated connections (pipelines, monitoring, CI/CD runners), you can exclude connections from notifications using three methods:
Exclude SSH keys by their comment in authorized_keys:
# Add key comment exclusion
sudo ./manage-exclusions.sh add key "pipeline@ci"
sudo ./manage-exclusions.sh add key "deploy@automation"
# Remove key comment exclusion
sudo ./manage-exclusions.sh remove key "pipeline@ci"Exclude connections from specific IP addresses (useful for password-based runners):
# Add IP exclusion
sudo ./manage-exclusions.sh add ip "192.168.1.100"
sudo ./manage-exclusions.sh add ip "10.0.0.50"
# Remove IP exclusion
sudo ./manage-exclusions.sh remove ip "192.168.1.100"Exclude connections by username:
# Add username exclusion
sudo ./manage-exclusions.sh add user "gitlab-runner"
sudo ./manage-exclusions.sh add user "jenkins"
# Remove username exclusion
sudo ./manage-exclusions.sh remove user "gitlab-runner"# View all exclusions
sudo ./manage-exclusions.sh list
# View specific type
sudo ./manage-exclusions.sh list key
sudo ./manage-exclusions.sh list ip
sudo ./manage-exclusions.sh list user
# Clear all exclusions of a type
sudo ./manage-exclusions.sh clear key
sudo ./manage-exclusions.sh clear ip
sudo ./manage-exclusions.sh clear userUsage examples:
- Key comments:
pipeline@ci,deploy@automation,monitoring@system - IP addresses:
192.168.1.100,10.0.0.50, GitLab Runner IP - Usernames:
gitlab-runner,jenkins,deploy-bot
Note: Changes take effect immediately for new connections. IP and username exclusions are perfect for CI/CD runners that connect via password authentication.
-
Create a bot:
- Send
/newbotto @BotFather - Follow the instructions to create a bot
- Save the received token
- Send
-
Get Chat ID:
- Add the bot to a chat or send it a message
- Go to the link:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates - Find
chat.idin the response
# View logs
sudo tail -f /var/log/ssh-alert.log
# Test configuration
sudo /opt/ssh-alert/ssh-alert-enhanced.sh
# Log management
sudo /opt/ssh-alert/check-log-rotation.sh status # Check rotation status
sudo /opt/ssh-alert/check-log-rotation.sh test # Test configuration
sudo /opt/ssh-alert/check-log-rotation.sh rotate # Force rotation
# Exclusion management
sudo ./manage-exclusions.sh list # Show all exclusions
sudo ./manage-exclusions.sh add key "pipeline@ci" # Add key exclusion
sudo ./manage-exclusions.sh add ip "192.168.1.100" # Add IP exclusion
sudo ./manage-exclusions.sh add user "gitlab-runner" # Add user exclusion
sudo ./manage-exclusions.sh remove key "pipeline@ci" # Remove key exclusion
sudo ./manage-exclusions.sh clear key # Clear key exclusions
# Uninstall
sudo /opt/ssh-alert/uninstall.shSSH Alert distinguishes the following connection types:
- Interactive shell - Interactive session (default with sound)
- Tunnel - SSH tunnel (default without sound)
- Command execution - Command execution (configurable)
๐ SSH Login Alert:
Host IP: 203.0.113.1 / 192.168.1.100
Host: server01.example.com
Person: alice@example.com
IP: 198.51.100.50
Type: Interactive shell
Key: SHA256:abcd1234...
Time: 2024-01-15 14:30:25 UTC
-
Restrict access to configuration:
sudo chmod 600 /etc/ssh-alert/config.conf sudo chown root:root /etc/ssh-alert/config.conf
-
Configure firewall:
# Allow SSH only from trusted IPs sudo ufw allow from 192.168.1.0/24 to any port 22 -
Use keys instead of passwords:
sudo nano /etc/ssh/sshd_config # Set: PasswordAuthentication no sudo systemctl restart sshd
SSH Alert maintains detailed logs:
# View logs
sudo tail -f /var/log/ssh-alert.log
# JSON logging (optional)
# Set JSON_LOGGING=true in config.confSSH Alert automatically configures log rotation through logrotate:
# Check rotation status
make check-logs
# Test rotation configuration
make test-logs
# Force rotation
make rotate-logs
# Manual check
sudo ./check-log-rotation.sh statusRotation settings:
- ๐ Daily rotation of logs
- ๐ฆ 30 days of compressed log storage
- ๐๏ธ Compression of old logs
- ๐ Minimum size 100KB for rotation
- ๐ Maximum size 10MB for forced rotation
- ๐งน Cleanup of rate limiting temporary files
-
Post-installation errors:
# If you see errors like "[[ not found" or "config.conf not found" sudo ./fix-installation.sh -
Notifications not arriving:
# Check token and chat_id sudo grep -E "TELEGRAM_BOT_TOKEN|TELEGRAM_CHAT_ID" /etc/ssh-alert/config.conf # Check logs sudo tail -f /var/log/ssh-alert.log
-
Script not starting:
# Check permissions ls -la /opt/ssh-alert/ssh-alert-enhanced.sh # Check syntax bash -n /opt/ssh-alert/ssh-alert-enhanced.sh
-
Python errors:
# Check Python version python3 --version # Test parser python3 /opt/ssh-alert/key-parser.py get-info
Enable debug logs:
sudo nano /etc/ssh-alert/config.conf
# Set: LOG_LEVEL="DEBUG"# System status
sudo systemctl status ssh-alert 2>/dev/null || echo "Service not installed"
# Active connections
sudo ss -tnp | grep sshd
# Recent notifications
sudo grep "SSH alert sent" /var/log/ssh-alert.log | tail -5SSH Alert can integrate with monitoring systems through JSON logs:
# Enable JSON logging
echo 'JSON_LOGGING=true' | sudo tee -a /etc/ssh-alert/config.conf
# Parse logs
sudo tail -f /var/log/ssh-alert.log | jq '.'# Update from repository
git pull origin main
sudo ./install.sh# Create backup
sudo cp -r /opt/ssh-alert /opt/ssh-alert.backup
sudo cp /etc/ssh-alert/config.conf /etc/ssh-alert/config.conf.backup
# Update files
sudo cp ssh-alert-enhanced.sh /opt/ssh-alert/
sudo cp key-parser.py /opt/ssh-alert/
sudo cp uninstall.sh /opt/ssh-alert/
sudo cp check-log-rotation.sh /opt/ssh-alert/
sudo cp logrotate.conf /etc/logrotate.d/ssh-alert# Run the uninstall script
sudo /opt/ssh-alert/uninstall.sh- โ All SSH Alert files
- โ
SSH integration from
/etc/ssh/sshrc - โ Systemd service
- โ Log rotation configuration
- โ Temporary files and cache
- โ Backup copies are created
# Stop processes
sudo pkill -f ssh-alert
# Remove files
sudo rm -rf /opt/ssh-alert
sudo rm -rf /etc/ssh-alert
# Clear SSH integration
sudo rm -f /etc/ssh/sshrc
# Remove temporary files
sudo rm -f /tmp/ssh-alert.lock
sudo rm -rf /tmp/ssh-alert-rate-limitThis project is distributed under the MIT license. See the LICENSE file for details.
- Fork the repository
- Create a branch for a new feature (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
If you encounter problems or have questions:
- Check the troubleshooting section
- Create an Issue
- Refer to the documentation
- Support for other messengers (Slack, Discord)
- Web interface for management
- Integration with SIEM systems
- Machine learning for anomaly detection
- IPv6 support
- Advanced connection analytics