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
.txtfile and then change the suffix to.shfile and run it on Linux with./root.shlinux Just use
nanoorvimto create aroot.shand run it with./
ubuntu exclusive
Only for intranet ubuntu (no need for security, just convenience)
```Bash #!/bin/bashEnable 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 CentOS, Ubuntu, Arch and Debian
Written casually
Script compatible with CentOS, Ubuntu, Arch and Debian
```Bash #!/bin/bashCheck 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”
| |
