CentOS7 安装好以后,sshd默认是启动的。可以通过下令来检察:
[root@centos7 ~]# systemctl status sshd● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since 五 2022-12-02 16:13:02 CST; 1h 6min ago Docs: man:sshd(8) man:sshd_config(5) Process: 8920 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS) Main PID: 8767 (sshd) Tasks: 1 CGroup: /system.slice/sshd.service └─8767 /usr/sbin/sshd -D只要看到active(running),就阐明sshd服务是正常运行的。
在客户端直接利用下令就可以登录服务器了:
ssh test@192.168.0.10 为了安全起见,在服务器上克制root用户登录:
vim /etc/ssh/sshd_configPermitRootLogin no这种方式是常见的,只要账户的口令精确,就可以登录服务器。
1.2 通过证书登录服务器
首老师成证书,包罗公钥证书和私钥证书:
ssh-keygen -t rsa Generating public/private rsa key pair.Enter file in which to save the key (/Users/liyz/.ssh/id_rsa): Enter passphrase (empty for no passphrase): 111222(口令不可见)Enter same passphrase again: 111222(口令不可见)Your identification has been saved in /Users/liyz/.ssh/id_rsa1Your public key has been saved in /Users/liyz/.ssh/id_rsa1.pubThe key fingerprint is:SHA256:YiZvjxUCjKVbg6glfjm3ZL3z1ILY26I1WUutbdrYkB4 liyz@192.168.0.101The key's randomart image is:+---[RSA 3072]----+| . || . * ||o = = ||oo o.o. . ||...+.+=.So . || . ==+o*.* || ooO.E + || ..=O X || .o.o* o |+----[SHA256]-----+在/Users/liyz/.ssh目次中会产生两个文件,一个是id_rsa,这个是私钥证书,必要生存在客户机中;另一个是id_rsa.pub,这个是公钥证书,必要上传到服务器中。
1.2.1 服务器设置修改
修改服务器sshd_config文件:
克制通过暗码登录,只允许证书登录;
修改公钥证书文件;
vim /etc/ssh/sshd_config PasswordAuthentication no //原来是yesAuthorizedKeysFile /root/.ssh/id_rsa.pub //原来是.ssh/authorized_keys
重启sshd服务;
systemctl restart ssd 1.2.2 客户端登录
查抄客户端应该有私钥证书:.ssh/id_rsa
利用下令ssh下令登录服务器:
# ssh root@192.168.0.10The authenticity of host '192.168.0.10 (192.168.0.10)' can't be established.ED25519 key fingerprint is SHA256:k/5TJYuu4PNlrljOW/asglLyr8jV9zHSwvBdfHoV3EQ.This key is not known by any other namesAre you sure you want to continue connecting (yes/no/[fingerprint])? yesWarning: Permanently added '192.168.0.10' (ED25519) to the list of known hosts.Enter passphrase for key '/Users/liyz/.ssh/id_rsa': Last login: Fri Dec 2 17:38:25 2022 from 192.168.0.101[root@centos7 ~]# 特殊留意: