学习目标
- 控制服务
- sshd服务
ps:博文中粉色shell为物理机,桔黄色为虚拟机client desktop,以方便区分。
##控制服务##
1.什么是服务
2.用什么控制服务
系统初始化进程可以对服务进行相应的控制
3.当前系统初始化进程是什么
systemd ##系统初始化进程
pstree ##显示系统中的进程树
4.进程控制命令
ssh -->sshd
client server
systemctl ##服务控制命令
systemctl status sshd ##查看服务状态,inactive(不可用),active(可用)
systemctl start sshd ##开启服务
systemctl stop sshd ##关闭服务
systemctl restart sshd ##重启服务
systemctl reload sshd ##重新加在配置服务
systemctl enable sshd ##设定服务开机启动
systemctl disable sshd ##设定开机服务不启动
systemctl list-units ##列出已经开启服务的当前状态
systemctl list-unit-files ##列出所有服务开机启动状态
systemctl list-dependencies ##列出服务依赖
systemctl set-default multi-user.target ##设定系统启动级别为多用户模式(无图形)
systemctl set-default graphical.target ##设定系统启动级别为图形模式
#init 5/startx退出无图形
< sshd stop后物理机无法连接虚拟机、重新start后又可以连接>
-status-
-restart-
<改主机名称>关闭终端重新进入即更改完成。
##sshd服务##
#1.sshd简介##
sshd = secure shell
可以通过网络在主机中开启shell的服务
SSH 为 Secure Shell 的缩写 , 是应用层的安全协议。 SSH
是目前较可靠 , 专为远程登录会话和其他网络服务提供安全性的
协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露
问题
客户端软件
ssh
连接方式:
ssh username@ip ##文本模式的连接
ssh -X username@ip ##可以在连接成功后打开图形
注意:
第一次连接陌生主机时要建立认证文件
会询问是否建立,需要输入yes
再次链接此主机时,因为已经生成 ~/.ssh/know_hosts
显示ip的几种方法:
配置虚拟机ip流程(以server为例):ip设置为172.25.254.203
从desktop端登陆server端:
【shell中光标移动ctrl a(段尾) e(段首)】
远程复制:
scp file root@ip(别人id):dir ##上传给别人文件(dir为绝对路径)
scp root@ip:file dir ##下载文件(file为绝对路径)
scp -rp dir root@ip(别人id):dir ##上传给别人目录(dir为绝对路径)
scp -rp root@ip:dir dir ##下载目录(file为绝对路径)
##2.sshd的key认证##
[root@server .ssh]# 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:
14:9f:ee:48:44:28:05:46:a4:f5:46:8a:44:62:f3:95 [email protected]
The key’s randomart image is:
±-[ RSA 2048]----+
|.+oo*++.o |
|.oo=oE… o . |
| o…o o o |
| . o . |
| S . |
| . o |
| . . |
| |
| |
±----------------+
2.加密服务
[root@server .ssh]# ssh-copy-id -i /root/.ssd/id_rsa.pub [email protected]
##加密root用户
The authenticity of host ‘172.25.254.100 (172.25.254.100)’ can’t be established.
ECDSA key fingerprint is 65:4d:ac:8a:c9:58:82:b5:0c:91:c4:ef:a5:e6:f6:65.
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:
\Permission denied, please try again.
[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.200)
ssh [email protected] ##连接时发现直接登陆不需要输入root密码
##3.sshd安全设定##
ssh 服务的配置文件为 /etc/sysconfig/sshd_config
PasswordAuthtication yes ##是否允许用户通过密码作sshd认证
PermitRootLogin yes ##是否允许root用户通过sshd服务的认证
AllowUsers student westos #设定用户白名单,白名单出现,默认不在白名单中的用户不能使用sshd
DenyUsers westos ##设定用户黑名单,黑名单出现,默认不在黑名单中的用户可以使用sshd
##4.添加sshd登陆信息##
vim /etc/motd ##文件内容就是登陆后的信息显示
补充:##用户的登陆审计##
1.w ##查看正在使用当前系统的用户
-f ##查看使用来源(主机名)
-i ##显示ip
2.last ##查看使用过并退出的用户信息
3.lastb ##试图登陆但没成功的用户