Ubuntu桌面版及server版启用root用户登录并用秘钥方式登录以及更换秘钥

默认安装Ubuntu都是不允许以root用户进行登录的,想要以root用户进行登录需要进行一些操作,主要是以下几个步骤:

一、ubuntu 16.04 server启用root用户方法

首先:使用sudo passwd root命令设置root的密码

其次:修改/etc/ssh/sshd_config文件

1、vim /etc/ssh/sshd_config
2、把PermitRootLogin Prohibit-password 添加#注释掉
3、新添加:PermitRootLogin yes
4、重启ssh服务/etc/init.d/ssh restart

二、ubuntu 16.04 桌面版启用root用户方法

1
.首先设置root用户密码:
# sudo passwd root
输入普通用户密码,再输入root用户密码;
2.启用登录时的root选项:
编辑50-ubuntu.conf文件:
# sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf
添加:
greeter-show-manual-login=true
编辑/root/.profile文件:
# sudo gedit /root/.profile
找到 mesg n这一行,修改为:
tty -s && mesg n
保存退出~
3.配置root自动登陆:
编辑lightdm.conf文件:
# sudo gedit  /etc/lightdm/lightdm.conf
添加如下内容:
[SeatDefaults]
autologin-user=root
greeter-session=unity-greeter
user-session=ubuntu
greeter-show-manual-login=true
allow-guest=false
保存重启完事~

秘钥方式登录:

首先确定你可以以密码的形式连接远程服务器,也可以创建一个非超级管理员用户,并增加 sudo 权限。

$ sudo ssh root@12.34.56.78

生成 .pem 步骤如下:

1.客户端(本地主机 )生成验证没有密码密钥对

$ ssh-keygen -t rsa -b 2048 -v

执行上述命令首先会让你输入生成密钥的文件名:我这里输入的 myPemKey ,之后一路回车。

Generating public/private rsa key pair.
Enter file in which to save the key (/home/anonymouse/.ssh/id_rsa): myPemKey
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in hetzner.
Your public key has been saved in hetzner.pub.
The key fingerprint is:
bb:c6:9c:ee:6b:c0:67:58:b2:bb:4b:44:72:d3:cc:a5 localhost@localhost
The key's randomart image is:

在执行命令的当前目录下会生成一个myPemKey.pub、myPemKey 两个文件。

2. 把生成的 myPemKey.pub 通过本地命令推送到服务器端,使服务器自动添加认证这个证书

$ ssh-copy-id -i ~/myPemKey.pub root@12.34.56.78

输入你的 root 用户密码

3. 测试连接

$ sudo ssh -i ~/myPemKey root@12.34.56.78

或者把 myPemKey 重命名为 myPemKey.pem

$ sudo ssh -i ~/myPemKey.pem root@12.34.56.78

4. 禁用密码连接(注意:要保证 .pem 连接成功的状态下,禁用密码连接)

sudo vi /etc/ssh/sshd_config

找到这一行 #PasswordAuthentication yes

# Change to no to disable tunnelled clear text passwords
# PasswordAuthentication yes

取消前边的 # 注释,改为

PasswordAuthentication no

重启 ssh 服务

sudo service ssh restart

服务器端生成秘钥给客户端登录使用:

ssh-keygen
cd /root/.ssh/
cat id_rsa.pub >>authorized_keys 

vim /etc/ssh/sshd_config

添加:
RSAAuthentication yes 
PubkeyAuthentication yes
另外,请留意 root 用户能否通过 SSH 登录: 
PermitRootLogin yes
当你完成全部设置,并以密钥方式登录成功后,再禁用密码登录: 
PasswordAuthentication no
service ssh restart 

 

更换秘钥

 

#删除原来秘钥
cd  /home/biadmin/.ssh/
vim authorized_keys
#删除原来的公钥行即可


 

发表评论

1,168 次浏览