前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >13. Linux 介绍与工具使用(九:开启电脑ssh服务)

13. Linux 介绍与工具使用(九:开启电脑ssh服务)

作者头像
北野茶缸子
发布2021-12-17 09:31:07
7710
发布2021-12-17 09:31:07
举报
文章被收录于专栏:北野茶缸子的专栏

有的时候,可能需要通过远程命令ssh 来访问其他电脑或者服务器,但如果直接访问的话,可能会出现下面的情况:

代码语言:javascript
复制
$ 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服务有没有开启。

查看一下:

代码语言:javascript
复制
sudo ps -e | grep ssh

返回结果中并没有:

需要安装ssh 服务:

代码语言:javascript
复制
# 更新ubuntu 下载源
sudo apt-get update

# 下载 openssh-server
sudo apt-get  install openssh-server

ps:这里有时候可能会出现缓存锁的报错,解决可以参见:https://www.yuque.com/mugpeng/linux/uwt2mc

安装完毕后,再次查看一下:

代码语言:javascript
复制
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:

代码语言:javascript
复制
mugpeng@mugpeng-virtual-machine:~$ sudo /etc/init.d/ssh start
Starting ssh (via systemctl): ssh.service.

显示是starting,说明服务成功运行了。

还有下面几个比较有用的命令:

代码语言:javascript
复制
sudo service ssh status 查看服务状态
sudo service ssh stop  关闭服务
sudo service ssh restart  重启服务

查看服务状态的命令也可以查看端口等其他信息:

代码语言:javascript
复制
$ 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:

代码语言:javascript
复制
$ 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:

代码语言:javascript
复制
$ 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:~$ 

如果正巧机房内某个人没有修改用户的密码,我们就可以在别人电脑的命令行内恶作剧啦:

代码语言:javascript
复制
$ 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 打开很简单。

直接在设置中打开允许访问的用户权限就可以了。

代码语言:javascript
复制
$ 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
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-10-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 北野茶缸子 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ubuntu/linux
    • 开启ssh 服务
    • MAC
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档