Linux之sshd服务

一、linux中对服务管理与控制(以sshd为例)

1.什么是服务

  可以用来给客户提供相关操作,对自己没有什么好处

2.用什么控制服务
  系统初始化进程可以对服务进行相应的控制

3.当前系统初始化进程是什么
   systemd    ##系统初始化进程
   pstree     ##显示系统中的进程树

4.进程控制命令
  ssh -------->sshd(ssh用为客户端,主要进行服务器端的连接;sshd用为服务器端)

  systemctl              ##服务控制命令
  systemctl start sshd   ##开启服务
  systemctl stop sshd    ##关闭服务
  systemctl restart sshd ##重启服务
  systemctl reload sshd  ##重新加载服务配置
  systemctl enable sshd  ##设定服务开机启动
  systemctl disable sshd ##设定服务开机不启动
  systemctl list-units   ##列出已经开启服务当前状态
  systemctl list-dependencies ##列出服务的倚赖
  systemctl set-default multi-user.target ##设定系统启动级别为多用户模式(无图形)
  systemctl set-default graphical.target  ##设定系统启动级别为图形模式

  systemctl status sshd  ##查看服务状态,inactive(不可用),active(可用)

  注意:也可以使用init 5使界面图形化

 
  systemctl list-unit-files   ##列出所有服务开机启动的状态 (disable,enable,static)  
 

  若想对外开启服务,可以使用systemctl start ssh来开启服务接口。

  对于服务来说,是不可以关闭的;若修改了配置文件,只能通过systemctl restart  sshd进行重新启动。

二、sshd服务

1.sshd介绍
sshd= secure shell
可以通过网络在主机中开机shell的服务

客户端软件<C-F9>
sshd

连接方式:
ssh username@ip ##文本模式的链接
ssh -X username@ip ##可以在链接成功后开机图形

注意:
第一次链接陌生主机是要建立认证文件
所以会询问是否建立,需要树入yes
在次链接此台主机时,因为已经生成~/.ssh/know_hosts文件所以不需要再次输入yes

远程复制:
scp file root@ip:dir      ##上传
scp root@ip:file dir     ##下载


###2.sshd 的key认证 #######

1.生成认证KEY
[root@server ~]# ssh-keygen ##生成密钥的命令
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): ##指定保存加密字符的文件(使用默认)
Enter passphrase (empty for no passphrase): ##设定密码(使用空密码)
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". ##公钥(锁)
The key fingerprint is:
86:61:e4:f1:6e:51:3a:4b:d7:3c:1b:2f:e8:3f:b0:5d [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| o . |
| o o o o |
| + * . = |
| . = = . = |
| . S . o . |
| o .. .E |
| .+ . |
| ..o |
| .. |
+-----------------+

2.加密服务
[root@server .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected] ##加密sshd服务
The authenticity of host '172.25.254.200 (172.25.254.200)' can't be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

[root@server .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
^
此文件出现表示加密完成

3.分发钥匙
scp /root/.ssh/id_rsa [email protected]:/root/.ssh/

4.测试
在客户主机中(172.25.254.100)
ssh [email protected] ##连接时发现直接登陆不需要root登陆系统的密码认证

####3.sshd的安全设定####
78 PasswordAuthentication yes|no ##是否允许用户通过登陆系统的密码做sshd的认证(也可登录其他用户密码)
48 PermitRootLogin yes|no ##是否允许root用户通过sshd服务的认证
52 Allowusers student westos ##设定用户白名单,白名单出现默认不再名单中的用户不能使用sshd
53 Denyusers westos ##设定用户黑名单,黑名单出现默认不再名单中的用户可以使用sshd

使用户不能连接到服务器:vim /etc/ssh/sshd_config(服务端配置)
配置完成之后要重启服务:systemctl restart sshd.service

猜你喜欢

转载自www.cnblogs.com/uthnb/p/9367875.html