当前位置:   article > 正文

Ubuntu安装SSH服务

ubuntu安装ssh

目录

0、更新软件源

1、安装openssl:

2、启动ssh服务并配置开机自启

3、配置防火墙

        3.1 配置防火墙ssh通过及22端口

4、尝试使用SSH登录

        4.1 ROOT登录时出现权限问题

                4.1.1 修改ssh配置文件,设置允许root登录

                4.1.2 保存退出,重启ssh服务

                4.1.3 查看ssh服务状态

                4.1.4 查看ssh详细进程信息

5 重新登录SSH即可

注:安装前需要先将“源”配置好(以下演示为root账号)

0、更新软件源

apt-get install -y update

1、安装openssl

apt-get install -y update apt-get install -y openssh-server

2、启动ssh服务并配置开机自启

  1. systemctl enable ssh #开机启动
  2. systemctl restart ssh #重启ssh
  3. systemctl status ssh #查看ssh状态

3、配置防火墙

注:也可以直接将防火墙关掉并禁止自启(不推荐此方式)

  1. apt-get install -y firewalld #安装firewalld防火墙
  2. systemctl stop firewalld #关闭防火墙
  3. systemctl disabled firewalld #禁止防火墙自启

        3.1 配置防火墙ssh通过及22端口

  1. firewall-cmd --permanent --add-port=22/tcp #允许22端口通过
  2. firewall-cmd --permanent --add-service=ssh #允许ssh服务通过
  3. systemctl reload firewalld #刷新防火墙规则
  4. firewall-cmd --list-all #查看防火墙所有规则

4、尝试使用SSH登录

查询Server IP地址

  • ifconfig
    • 若提示未安装需要“apt-get install -y net-tools”安装
  1. root@alone:~# ifconfig
  2. ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  3. inet 192.168.0.70 netmask 255.255.255.0 broadcast 192.168.0.255
  4. inet6 fe80::54ca:4141:8163:edcc prefixlen 64 scopeid 0x20<link>
  5. ether 00:50:56:8c:ba:ab txqueuelen 1000 (以太网)
  6. RX packets 5141 bytes 2567997 (2.5 MB)
  7. RX errors 0 dropped 0 overruns 0 frame 0
  8. TX packets 1318 bytes 154200 (154.2 KB)
  9. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  10. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  11. inet 127.0.0.1 netmask 255.0.0.0
  12. inet6 ::1 prefixlen 128 scopeid 0x10<host>
  13. loop txqueuelen 1000 (本地环回)
  14. RX packets 196 bytes 18395 (18.3 KB)
  15. RX errors 0 dropped 0 overruns 0 frame 0
  16. TX packets 196 bytes 18395 (18.3 KB)
  17. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  • ip add
  1. root@alone:~# ip add
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. inet 127.0.0.1/8 scope host lo
  5. valid_lft forever preferred_lft forever
  6. inet6 ::1/128 scope host
  7. valid_lft forever preferred_lft forever
  8. 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
  9. link/ether 00:50:56:8c:ba:ab brd ff:ff:ff:ff:ff:ff
  10. altname enp3s0
  11. inet 192.168.0.70/24 brd 192.168.0.255 scope global dynamic noprefixroute ens160
  12. valid_lft 1306sec preferred_lft 1306sec
  13. inet6 fe80::54ca:4141:8163:edcc/64 scope link noprefixroute
  14. valid_lft forever preferred_lft forever

        4.1 ROOT登录时出现权限问题

  1. PS C:\Users\Alone> ssh root@192.168.0.70
  2. root@192.168.0.70's password:
  3. Permission denied, please try again.

                4.1.1 修改ssh配置文件,设置允许root登录

注:找到“PermitRootLogin”修改或新增一行“PermitRootLogin yes”

  1. root@alone:~# vim /etc/ssh/sshd_config
  2. #LoginGraceTime 2m
  3. #PermitRootLogin prohibit-password
  4. PermitRootLogin yes
  5. #StrictModes yes
  6. #MaxAuthTries 6
  7. #MaxSessions 10

                4.1.2 保存退出,重启ssh服务

  1. 方式一:
  2. root@alone:~# /etc/init.d/ssh restart
  3. Restarting ssh (via systemctl): ssh.service.
  4. 方式二:
  5. root@alone:~# systemctl restart ssh

                4.1.3 查看ssh服务状态

  1. root@alone:~# systemctl status ssh
  2. ● ssh.service - OpenBSD Secure Shell server
  3. Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
  4. Active: active (running) since Tue 2022-10-11 14:05:26 CST; 9min ago
  5. Docs: man:sshd(8)
  6. man:sshd_config(5)
  7. Process: 3686 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
  8. Main PID: 3687 (sshd)
  9. Tasks: 1 (limit: 19119)
  10. Memory: 4.0M
  11. CPU: 130ms
  12. CGroup: /system.slice/ssh.service
  13. └─3687 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
  14. 1011 14:05:26 alone systemd[1]: Starting OpenBSD Secure Shell server...
  15. 1011 14:05:26 alone sshd[3687]: Server listening on 0.0.0.0 port 22.
  16. 1011 14:05:26 alone sshd[3687]: Server listening on :: port 22.
  17. 1011 14:05:26 alone systemd[1]: Started OpenBSD Secure Shell server.
  18. 1011 14:05:48 alone sshd[3689]: Accepted password for root from 192.168.2.124 port 53758 ssh2
  19. 1011 14:05:48 alone sshd[3689]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)

                4.1.4 查看ssh详细进程信息

  1. root@alone:~# ps -ef | grep ssh
  2. root 3687 1 0 14:05 ? 00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
  3. root 3689 3687 0 14:05 ? 00:00:00 sshd: root@pts/1
  4. root 3764 3725 0 14:16 pts/1 00:00:00 grep --color=auto ssh

5 重新登录SSH即可

  1. PS C:\Users\Alone> ssh root@192.168.0.70
  2. root@192.168.0.70's password:
  3. Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-50-generic x86_64)
  4. * Documentation: https://help.ubuntu.com
  5. * Management: https://landscape.canonical.com
  6. * Support: https://ubuntu.com/advantage
  7. 0 更新可以立即应用。
  8. Last login: Tue Oct 11 14:03:02 2022 from 192.168.2.124
  9. root@alone:~#

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/185191?site
推荐阅读
相关标签
  

闽ICP备14008679号