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 ./
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 permissionssed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
# Set root passwordecho"root:awa114514"| chpasswd
# Restart sshd servicesystemctl restart sshd.service
echo"SSH ROOT permissions have been enabled and the password has been changed to awa114514"
#!/bin/bash
# Check if the root password meets security requirementsif ! check_passwd;thenecho"The root password is not secure, please reset the root password"exit1fi# Prompt the user to confirm the operationread -p "Are you sure to enable root permissions and set the root password? (y/n) " answer
if[[$answer !="y"]];thenecho"The operation has been canceled"exit1fi# Check the system typetype=$(uname | tr '[:upper:]''[:lower:]')# Enable root permissionscase$type in
linux)sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
;;*)echo"This script is only for Linux systems"exit1;;esac# Set root passwordecho"root:$password"| chpasswd
# Restart sshd servicesystemctl 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
#!/bin/bash
# Check if the root password meets security requirementsif ! check_passwd;thenecho"The root password is not secure, please reset the root password"exit1fi# Prompt the user to confirm the operationread -p "Are you sure you want to enable root permissions and set a root password? (y/n) " answer
if[[$answer !="y"]];thenecho"The operation has been cancelled"exit1fi# Check the operating systemos=$(cat /etc/os-release | grep "PRETTY_NAME"| cut -d '=' -f 2| tr -d '"')# Enable root permissionscase$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 passwordecho"root:awa114514"| chpasswd
# Restart sshd servicesystemctl restart sshd.service
echo"SSH ROOT permission is enabled and password has been changed to awa114514"