Linux-基础学习(6)

一、系统服务的控制

1、systemd              

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

Linux-基础学习(6)

2.systemctl 命令

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

Linux-基础学习(6)



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

Linux-基础学习(6)



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

Linux-基础学习(6)



systemctl stop sshd    关闭指定服务

Linux-基础学习(6)



systemctl start sshd     开启指定服务

Linux-基础学习(6)



systemctl restart sshd     重新启动服务

Linux-基础学习(6)


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

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

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


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

Linux-基础学习(6)



systemctl mask sshd   冻结指定服务

Linux-基础学习(6)

systemctl unmask sshd   启用服务

Linux-基础学习(6)


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

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

setterm     文本界面设定color

3.服务状态

systemctl status     服务名称

 

loaded               系统服务已经初始化完成,加载过配置

active(running)    正有一个或多个程序正在系统中执行,

                     vsftpd 就是这种模式

Linux-基础学习(6)

 

atcive(exited)     仅执行一次就正常結束的服务,

                    目前并没有任何程序在系統中执行

 

atcive(waiting)    正在执行当中,

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

 

inactive            服务关闭

enbaled             服务开机启动

disabled            服务开机不自启

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

failed              系统配置错误

Linux-基础学习(6)




二、openssh-server


1.openssh-server

功能:让远程主机可以通过网络访问sshd服务,开始一个安全shell

2、网络设置

第一步:    在命令行输入 nm-connection-editor

Linux-基础学习(6)


第二步:Add一个新Network

Linux-基础学习(6)


第三步:设置名称和选择一块网卡

Linux-基础学习(6)


第四步:点击IPv4,选择静态网络Manual


Linux-基础学习(6)


第五步:保存、退出

第六步:输入ifconfig检查是否设置成功

Linux-基础学习(6)



3、客户端连接方式


命令行输入:ssh 远程主机用户@远程主机ip

例:ssh [email protected]

Linux-基础学习(6)

[[email protected] ~]# ssh [email protected]

The authenticity of host '172.25.0.11 (172.25.0.11)' 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.0.11' (ECDSA) to the list of known hosts.

[email protected]'s password:     远程用户密码

Last login: Mon Oct  3 03:13:47 2016

[[email protected] ~]#     登陆成功


ssh 远程主机用户@远程主机ip -X    调用远程主机图形工具

ssh     远程主机用户@远程主机ip command      直接在远程主机运行某条命令



4、sshkey加密

(1)生成公钥私钥

     输入:ssh-******

Linux-基础学习(6)

[[email protected] ~]# ssh-******    生成公钥私钥工具

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):[enter]  加密字符保存文件(建议用默认)

Created directory '/root/.ssh'.

Enter passphrase (empty for no passphrase): [enter]   **密码,必须>4个字符

Enter same passphrase again: [enter]   确认密码

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:

ab:3c:73:2e:c8:0b:75:c8:39:3a:46:a2:22:34:84:81 [email protected]

The key's randomart image is:



(2)[[email protected] ~]# ls /root/.ssh/

id_rsa  id_rsa.pub

id_rsa    私钥,就是钥匙

id_rsa.pub   公钥,就是锁

Linux-基础学习(6)


5、添加key认证方式

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

ssh-copy-id    添加key认证方式的工具

-i   指定加密key文件

/root/.ssh/id_rsa.pub    加密key

root    加密用户为root

172.25.254.109    被加密主机ip

Linux-基础学习(6)


6、分发钥匙给client主机

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

Linux-基础学习(6)


7、测试

[[email protected] Desktop]# ssh [email protected]      通过id_rsa直接连接不需要输入用户密码

Last login: Mon Apr  9 04:03:35 2018 from 172.25.154.109

[[email protected] ~]# 

Linux-基础学习(6)



8、提升openssh的安全级别

1.openssh-server配置文件

/etc/ssh/sshd_config

   78 PasswordAuthentication yes|no   是否开启用户密码认证,yes为支持no为关闭

   48 PermitRootLogin yes|no     是否允许超级用户登陆

   49 AllowUsers student westos    用户白名单,只有在名单中出现的用户可以使用sshd建立shell

   50 DenyUsers westos     用户黑名单


例:创建一个新的用户yu 并且:在第49行 白名单中添加此用户,表示只有yu这个用户才可以远程连接,第78行 其余用户访问直接拒绝

Linux-基础学习(6)

Linux-基础学习(6)


Linux-基础学习(6)

访问被拒绝

Linux-基础学习(6) 

当我们切换至 yu用户下时,输入 ssh [email protected],254.109   无需密码,方可进入


9、ssh登陆提示修改该

vim /etc/motd     显示登陆后字符

编辑完成后  :wq   回车保存

Linux-基础学习(6)