linux——sshd服务及其管理命令

一、.openssh

当主机中开启openssh服务,那么就对外开放了远程连接的接口,即让远程主机可以通过网络访问sshd服务,开始一个安全shell

#openssh服务的服务端

sshd

#openssh服务的客户端

ssh

二、客户端连接sshd的方式

ssh   服务端用户@服务端ip地址


ssh   [email protected] -X ##在客户端用ssh命令连接172.25.254.250主机的root用户


**注意:以上连接方式是可以打开远程主机的图形功能的,如果不需要打开远程主机图形功能则不需要输入 -X

ssh [email protected]

三、给ssh服务添加新的认证方式 KEY认证

1.生成锁和钥匙(在服务端)

注:

[[email protected] Desktop]# ssh-keygen ##生成密钥的命令

Enter file in which to save the key (/root/.ssh/id_rsa): #保存加密字符的文件用默认

Enter passphrase (empty for no passphrase): #可以为空,如果想为空必须大于4位

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa. ##私钥

Your public key has been saved in /root/.ssh/id_rsa.pub. ##公钥

2.加密ssh用户的认证

#在服务端

ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

ssh-copy-id ##加密命令

-i ##指定密钥

/root/.ssh/id_rsa.pub ##密钥

root ##加密用户

172.25.254.250 ##被加密主机ip


3.验证

#解密文件传输到客户端

scp /root/.ssh/id_rsa  [email protected]:/root/.ssh/

#在客户端

ssh [email protected] #连接不需要密码

#在服务端

rm -fr /root/.ssh/authorized_keys ##当此文件被删除,客户端解密文件失效

即 ssh [email protected] #连接需要密码

#在服务端

cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys ##从新生成锁文件,解密文件功能恢复

ssh [email protected] #连接不需要密码


4.sshd的安全配置

vim /etc/ssh/sshd_config

禁止原始认证方式

78 PasswordAuthentication no|yes ##开启或关闭ssh的默认认证方式


48 PermitRootLogin no|yes ##开启或关闭root用户的登陆权限


79 AllowUsers westos ##用户白名单,当前设定是只允许westos登陆


80 DenyUsers linux ##用户黑名单,当前设定是只不允许linux登陆

注:黑名单和白名单不能同时存在

5.系统初始化程序

1.systemd

系统初始化程序,系统开始的第一个进程,pid为1

2.systemctl 命令

systemctl list-units ##列出当前系统服务的状态

systemctl list-units-files ##列出服务的开机状态

systemctl status sshd ##查看指定服务的状态

systemctl stop sshd ##关闭指定服务

systemctl start sshd ##开启指定服务

systemctl restart sshd ##重新启动服务

systemctl enable sshd ##设定指定服务开机开启

systemctl disable sshd ##设定指定服务开机关闭

systemctl reload sshd ##使指定服务重新加载配置

systemctl list-dependencies sshd ##查看指定服务的倚赖关系

systemctl mask sshd ##冻结指定服务

systemctl unmask sshd ##启用服务

systemctl set-default multi-user.target ##开机不开启图形

systemctl set-default graphical.target ##开机启动图形

setterm ##文本界面设定color

vga = ask


3.服务状态

systemctl status 服务名称

loaded ##系统初始化已经完成

active(running)

##正有一个或多个程序正在系统中执行

##vsftpd 就是这种模式

active(exited) ##仅执行一次就正常结束的服务

active(waiting)

##正在执行当中

##不过还在等待其他的事件才能继续处理

inactive ##服务关闭

enabled ##服务开机启动

disabled ##服务开机不启动

static ##服务开机启动项不可被管理

failed ##系统配置错误