{"id":14098,"date":"2023-04-22T01:28:56","date_gmt":"2023-04-22T01:28:56","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=14098"},"modified":"2023-04-28T09:46:22","modified_gmt":"2023-04-28T09:46:22","slug":"how-to-get-email-alerts-for-ssh-login-on-linux-server","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-get-email-alerts-for-ssh-login-on-linux-server\/","title":{"rendered":"How To Get Email Alerts for SSH Login on Linux Server"},"content":{"rendered":"<p>Monitoring SSH logins to your Linux server is crucial for maintaining the security of your system. One way to stay informed about login activity is by setting up email alerts whenever someone logs in via SSH. In this step-by-step guide, we will show you how to configure your Linux server to send email notifications when an SSH login occurs.<\/p>\n<h2>Step 1: Install the mail utility<\/h2>\n<p>First, you need to install a mail utility that will allow your server to send emails. We&#8217;ll use &#8220;mailutils&#8221; for this purpose. To install mailutils on your Ubuntu or Debian-based system, run the following command:<\/p>\n<pre>\r\nsudo apt-get update\r\nsudo apt-get install -y mailutils\r\n<\/pre>\n<p>On CentOS, RHEL, or Fedora-based systems, use the following command:<\/p>\n<pre>\r\nsudo yum install -y mailx\r\n<\/pre>\n<h2>Step 2: Configure the email script<\/h2>\n<p>Next, create a script that will send an email notification with the SSH login details. Create a new file called &#8220;ssh_login_notify.sh&#8221; in the &#8220;\/usr\/local\/bin&#8221; directory:<\/p>\n<pre>\r\nsudo nano \/usr\/local\/bin\/ssh_login_notify.sh\r\n<\/pre>\n<p>Add the following lines to the script:<\/p>\n<pre>\r\n#!\/bin\/bash\r\n\r\n# Change this to your email address\r\nrecipient=\"your-email@example.com\"\r\nsubject=\"SSH Login Alert\"\r\nmessage=\"SSH login detected on $(hostname) at $(date) by user $(whoami) from $(echo $SSH_CONNECTION | awk '{print $1}')\"\r\n\r\necho \"$message\" | mail -s \"$subject\" \"$recipient\"\r\n<\/pre>\n<p>Replace &#8220;your-email@example.com&#8221; with your email address. Save and exit the editor.<\/p>\n<p>Make the script executable:<\/p>\n<pre>\r\nsudo chmod +x \/usr\/local\/bin\/ssh_login_notify.sh\r\n<\/pre>\n<h2>Step 3: Configure SSH to run the script on login<\/h2>\n<p>Edit the SSH configuration file &#8220;\/etc\/ssh\/sshd_config&#8221; to run the email script whenever someone logs in:<\/p>\n<pre>\r\nsudo nano \/etc\/ssh\/sshd_config\r\n<\/pre>\n<p>Add the following line at the end of the file:<\/p>\n<pre>\r\nForceCommand \/usr\/local\/bin\/ssh_login_notify.sh; $SSH_ORIGINAL_COMMAND\r\n<\/pre>\n<p>Save and exit the editor.<\/p>\n<p>Restart the SSH service to apply the changes:<\/p>\n<pre>\r\nsudo systemctl restart sshd\r\n<\/pre>\n<p>Now, you will receive an email notification whenever someone logs in to your server via SSH.<\/p>\n<h2>Commands Mentioned:<\/h2>\n<ul>\n<li><span class=\"fw-bold\">apt-get update<\/span> &#8211; Update package repositories<\/li>\n<li><span class=\"fw-bold\">apt-get install<\/span> &#8211; Install specified packages and their dependencies<\/li>\n<li><span class=\"fw-bold\">yum install<\/span> &#8211; Install specified packages and their dependencies (CentOS\/RHEL\/Fedora)<\/li>\n<li><span class=\"fw-bold\">nano<\/span> &#8211; Open the specified file in the nano text editor<\/li>\n<li><span class=\"fw-bold\">chmod<\/span> &#8211; Change the permissions of a file<\/li>\n<li><span class=\"fw-bold\">systemctl restart<\/span> &#8211; Restart a system service<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>In this guide, we&#8217;ve shown you how to set up email alerts for SSH logins on your Linux server. By configuring your server to send notifications, you can stay informed about SSH login activity and take appropriate action if you notice any unauthorized access.<\/p>\n<p>Please note that while this method is useful for monitoring login activity, it&#8217;s crucial to maintain the overall security of your server by keeping your system up-to-date, using strong passwords, enabling key-based authentication, and setting up firewalls. Additionally, you can implement other security measures, such as using intrusion detection systems (IDS) and intrusion prevention systems (IPS), configuring two-factor authentication (2FA) for SSH logins, and regularly auditing your server&#8217;s security settings.<\/p>\n<p>By combining these security practices with email alerts for SSH logins, you can proactively monitor and protect your Linux server from unauthorized access and potential security threats.<\/p>\n<p>We hope this guide has helped you set up email alerts for SSH logins on your Linux server. If you have any questions, comments, or suggestions for improvement, please feel free to share your thoughts in the comments section below. Your feedback is invaluable to us, and it helps us create better and more informative content for our users.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Monitoring SSH logins to your Linux server is crucial for maintaining the security of your system. One way to stay informed about login activity is by setting up email alerts&#8230;<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[996],"tags":[1744],"class_list":["post-14098","post","type-post","status-publish","format-standard","hentry","category-linux","tag-security"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/14098","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=14098"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/14098\/revisions"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=14098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=14098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=14098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}