###控制服务###
1.什么是服务
2.用什么控制服务
系统初始化进程可以对服务进行相应的控制
3.当前系统初始化进程是什么
systemd ##系统初始化进程
pstree ##显示系统中的进程树
4.进程控制命令
ssh-------->sshd
client server
systemctl ##服务控制命令
systemctl status sshd ##查看服务状态,inacticve(不可用),active(可用)
systemctl start sshd ##开启服务
systemctl stop sshd ##关闭服务
systemctl restart sshd ##重启服务
systemctl reload sshd ##重新加载服务配置(相当于信号1)
systemctl enable sshd ##设置服务开机自动启动
systemctl disable sshd ##设置服务开机不启动
systemctl list-units ##列出系统所有已开启服务当前状态
systemctl list-unit-files ##列出所有服务开机启动的状态,static依赖disable未启动 enable启动
systemctl list-dependencies ##列出所有服务的依赖
systemctl set-default multi-user.target ##设定系统启动级别为多用户模式(无图形)输入startx 就有图形了
systemctl set-default graphical.target ##设定系统启动级别为图形模式
###1.sshd简介###
sshd= secure shell
可以通过网络在主机中开启shell的服务
客户端软件
sshd
连接方式:
ssh username@ip ##文本模式的连接
ssh -X username@ip ##可以在连接成功后开启图形
注意:
第一次连接陌生主机时要建立认证文件
所以会询问是否建立,需要输入yes
再次连接此台主机时,因为已经生成~/.ssh/know_hosts文件,所以不需要再次输入yes
远程复制:
scp file root@ip:dir ##上传
scp -r test/ [email protected]:/root/Desktop/ ##远程上传文件夹
scp root@ip:file dir ##下载
scp -r [email protected]:/root/Desktop/test/ . ##远程复制文件夹
###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:
8b:0b:a3:f2:2e:f0:84:2b:fc:d2:33:31:a5:db:75:ce [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| . |
| . o S |
|o .+ o o |
|o+. B o = |
|=o.* + . E |
|.=*.o . |
+-----------------+
2.加密服务
[root@server ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
The authenticity of host '172.25.254.215 (172.25.254.215)' 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 ~]# ls /root/.ssh/
authorized_keys id_rsa id_rsa.pub known_hosts
^
此文件出现表示加密完成
3.分发钥匙
scp /root/.ssh/id_rsa [email protected]:/root/.ssh/
[root@server ~]# scp /root/.ssh/id_rsa [email protected]:/root/.ssh/
The authenticity of host '172.25.254.115 (172.25.254.115)' 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
Warning: Permanently added '172.25.254.115' (ECDSA) to the list of known hosts.
[email protected]'s password:
id_rsa 100% 1675 1.6KB/s 00:00
4.测试
在客户主机中(172.25.254.115)
ssh [email protected] ##连接时发现直接登陆不需要root登录系统密码认证
[root@client ~]# ssh [email protected]
The authenticity of host '172.25.254.215 (172.25.254.215)' 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
Warning: Permanently added '172.25.254.215' (ECDSA) to the list of known hosts.
Last login: Mon Jul 23 04:18:32 2018 from 172.25.254.115
###3.sshd的安全设定###
vim /etc/ssh/sshd_config
systemctl restart sshd.service
78 PasswordAuthentication yes|no ##是否允许用户通过登录系统的密码做sshd的认证
48 PermitRootLogin yes|no ##是否允许root用户通过sshd服务的认证
52 AllowUsers student westos ##设定用户白名单,白名单出现默认不在名单中的用户不能使用sshd
53 DenyUsers westos ##设定用户黑名单,黑名单出现默认不在名单中的用户可以使用sshd
###4.添加sshd登陆信息###
vim /etc/motd ##文件内容就是登陆后显示的信息
###5.用户的登陆审计###
1.w ##查看正在使用当前系统的用户
-f ##查看使用来源
-i ##显示IP
/var/run/utmp
2.last ##查看使用过并退出的用户信息
/var/log/wtmp
3.lastb ##试图登陆但没成功的用户
/var/log/btmp
/var/log/messages