有的时候,可能需要通过远程命令ssh 来访问其他电脑或者服务器,但如果直接访问的话,可能会出现下面的情况:
$ ssh mugpeng@192.168.130.128
ssh: connect to host 192.168.130.128 port 22: Connection refused
有以下几种可能:https://www.wbolt.com/how-to-fix-ssh-connection-refused.html
ubuntu/linux
开启ssh 服务
参考:https://www.cnblogs.com/jiarenanhao/p/9938280.html
这里我们首先开启ssh 服务。
先检查一下SSH服务有没有开启。
查看一下:
sudo ps -e | grep ssh
返回结果中并没有:
需要安装ssh 服务:
# 更新ubuntu 下载源
sudo apt-get update
# 下载 openssh-server
sudo apt-get install openssh-server
ps:这里有时候可能会出现缓存锁的报错,解决可以参见:https://www.yuque.com/mugpeng/linux/uwt2mc
安装完毕后,再次查看一下:
sudo ps -e | grep ssh
mugpeng@mugpeng-virtual-machine:~$ sudo ps -e | grep ssh
1814 ? 00:00:00 ssh-agent
32866 ? 00:00:00 ssh-agent
73377 ? 00:00:00 sshd
出现sshd啦。
如果还是不放心,可以再次检查或者打开sshd:
mugpeng@mugpeng-virtual-machine:~$ sudo /etc/init.d/ssh start
Starting ssh (via systemctl): ssh.service.
显示是starting,说明服务成功运行了。
还有下面几个比较有用的命令:
sudo service ssh status 查看服务状态
sudo service ssh stop 关闭服务
sudo service ssh restart 重启服务
查看服务状态的命令也可以查看端口等其他信息:
$ sudo service ssh status | cat
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2021-02-09 12:41:23 CST; 4min 35s ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 73377 (sshd)
Tasks: 1 (limit: 1074)
Memory: 2.2M
CGroup: /system.slice/ssh.service
└─73377 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
2月 09 12:41:23 mugpeng-virtual-machine systemd[1]: Starting OpenBSD Secure Shell server...
2月 09 12:41:23 mugpeng-virtual-machine sshd[73377]: Server listening on 0.0.0.0 port 22.
2月 09 12:41:23 mugpeng-virtual-machine sshd[73377]: Server listening on :: port 22.
2月 09 12:41:23 mugpeng-virtual-machine systemd[1]: Started OpenBSD Secure Shell server.
显示22 端口(默认端口)是开启的。
这时候直接搜索一下ubuntu 虚拟机的IP:
$ ifconfig | grep inet
inet 192.168.130.128 netmask 255.255.255.0 broadcast 192.168.130.255
inet6 fe80::9f83:917c:bde1:77fa prefixlen 64 scopeid 0x20<link>
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
我们远程访问一下IP 192.168.130.128,端口为22(其实ssh默认下也就是22),用户为虚拟机用户mugpeng:
$ ssh mugpeng@192.168.130.128 -p 22
The authenticity of host '192.168.130.128 (192.168.130.128)' can't be established.
ECDSA key fingerprint is SHA256:uQKHuYrukuWxYAmR7owG6f+Xz7q2KD6a+lgxXbfLy6c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.130.128' (ECDSA) to the list of known hosts.
mugpeng@192.168.130.128's password:
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.8.0-41-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
243 updates can be installed immediately.
24 of these updates are security updates.
To see these additional updates run: apt list --upgradable
Your Hardware Enablement Stack (HWE) is supported until April 2025.
*** 需要重启系统 ***
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
# 成功进去啦
mugpeng@mugpeng-virtual-machine:~$ ls
Desktop Documents Downloads Music Pictures Public snap Templates Videos
mugpeng@mugpeng-virtual-machine:~$
如果正巧机房内某个人没有修改用户的密码,我们就可以在别人电脑的命令行内恶作剧啦:
$ sudo shutdown now
[sudo] password for mugpeng:
mugpeng@mugpeng-virtual-machine:~$ Connection to 192.168.130.128 closed by remote host.
Connection to 192.168.130.128 closed.
MAC
mac 打开很简单。
直接在设置中打开允许访问的用户权限就可以了。
$ ssh appe@192.168.1.6
The authenticity of host '192.168.1.6 (192.168.1.6)' can't be established.
ECDSA key fingerprint is SHA256:N4xtwHOpDn2sdsagOkxlG6e3/AY4Eb0U34TLtPZ6GccVQ.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.1.6' (ECDSA) to the list of known hosts.
Password:
Last login: Tue Feb 9 12:20:17 2021