0%

CentOS 7 直接登录 root 账户

问题

今天在亚马逊上启动一台安装 CentOS 7 的实例,然后用 SSH 远程登录 root 账户

1
ssh -i PrivateKey.pem root@xxx.xxx.xxx.xxx

提示如下错误

1
Please login as the user "centos" rather than the user "root".

解决方法

使用”centos”登录

1
ssh -i PrivateKey.pem centos@xxx.xxx.xxx.xxx

切换到”root”

1
$ sudo -s

编辑文件”/root/.ssh/authorized_keys”

1
# vi /root/.ssh/authorized_keys

只保留key(以”ssh-rsa”开头) 的部分,修改前:

1
no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"centos\" rather than the user \"root\".';echo;sleep 10" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCNpshdRh2KNPMuCCLm/RidyIoCpTf497bZW8s0ZTWqJAQ/CnabNmiFMTIkZWTXv12E0q70EwPNKhqy5lbQNKNZnKtLTuWwvU79klniZJ4uzUVbJUgwqARG4VcnDR8LEqTRf3Ewq3hauWnuDU4v326+KCNbpZUN5znSbKpbncmKarSFahTP3AaYcfnh5LRrLh8mIPzSPFGtca5MZT/QqzZ5VctVDmIl4JwGHi1pBrSsDHeNiWohfuS9T849PKWAOZYPJ4z77+JSUWd6kTFHQRI+ZENlDfu+hp5qvbOLAwHuTtezR3IdnQOLzwGQmbjQCPaSVNcg5EOZNE1QjUjNsAbp PrivateKey

修改后:

1
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCNpshdRh2KNPMuCCLm/RidyIoCpTf497bZW8s0ZTWqJAQ/CnabNmiFMTIkZWTXv12E0q70EwPNKhqy5lbQNKNZnKtLTuWwvU79klniZJ4uzUVbJUgwqARG4VcnDR8LEqTRf3Ewq3hauWnuDU4v326+KCNbpZUN5znSbKpbncmKarSFahTP3AaYcfnh5LRrLh8mIPzSPFGtca5MZT/QqzZ5VctVDmIl4JwGHi1pBrSsDHeNiWohfuS9T849PKWAOZYPJ4z77+JSUWd6kTFHQRI+ZENlDfu+hp5qvbOLAwHuTtezR3IdnQOLzwGQmbjQCPaSVNcg5EOZNE1QjUjNsAbp PrivateKey

允许 root 登录

/etc/ssh/sshd_config 文件中的 #PermitRootLogin yes改成 PermitRootLogin yes(去掉’#’号’)

1
# sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/g' /etc/ssh/sshd_config

重启 SSH 服务

1
# systemctl restart sshd

至此已可以直接登录 root 账户