{"id":1375,"date":"2026-09-03T11:37:53","date_gmt":"2026-09-03T06:07:53","guid":{"rendered":"https:\/\/cpanelfree.com\/blog\/how-to-disable-root-ssh-login-create-sudo-user-ubuntu\/"},"modified":"2026-09-03T12:32:24","modified_gmt":"2026-09-03T07:02:24","slug":"how-to-disable-root-ssh-login-create-sudo-user-ubuntu","status":"publish","type":"post","link":"https:\/\/cpanelfree.com\/blog\/how-to-disable-root-ssh-login-create-sudo-user-ubuntu\/","title":{"rendered":"How to Disable Root SSH Login and Create a Sudo User on Ubuntu (Best Practices)"},"content":{"rendered":"<div style=\"background-color: #f8fafc;border-left: 4px solid #a855f7;padding: 20px;border-radius: 6px;margin-bottom: 25px\">\n<p style=\"margin: 0;font-size: 16px;color: #1e293b\">\n        <strong>Quick Answer:<\/strong> To disable root login on Ubuntu, create a new administrative user (<code>adduser deployer<\/code>), add the user to the sudo group (<code>usermod -aG sudo deployer<\/code>), copy authorized SSH keys to <code>\/home\/deployer\/.ssh\/<\/code>, edit <code>\/etc\/ssh\/sshd_config<\/code> to set <code>PermitRootLogin no<\/code>, and restart OpenSSH with <code>sudo systemctl restart ssh<\/code>.\n    <\/p>\n<\/div>\n<h2>Why Direct Root Login is a Critical Security Risk<\/h2>\n<p>In Linux systems administration, the <code>root<\/code> superuser possesses absolute power to modify files, delete databases, and reconfigure kernels without confirmation. Because the <code>root<\/code> username exists on every Linux system, 100% of automated brute-force attacks target this exact account name.<\/p>\n<p>Enforcing the <strong>Principle of Least Privilege<\/strong> requires disabling direct root login and granting administrative privileges only via <code>sudo<\/code>, creating a traceable audit trail in system logs.<\/p>\n<h2>Step 1: Creating a Dedicated Administrative User<\/h2>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Create a new non-root user (e.g. sysadmin)\nsudo adduser sysadmin\n\n# Add the user to the sudo administrative group\nsudo usermod -aG sudo sysadmin<\/pre>\n<h2>Step 2: Migrating SSH Keys to the New Sudo User<\/h2>\n<p>Copy your public SSH key to the new user&#8217;s profile to enable passwordless authentication:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Create SSH directory with strict permissions\nsudo mkdir -p \/home\/sysadmin\/.ssh\nsudo cp \/root\/.ssh\/authorized_keys \/home\/sysadmin\/.ssh\/\nsudo chown -R sysadmin:sysadmin \/home\/sysadmin\/.ssh\nsudo chmod 700 \/home\/sysadmin\/.ssh\nsudo chmod 600 \/home\/sysadmin\/.ssh\/authorized_keys<\/pre>\n<h2>Step 3: Disabling Root Login in OpenSSH Configuration<\/h2>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<p>Locate the <code>PermitRootLogin<\/code> directive and update it:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Disable direct root access\nPermitRootLogin no\n\n# Disable insecure password logins (SSH keys only)\nPasswordAuthentication no<\/pre>\n<h2>Step 4: Testing the Configuration and Restarting SSH<\/h2>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Verify configuration syntax\nsudo sshd -t\n\n# Restart OpenSSH daemon\nsudo systemctl restart ssh<\/pre>\n<p><strong>Warning:<\/strong> Keep your existing root terminal session open! Open a second terminal window and test logging in with your new user:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">ssh sysadmin@your-server-ip\nsudo whoami  # Should output: root<\/pre>\n<h2>Configuring Passwordless Sudo for Specific Automated Tasks<\/h2>\n<p>If you deploy automated CI\/CD runners (such as GitHub Actions or GitLab Runners) using your sudo user, you can configure granular sudo permissions without exposing full root access. Edit the sudoers file using <code>visudo<\/code>:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">sudo visudo -f \/etc\/sudoers.d\/deployer<\/pre>\n<p>Allow the deployer user to restart Nginx and PHP-FPM without prompting for a password:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">deployer ALL=(ALL) NOPASSWD: \/usr\/bin\/systemctl reload nginx, \/usr\/bin\/systemctl restart php8.3-fpm<\/pre>\n<h2>Auditing Sudo Execution Logs in \/var\/log\/auth.log<\/h2>\n<p>Every single command executed with <code>sudo<\/code> is recorded with an immutable timestamp, user identity, and working directory. Review administrative actions with:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">sudo grep 'sudo:' \/var\/log\/auth.log | tail -n 20<\/pre>\n<h2>Advanced Sudo Privilege Configuration &amp; User Expirations<\/h2>\n<p>In team development environments where multiple engineers access production infrastructure, granular sudoers rules ensure users only execute the exact tools required for their responsibilities:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># \/etc\/sudoers.d\/developers\n# Allow developer group to inspect system logs and reload web services only\n%developers ALL=(ALL) \/usr\/bin\/systemctl status *, \/usr\/bin\/systemctl reload nginx, \/usr\/bin\/journalctl<\/pre>\n<h2>Setting Account Password Expiration and Inactivity Locks<\/h2>\n<p>Prevent orphaned administrative accounts from remaining accessible indefinitely by enforcing account expiration dates and automatic lockout policies:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Set account password expiration to 90 days\nsudo chage -M 90 sysadmin\n\n# Lock inactive accounts automatically after 30 days of inactivity\nsudo chage -I 30 sysadmin\n\n# Review user password aging status\nsudo chage -l sysadmin<\/pre>\n<h2>Restricting Sudo Access by IP Address and Terminal TTY<\/h2>\n<p>For high-security production environments (such as financial or e-commerce servers), you can enforce PAM restrictions that prevent sudo privileges from being invoked unless the user is connected from a verified VPN IP address or physical local console:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># \/etc\/security\/access.conf\n# Allow sudo only from corporate VPN subnet\n+ : sysadmin : 10.8.0.0\/24 LOCAL\n- : sysadmin : ALL<\/pre>\n<h2>Setting Up Real-Time Slack or Telegram Alerts for Sudo Execution<\/h2>\n<p>To detect unauthorized privilege escalation instantly, configure a PAM session hook in <code>\/etc\/pam.d\/sudo<\/code> that dispatches a webhook notification to your team&#8217;s Slack or Telegram channel whenever any administrative command is executed:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># \/usr\/local\/bin\/sudo-alert.sh\n#!\/bin\/bash\nif [ \"$PAM_TYPE\" = \"open_session\" ]; then\n    MESSAGE=\"\u26a0\ufe0f Sudo session opened by ${PAM_USER} on $(hostname) at $(date)\"\n    curl -s -X POST -H 'Content-type: application\/json' --data \"{\"text\":\"$MESSAGE\"}\" https:\/\/hooks.slack.com\/services\/YOUR\/WEBHOOK\/URL\nfi<\/pre>\n<h2>Auditing Sudo Permissions with Automated Security Benchmarks<\/h2>\n<p>Run automated Linux security auditing tools like <strong>Lynis<\/strong> to verify that user permissions, file ownership, and sudo policies conform to CIS (Center for Internet Security) Linux benchmark standards:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">sudo apt install lynis -y\nsudo lynis audit system --quick<\/pre>\n<p>Lynis analyzes your sudoers configuration, file permissions, and PAM authentication modules, generating a hardened compliance score and actionable remediation steps.<\/p>\n<div style=\"background-color: #f8fafc;border: 1px solid #e2e8f0;border-left: 4px solid #0ea5e9;padding: 20px;border-radius: 8px;margin: 30px 0\">\n<h3 style=\"margin-top: 0;color: #0f172a;font-size: 18px;display: flex;align-items: center\">\n        <span style=\"margin-right: 8px\">\ud83d\udd17<\/span> Recommended Related Technical Guides:<br \/>\n    <\/h3>\n<ul style=\"margin: 10px 0 0 0;padding-left: 20px;line-height: 1.8\">\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-configure-ufw-firewall-ubuntu\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">How to Configure UFW Firewall on Ubuntu Server<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-install-configure-fail2ban-linux\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">How to Install and Configure Fail2ban on Linux<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-change-ssh-port-linux-vps\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">How to Change Default SSH Port on Linux VPS<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/best-free-web-hosting-2026\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">Top 10 Best Free Web Hosting Services<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/#plans\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">Explore $0 Free cPanel Web Hosting Plans<\/a><\/li>\n<\/ul>\n<\/div>\n<div style=\"background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);border: 1px solid #334155;border-radius: 12px;padding: 25px;margin: 30px 0;text-align: center\">\n<h3 style=\"color: #38bdf8;margin-top: 0;font-size: 20px\">Zero-Management Cloud Hosting on CpanelFree<\/h3>\n<p style=\"color: #94a3b8;font-size: 14px;line-height: 1.6;max-width: 600px;margin: 0 auto 15px\">\n        Don&#8217;t want to spend hours configuring Linux sudo accounts and SSH keys? <strong>CpanelFree<\/strong> provides secure, sandboxed cPanel hosting with free SSL, MySQL, and email accounts at 100% zero cost.\n    <\/p>\n<p>    <a href=\"https:\/\/cpanelfree.com\/#plans\" style=\"display: inline-block;background-color: #a855f7;color: #ffffff;padding: 10px 22px;border-radius: 6px;text-decoration: none;font-weight: bold;font-size: 14px\">Claim Free Account<\/a>\n<\/div>\n<h2>Frequently Asked Questions<\/h2>\n<div style=\"border-bottom: 1px solid #e2e8f0;padding: 12px 0\">\n<h4 style=\"margin: 0 0 8px 0;color: #1e293b\">Can I still execute root commands after disabling PermitRootLogin?<\/h4>\n<p style=\"margin: 0;color: #475569;font-size: 14px\">Yes. Simply log in with your sudo user and prefix commands with <code>sudo<\/code>, or switch to an interactive root shell with <code>sudo -i<\/code>.<\/p>\n<\/div>\n<p>Additionally, always remember to test sudo group access in a secondary terminal session before disconnecting your root session to avoid configuration lockouts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quick Answer: To disable root login on Ubuntu, create a new administrative user (adduser deployer), add the user to the sudo group (usermod -aG sudo deployer), copy authorized SSH keys to \/home\/deployer\/.ssh\/, edit \/etc\/ssh\/sshd_config to set PermitRootLogin no, and restart OpenSSH with sudo systemctl restart ssh. Why Direct Root Login is a Critical Security Risk [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1374,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-1375","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-security"],"_links":{"self":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/comments?post=1375"}],"version-history":[{"count":6,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1375\/revisions"}],"predecessor-version":[{"id":1560,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1375\/revisions\/1560"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media\/1374"}],"wp:attachment":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media?parent=1375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/categories?post=1375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/tags?post=1375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}