ssh使用root远程登录问题
在刚装完Ubuntu以后,会下载ssh服务,但是在下载ssh服务后,无法使用root用户登录。导致在使用VisCode远程登录时没有办法修改带root权限的文件。
本次主要介绍一下ssh服务简介,下载ssh服务,配置ssh服务,以及配置完成后还是无法使用root权限登录的问题。
一、ssh服务简介
SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定;SSH 为建立在应用层基础上的安全协议。SSH 是较可靠,专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题。SSH最初是UNIX系统上的一个程序,后来又迅速扩展到其他操作平台。SSH在正确使用时可弥补网络中的漏洞。SSH客户端适用于多种平台。几乎所有UNIX平台—包括HP-UX、Linux、AIX、Solaris、Digital UNIX、Irix,以及其他平台,都可运行SSH
传统的网络服务程序,如:ftp、pop和telnet在本质上都是不安全的,因为它们在网络上用明文传送口令和数据,别有用心的人非常容易就可以截获这些口令和数据。而且,这些服务程序的安全验证方式也是有其弱点的, 就是很容易受到“中间人”(man-in-the-middle)这种方式的攻击。所谓“中间人”的攻击方式, 就是“中间人”冒充真正的服务器接收你传给服务器的数据,然后再冒充你把数据传给真正的服务器。服务器和你之间的数据传送被“中间人”一转手做了手脚之后,就会出现很严重的问题。通过使用SSH,你可以把所有传输的数据进行加密,这样"中间人"这种攻击方式就不可能实现了,而且也能够防止DNS欺骗和IP欺骗。使用SSH,还有一个额外的好处就是传输的数据是经过压缩的,所以可以加快传输的速度。SSH有很多功能,它既可以代替Telnet,又可以为FTP、PoP、甚至为PPP提供一个安全的"通道" 。
二、下载ssh服务
下载ssh服务很简单,我这边就是简单的说一下,毕竟下载ssh服务的方法在网上比比皆是了。
sudo apt-get update //更新安装环境
sudo apt-get install openssh openssh-server //安装ssh服务
sudo /etc/init.d/ssh restart //重启ssh服务
ps -e | grep ssh //查看服务是否启动
测试结果:
:ssh wht@192.168.0.3
wht@192.168.0.3's password:
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.15.0-29-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
492 个可升级软件包。
416 个安全更新。
wht@wht-virtual-machine:~$
但是无法使用root用户登录:
ssh root@192.168.0.3
root@192.168.0.3's password:
root@192.168.0.3's password:
root@192.168.0.3's password:
Permission denied (publickey,password).
三、配置ssh服务
配置ssh服务也是很简单,只需要打开/etc/ssh/sshd_config文件,修改一下PermitRootLogin配置就好。
一般这个配置都会有,如果没有就添加进去。
文件的一部分内容:
# Authentication:
LoginGraceTime 120
#PermitRootLogin prohibit-password
...
...
...
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
PermitRootLogin yes
修改完成以后重启ssh服务,进行测试:
ssh root@192.168.0.3
root@192.168.0.3's password:
root@192.168.0.3's password:
root@192.168.0.3's password:
Permission denied (publickey,password).
经过打印信息不难看出,并没有实际效果,还是无法登录的状态。那接下来就要查找原因了。
四、ssh root用户无法登录问题排查
1、查看报错信息
在修改完sshd_config文件后非常容易出现这一类报错 Failed to start OpenBSD Secure Shell server.
root@wht-virtual-machine:~# /etc/init.d/ssh start
[....] Starting ssh (via systemctl): ssh.serviceJob for ssh.service failed because the control process exited with error code. See "systemctl status ssh.service" and "journalctl -xe" for details.
failed!
root@wht-virtual-machine:~# systemctl status ssh.service
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: inactive (dead) (Result: exit-code) since 日 2022-09-18 22:57:27 CST; 18s ago
Process: 2646 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=255)
9月 18 22:57:27 wht-virtual-machine systemd[1]: Failed to start OpenBSD Secure Shell server.
9月 18 22:57:27 wht-virtual-machine systemd[1]: ssh.service: Unit entered failed state.
9月 18 22:57:27 wht-virtual-machine systemd[1]: ssh.service: Failed with result 'exit-code'.
9月 18 22:57:27 wht-virtual-machine systemd[1]: ssh.service: Service hold-off time over, scheduling restart.
9月 18 22:57:27 wht-virtual-machine systemd[1]: Stopped OpenBSD Secure Shell server.
9月 18 22:57:27 wht-virtual-machine systemd[1]: ssh.service: Start request repeated too quickly.
9月 18 22:57:27 wht-virtual-machine systemd[1]: Failed to start OpenBSD Secure Shell server.
9月 18 22:57:31 wht-virtual-machine systemd[1]: ssh.service: Start request repeated too quickly.
9月 18 22:57:31 wht-virtual-machine systemd[1]: Failed to start OpenBSD Secure Shell server.
此类报错是因为在上一步修改sshd_config时,误改了其他配置,如我是把UsePAM yes 改为了UsePAM yesa 了,修改回去即可。主要是仔细看看文件中时配置选项是否发生了改变,耐心一点就会找到。
2、sshd是否已经启动
通过ps命令查询到sshd进程:
root@localhost:~# ps -e|grep ssh
4533 ? 00:00:00 sshd
4976 ? 00:00:00 sshd
如果进程能够被查询到,证明进程已经启动;如果没有,则需要重启ssh服务:
sudo /etc/init.d/ssh start
Starting ssh (via systemctl): ssh.service.
sudo /etc/init.d/ssh restart
Restarting ssh (via systemctl): ssh.service.
3、检查防火墙问题
forlinx@ubuntu:~$ sudo ufw status
状态:不活动
如果是active,不用关心怎么配置让ssh通过,先关掉防火墙试试(如果确认是防火墙,在专门找防火墙的问题)
sudo ufw disable
Firewall stopped and disabled on system startup
4、设置root用户默认密码
默认root密码是随机的,即每次开机都有一个新的root密码。
可以在终端输入命令 sudo passwd,然后输入当前用户的密码,enter。
终端会提示输入新的密码并确认,此时的密码就是root新密码。修改成功后,输入命令 su root,再输入新的密码就ok了。
root@localhost:~# sudo passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
5.查看主机的路由表
查看电脑的路由表,看一下路由表的IP和MAC的对应关系,看看是否和板卡中的对应关系相一致。
电脑端地址解析协议:
C:\Users\Administrator>arp -a
172.16.0.245 7a-35-1e-01-97-39 dynamic
板卡中的IP与MAC对应关系:
root@localhost:~# ifconfig
fm1-mac3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.0.245 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::7835:1eff:fe01:9739 prefixlen 64 scopeid 0x20<link>
ether 7a:35:1e:01:97:39 txqueuelen 1000 (Ethernet)
RX packets 73027 bytes 7305322 (7.3 MB)
RX errors 0 dropped 545 overruns 0 frame 0
TX packets 220 bytes 21624 (21.6 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device memory 0x1ae4000-1ae4fff
如果不对应会报这个错:
ssh: connect to host XX.XX.XX.XX port 22: Connection refused