Back
Featured image of post One-click script to set up SSH access for ROOT account

One-click script to set up SSH access for ROOT account

One-click script to start SSH access for root account in Linux

Table of contents

TL;DR / [Geek Summary]:

  • Root Access Loophole: Bypass default SSH restrictions on Ubuntu/Debian images to reclaim full administrative control via one-click scripts.
  • Config Hardening: Uses sed for surgical modification of sshd_config and chpasswd for instantaneous root password overrides.
  • Universal Support: Tailored snippets for Ubuntu, CentOS, Arch, and Debian to automate server initialization across diverse environments.

# Preface

When we use the official Ubuntu server image to install it on the server, we can only log in to the account that was originally installed and set and use sudo su command to elevate privileges, but there is no root account that can log in through SSH. This is because after the root account is created, a random password will be set each time the computer is turned on and SSH login will be disabled. Therefore, it is not convenient for us to change the passwords one by one using nano and vim.

Windows All need to create a new .txt file and then change the suffix to .sh file and run it on Linux with ./root.sh

linux Just use nano or vim to create a root.sh and run it with ./

# ubuntu exclusive

Only for intranet ubuntu (no need for security, just convenience)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/bin/bash

# Enable root permissions
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config

# Set root password
echo "root:awa114514" | chpasswd

# Restart sshd service
systemctl restart sshd.service

echo "SSH ROOT permissions have been enabled and the password has been changed to awa114514"

# Script compatible with all Linux distributions

Written casually

Normal script compatible with all Linux distributions
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash

# Check if the root password meets security requirements
if ! check_passwd; then
echo "The root password is not secure, please reset the root password"
exit 1
fi

# Prompt the user to confirm the operation
read -p "Are you sure to enable root permissions and set the root password? (y/n) " answer
if [[ $answer != "y" ]]; then
echo "The operation has been canceled"
exit 1
fi

# Check the system type
type=$(uname | tr '[:upper:]' '[:lower:]')

# Enable root permissions
case $type in
linux)
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
;;
*)
echo "This script is only for Linux systems"
exit 1
;;
esac

# Set root password
echo "root:$password" | chpasswd

# Restart sshd service
systemctl restart sshd.service

echo "SSH ROOT permission is enabled and password has been changed to $password"

# Script compatible with CentOS, Ubuntu, Arch and Debian

Written casually

Script compatible with CentOS, Ubuntu, Arch and Debian
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash

# Check if the root password meets security requirements
if ! check_passwd; then
echo "The root password is not secure, please reset the root password"
exit 1
fi

# Prompt the user to confirm the operation
read -p "Are you sure you want to enable root permissions and set a root password? (y/n) " answer
if [[ $answer != "y" ]]; then
echo "The operation has been cancelled"
exit 1
fi

# Check the operating system
os=$(cat /etc/os-release | grep "PRETTY_NAME" | cut -d '=' -f 2 | tr -d '"')

# Enable root permissions
case $os in
"CentOS Linux")
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
;;
"Ubuntu")
sed -i 's/#PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config
;;
"Arch Linux")
sed -i 's/#PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config
;;
"Debian")
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
;;
esac

# Set root password
echo "root:awa114514" | chpasswd

# Restart sshd service
systemctl restart sshd.service

echo "SSH ROOT permission is enabled and password has been changed to awa114514"