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服务并配置开机自启

systemctl enable ssh #开机启动 
systemctl restart ssh #重启ssh 
systemctl status ssh #查看ssh状态

3、配置防火墙

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

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

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

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

4、尝试使用SSH登录

查询Server IP地址

  • ifconfig
    • 若提示未安装需要“apt-get install -y net-tools”安装
root@alone:~# ifconfig
ens160: flags=4163  mtu 1500
        inet 192.168.0.70  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::54ca:4141:8163:edcc  prefixlen 64  scopeid 0x20
        ether 00:50:56:8c:ba:ab  txqueuelen 1000  (以太网)
        RX packets 5141  bytes 2567997 (2.5 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1318  bytes 154200 (154.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (本地环回)
        RX packets 196  bytes 18395 (18.3 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 196  bytes 18395 (18.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • ip add
root@alone:~# ip add
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens160:  mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:50:56:8c:ba:ab brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 192.168.0.70/24 brd 192.168.0.255 scope global dynamic noprefixroute ens160
       valid_lft 1306sec preferred_lft 1306sec
    inet6 fe80::54ca:4141:8163:edcc/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

        4.1 ROOT登录时出现权限问题

PS C:\Users\Alone> ssh [email protected]
[email protected]'s password:
Permission denied, please try again.

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

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

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

                4.1.2 保存退出,重启ssh服务

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

                4.1.3 查看ssh服务状态

root@alone:~# systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-10-11 14:05:26 CST; 9min ago
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 3686 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 3687 (sshd)
      Tasks: 1 (limit: 19119)
     Memory: 4.0M
        CPU: 130ms
     CGroup: /system.slice/ssh.service
             └─3687 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

10月 11 14:05:26 alone systemd[1]: Starting OpenBSD Secure Shell server...
10月 11 14:05:26 alone sshd[3687]: Server listening on 0.0.0.0 port 22.
10月 11 14:05:26 alone sshd[3687]: Server listening on :: port 22.
10月 11 14:05:26 alone systemd[1]: Started OpenBSD Secure Shell server.
10月 11 14:05:48 alone sshd[3689]: Accepted password for root from 192.168.2.124 port 53758 ssh2
10月 11 14:05:48 alone sshd[3689]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)

                4.1.4 查看ssh详细进程信息

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

5 重新登录SSH即可

PS C:\Users\Alone> ssh [email protected]
[email protected]'s password:
Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-50-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0 更新可以立即应用。

Last login: Tue Oct 11 14:03:02 2022 from 192.168.2.124
root@alone:~#

你可能感兴趣的:(Ubuntu,ubuntu,ssh,linux)