默认用户名
debian:admin
ubuntu:ubuntu
Amazon Linux:ec2-user
Centos:centos
示例
chmod 400 ssh.pem
ssh -i "ssh.pem" ubuntu@IP
Mac 建议使用自带的Terminal(终端)进行远程
Windows 建议Xhell进行远程:https://www.xshell.com/zh/free-for-home-school/
ubuntu/debian 系统安装Curl 命令:
apt-get update -y && apt-get install curl -y
centos 系统安装Curl命令:
yum update -y && yum install curl -y
允许root远程
以下是一个bash脚本,可以实现在 /etc/ssh/sshd_config
文件中的 #PermitRootLogin prohibit-password
所在行之后插入 PermitRootLogin yes
和 PasswordAuthentication yes
这两个配置项:
通过脚本
wget -O root_login.sh https://img.cloudduo.cn/sh/root_login.sh && sudo bash root_login.sh
ubuntu/debian/Amazon Linux 可以通过一下指令允许root登录
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak$(date +%Y-%m-%d-%H%M%S) && sudo sed -i '/#PermitRootLogin prohibit-password/a PermitRootLogin yes\nPasswordAuthentication yes' /etc/ssh/sshd_config && sudo systemctl restart sshd
centos 可以通过一下指令允许root登录
sudo sed -i '/PasswordAuthentication/ s/^/#/g' /etc/ssh/sshd_config && sudo systemctl restart sshd
脚本代码
#!/bin/bash
# Check if user is root
if [[ $(id -u) -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Backup the original sshd_config file
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak$(date +%Y-%m-%d-%H%M%S)
# Insert PermitRootLogin and PasswordAuthentication configuration after the line containing "#PermitRootLogin prohibit-password"
sed -i '/#PermitRootLogin prohibit-password/a PermitRootLogin yes\nPasswordAuthentication yes' /etc/ssh/sshd_config
# Restart the sshd service to apply the changes
systemctl restart sshd
echo "Configuration complete. SSH now allows root login with password authentication."
将上述代码保存到文件中(比如 sshd_config_setup.sh
),并使用 chmod
命令赋予脚本执行权限(例如 chmod +x sshd_config_setup.sh
)。然后,以 root 用户身份运行该脚本即可。
在脚本中,首先检查当前用户是否为 root 用户,然后备份原始的 /etc/ssh/sshd_config
文件,并将 PermitRootLogin yes
和 PasswordAuthentication yes
配置项附加到文件的末尾。最后,重启 sshd
服务以应用更改。运行脚本后,你将会看到一条消息,告诉你 SSH 现在已经允许 root 用户使用密码进行认证。
AWS 允许root 远程登录Centos7系统
修改 /etc/ssh/sshd_config
修改前:
#PermitRootLogin yes
PasswordAuthentication no
修改后:
PermitRootLogin yes
PasswordAuthentication yes
也可以以下指令
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak$(date +%Y-%m-%d-%H%M%S) && sudo sed -i '/PasswordAuthentication/ s/^/#/g' /etc/ssh/sshd_config && sudo sed -i '/#PermitRootLogin prohibit-password/a PermitRootLogin yes\nPasswordAuthentication yes' /etc/ssh/sshd_config && sudo systemctl restart sshd
设置root密码为12位随机数
一个示例脚本 root_login.sh 可以生成随机密码并将其打印出来:
#!/bin/bash
# Generate a random password
PASSWORD=$(date +%s | sha256sum | base64 | head -c 12 ; echo)
# Set the root password to the random password
sudo passwd root << EOF
$PASSWORD
$PASSWORD
EOF
# Print the password
echo "Randomly generated root password: $PASSWORD"
可以直接复制这个
PASSWORD=$(date +%s | sha256sum | base64 | head -c 12 ; echo)
echo "Randomly generated root password: $PASSWORD"
sudo passwd root << EOF
$PASSWORD
$PASSWORD
EOF
这个脚本使用了 date
命令生成一个随机的 seed,并使用 sha256sum
和 base64
算法将其转换为一个随机的 12 位密码。然后使用 sudo passwd root
命令将 root 用户的密码设置为这个随机密码。最后,脚本打印出这个随机密码。
使用FinalShell、nuoshell等部分SSH客户端登录失败
参考:https://help.aliyun.com/document_detail/460037.html
使用FinalShell、nuoshell等部分SSH客户端软件通过RSA密钥远程连接ECS实例时,SSH客户端提示登录失败,sshd服务日志提示如下:
userauth_pubkey: key type ssh-rsa not in PubkeyAcceptedAlgorithms [preauth]
仅Ubuntu 22.04无法使用RSA密钥登录解决方案
在最新版本的操作系统中,OpenSSH默认不再支持ssh-rsa(rsa/SHA1)签名算法,如果指定使用ssh-rsa(rsa/SHA1)签名算法将会登录失败。由于FinalShell、nuoshell等部分SSH客户端软件默认仅支持使用ssh-rsa(rsa/SHA1)签名算法,不能兼容使用rsa-sha2-256(rsa/SHA256)或者rsa-sha2-512(rsa/SHA512),所以无法登录。
解决方案
您可以通过以下任一种方案解决该问题:
方案一:使用ECDSA或者DSA等其他加密方式。
方案二:使用其他SSH客户端进行登录。
方案三:运行以下命令,在sshd配置中允许使用ssh-rsa(rsa/SHA1)的签名算法。
重要由于ssh-rsa(rsa/SHA1)签名算法并不安全,请您谨慎操作。
echo "echo 'PubkeyAcceptedAlgorithms=+ssh-rsa' >> /etc/ssh/sshd_config" | sudo sh
sudo systemctl restart sshd