使用at命令来一次性执行例行性任务
一般情况下at是默认安装在系统中的
[root@server ~]# at -V at version 3.1.23 at命令的相关参数:
-l Is an alias for atq.#列出系统中所有用户的at命令 -d deletes jobs, identified by their job number.#删除指定编号的任务 -v Shows the time the job will be executed before reading the job.#使用时间格式累出at任务 Times displayed will be in the format "Thu Feb 20 14:50:001997". -c cats the jobs listed on the command line to standard output#列出任务的命令内容 -f file Reads the job from file rather than standard input.#从文件读取 [root@server ~]# at 22:07 warning: commands will be executed using /bin/sh at> ls /root >> at.txt at> job 1 at Mon Aug 21 22:07:00 2023 [root@server ~]# at -l 1 Mon Aug 21 22:07:00 2023 a root [root@server ~]# at -c 1 ... SSH_TTY=/dev/pts/0; export SSH_TTY cd /root || { echo 'Execution directory inaccessible' >&2 exit 1 } ${SHELL:-/bin/sh} << 'marcinDELIMITER5a2a43cb' ls /root > at.txt marcinDELIMITER5a2a43cb [root@server ~]# cat at.txt anaconda-ks.cfg at.txt [root@server ~]# cat at.txt hello again [root@server ~]# ls /etc/at.* /etc/at.allow /etc/at.deny 注意:at命令有黑/白名单,当两个名单重复则忽略黑名单,若两个文件不存在则只有root用户可用
crontd服务
注意:
参数:
-u 只有root才能执行该任务 -e 编辑crontab工作内容 -l 查询crontab任务内容 -r 删除cronatb任务 格式:
.---------------- minute (0 - 59) | .------------- hour (0 - 23) | | .---------- day of month (1 - 31) | | | .------- month (1 - 12) OR jan,feb,mar,apr ... | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat | | | | | * * * * * user-name command to be executed 特殊字符:
* : 任意时刻 - :表示范围 , :分割时段 */num :指定时间间隔频率 #!/bin/bash while : do echo "this is my test sh"$(date) >> /root/cro.txt sleep 1 done [root@server ~]# crontab -e(写入*/1 * * * * /bin/bash /root/test.sh) crontab: no changes made to crontab [root@server ~]# crontab -l */1 * * * * /bin/bash /root/test.sh 
[root@server ~]# crontab -r [root@server ~]# crontab -l no crontab for root [root@server ~]# ps -aux | grep test root 2723 0.1 0.1 222516 3412 ? Ss 23:14 0:00 /bin/bash /root/test.sh [root@server ~]# kill -9 2723 yum install s-nail.x86_64 -y 在文件vim /etc/s-nail.rc中末尾添加 set from=2300000080@qq.com set smtp=smtp.qq.com set smtp-auth-user=2300000080@qq.com set smtp-auth-password=tg************fd<<这是授权码 set smtp-auth=login #测试每分钟发一次 [root@server ~]# crontab -e MAILTO=23*********80@qq.com * * * * * free | mail -v -s "告警邮件" 230000000080@qq.com 
/etc/crontab文件来实现
SHELL=/bin/bash#指定哪个shell解释器 PATH=/sbin:/bin:/usr/sbin:/usr/bin#系统执行命令的搜索路径 MAILTO=root#表示执行任务的信息通过邮件发送给谁 NTP
NTP:(Network Time Protocol,网络时间协议)是由RFC 1305定义的时间同步协议,用来在分布式时间服务器和客户端之间进行时间同步。
NTP基于UDP报文进行传输,使用的UDP端口号为123
NTP可以对网络内所有具有时钟的设备进行时钟同步,使网络内所有设备的时钟保持一致,从而使设备能够提供基于统一时间的多种应用,对于运行NTP的本地系统,既可以接受来自其他时钟源的同步,又可以作为时钟源同步其他的时钟,并且可以和其他设备互相同步。
NTP的其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms
Chrony
chrony是一个开源的自由软件,它能帮助你保持系统时钟与时钟服务器(NTP)同步,因此让你的时间保持精确。
chrony由两个程序组成,分别是chronyd和chronyc
注意:Chrony与NTP都是时间同步软件,两个软件不能够同时开启,会出现时间冲突,RHEL9中默认使用chrony作为时间服务器,不在支持NTP软件包
#安装chrony,一般默认就有 [root@server ~]# yum install chrony -y 
[root@server ~]# vim /etc/chrony.conf # 配置时间服务器,以server开头,理论上添加多少时间服务器都可以。 server ntp1.aliyun.com iburst # 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整 driftfile /var/lib/chrony/drift #如果系统时钟的偏移量大于1秒,则允许系统时钟在前三次更新中步进。 makestep 1.0 3 # 将启用一个内核模式,在该模式中,系统时间每1分钟会拷贝到实时时钟(RTC)。 rtcsync # 指定一台主机、子网,或者网络以允许或拒绝向自己同步时间 #allow 192.168.0.0/16 #deny 192.168/16 allow 192.168.0.0/16 允许该网段客户端来同步时间 # 当chrony服务器提供的时间不可用时,采用本地时间作为同步标准继续作为时间服务器让其他主机来同步时间 local stratum 10 # 指定包含NTP验证密钥的文件 keyfile /etc/chrony.keys # 指定存放日志文件的目录 logdir /var/log/chrony #选择日志文件要记录的信息 log measurements statistics tracking /etc/chrony.conf文件,修改后使用systemctl restart chronyd命令重启服务
修改第三行和第25行 3 server ntp.aliyun.com iburst#指定服务器 25 allow 192.168.110.0/24#允许哪个客户端访问 [root@server ~]# date -s "12:00:00 19970101" 1997年 01月 01日 星期三 12:00:00 CST chronyc sources -v来查看与阿里云时间服务器的连接情况[root@server ~]# chronyc sources -V MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 203.107.6.88 2 6 377 44 -1654us[-2677us] +/- 29ms chronyc -a makestep强制进行时间跳跃到ntp服务器的时间并用timedatectl status查看同步状态[root@server ~]# chronyc -a makestep 200 OK [root@server ~]# timedatectl set-ntp true [root@server ~]# timedatectl status Local time: 二 2023-08-22 00:28:57 CST Universal time: 一 2023-08-21 16:28:57 UTC RTC time: 一 2023-08-21 16:28:57 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes <<<<<表示已经同步 NTP service: active RTC in local TZ: no 此时服务器端配置完成
客户端同样安装软件
/etc/chrony.conf3 server 192.168.110.131 iburst [root@node1 ~]# chronyc sources -V MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 192.168.110.131 3 6 7 1 +7272ns[-9350us] +/- 31ms [root@node1 ~]# timedatectl status Local time: 二 2023-08-22 00:50:52 CST Universal time: 一 2023-08-21 16:50:52 UTC RTC time: 一 2023-08-21 16:50:52 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: no 配置成功,同步完成了
不安全的远程连接
#安装 yum install telnet-server.x86_64 -y #启动 systemctl start telnet.socket 使用Xshell进行telnet连接,并抓包查看明文传输
SSH工作过程:
| 过程 | 说明 |
|---|---|
| 版本号协商阶段 | SSH目前包括SSH1和SSH2两个版本,双方通过版本协商确定使用的版本 |
| **密钥和算法协商阶段 ** | SSH支持多种加密算法,双方根据本端和对端支持的算法,协商出最终使用的算法 |
| 认证阶段 | SSH客户端向服务器端发起认证请求,服务器端对客户端进行认证 |
| 会话请求阶段 | 认证通过后,客户端向服务器端发送会话请求 |
| 交互会话阶段 | 会话请求通过后,服务器端和客户端进行信息的交互 |
版本协商阶段
服务器端打开端口22,等待客户端连接;
客户端向服务器端发起TCP初始连接请求,TCP连接建立后,服务器向客户端发送第一个报文,包括版本标志字符串,格式为SSH-<主协议版本号>.<次协议版本号>.<软件版本号>,协议版本号由主版本号和次版本号组成,软件版本号主要是为调试使用。
客户端收到报文后,解析该数据包,如果服务器的协议版本号比自己的低,且客户端能支持服务器端的低版本,就使用服务器端的低版本协议号,否则使用自己的协议版本号。
客户端回应服务器一个报文,包含了客户端决定使用的协议版本号。服务器比较客户端发来的版本号,决定是否能同客户端一起工作。如果协商成功,则进入密钥和算法协商阶段,否则服务器断开TCP连接
注意:上述报文都是采用明文方式传输
密钥和算法协商阶段
服务器端和客户端分别发送算法协商报文给对端,报文中包含自己支持的公钥算法列表、加密算法列表、MAC(Message Authentication Code,消息验证码)算法列表、压缩算法列表等等
服务器端和客户端根据对端和本端支持的算法列表得出最终使用的算法
服务器端和客户端利用DH交换(Diffie-Hellman Exchange)算法、主机密钥对等参数,生成会话密钥和会话ID。
由此,服务器端和客户端就取得了相同的会话密钥和会话ID。对于后续传输的数据,两端都会使用会话密钥进行加密和解密,保证了数据传送的安全。在认证阶段,两端会使用会话用于认证过程
会话密钥的生成:
认证阶段(两种认证方法):
基于口令的认证(password认证):客户端向服务器发出password认证请求,将用户名和密码加密后发送给服务器,服务器将该信息解密后得到用户名和密码的明文,与设备上保存的用户名和密码进行比较,并返回认证成功或失败消息。
基于密钥的认证(publickey认证):
注:服务器端对客户端进行认证,如果认证失败,则向客户端发送认证失败消息,其中包含可以再次认证的方法列表。客户端从认证方法列表中选取一种认证方法再次进行认证,该过程反复进行。直到认证成功或者认证次数达到上限,服务器关闭连接为止
ssh服务系统默认自带
安装:yum install openssh-server
[root@server ~]# useradd redhat [root@server ~]# passwd redhat 更改用户 redhat 的密码 。 新的密码: 无效的密码: 密码少于 8 个字符 重新输入新的密码: passwd:所有的身份验证令牌已经成功更新。 [root@node1 ~]# ssh-keygen -t rsa #<<不输东西,一路回车 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. 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: SHA256:EJhw9eo6zukZmrZ7aT+U0IAg04KYwmpryCbs+f31G5o root@node1 The key‘s randomart image is: +---[RSA 3072]----+ |B+.o.+o | |Boo.+ o | |o. o. . | |.. . .o | |= . ...S | |o* .o | |= . .o. . . | | o.+*=. . + . | | .*OB+oo E o. | +----[SHA256]-----+ #将公钥上传给server的redhat用户 [root@node1 ~]# ssh-copy-id redhat@192.168.110.131 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.110.131 (192.168.110.131)' can’t be established. ED25519 key fingerprint is SHA256:iPPpVwQBFSX//xH3pUjZVklEVPozcl+iv4mX1Tb3gZs. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes <<这里输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 redhat@192.168.110.131’s password: <<<这里密码不回显 Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'redhat@192.168.110.131'" and check to make sure that only the key(s) you wanted were added. 
Web网络服务也叫WWW(World Wide Web 全球信息广播)万维网服务,一般是指能够让用户通过浏览器访问到互联网中文档等资源的服务
Web 网络服务是一种被动访问的服务程序,即只有接收到互联网中其他主机发出的请求后才会响应,最终用于提供服务程序的 Web 服务器会通过 HTTP(超文本传输协议)或HTTPS(安全超文本传输协议)把请求的内容传送给用户
网站是由域名、网页源程序和主机空间组成的,其中主机空间则是用于存放网页源代码并能够将网页内容展示给用户,虽然本小节与Apache服务没有直接关系,但如果您想要在互联网中搭建网站并被顺利访问,主机空间一定不能选错
虚拟主机:在一台服务器中分出一定的磁盘空间供用户放置网站、存放数据等,仅提供基础的网站访问、数据存放与传输流量功能,能够极大的降低用户费用,也几乎不需要管理员维护除网站数据以外的服务,适合小型网站
VPS(Virtual Private Server):在一台服务器中利用OpenVZ、Xen或KVM等虚拟化技术模拟出多个“主机”,每个主机都有独立的IP地址、操作系统,实现不同VPS之间磁盘空间、内存、CPU资源、进程与系统配置间的完全隔离,管理员可自由使用分配到的主机中的所有资源,所以需要有一定的维护系统的能力,适合小型网站
云服务器(ECS):是一种整合了计算、存储、网络,能够做到弹性伸缩的计算服务,其使用起来与VPS几乎一样,但差别是云服务器建立在一组集群服务器中,每个服务器都会保存一个主机的镜像(备份),大大的提升了安全稳定性,另外还具备了灵活性与扩展性,用户只需按使用量付费的即可,适合大中小型网站。
独立服务器:这台服务器仅提供给您使用,详细来讲又可以区分为租用方式与托管方式
/var/www/html 。<协议>://<主机或主机名>[:port]/<目录资源,路径>**终端客户在web浏览器地址栏输入访问地址:http://www.ceshi.com:80/index.html
web浏览器请求DNS服务器把域名www.ceshi.com解析成web服务器的IP地址
web浏览器将端口号(默认是80)从访问地址(URL)中解析出来
web浏览器通过解析后的ip地址及端口号与web服务器之间建立一条TCP连接
建立TCP连接后,web浏览器向web服务器发送一条HTTP请求报文
web服务器响应并读取浏览器的请求信息,然后返回一条HTTP响应报文。
web服务器关闭HTTP连接,关闭TCP连接,web浏览器显示访问的网站内容到屏幕上
该站可以让服务器与用户互动,常见的例如留言板,博客。这种类型的网站需要通过“网页程序语言”来实现与用户互动的行为。常见的例如:PHP网页程序语言,配合数据库系统来进行数据的读、写。当你在向服务器请求数据时,其实是通过服务器端同一个网页程序在负责将数据读出或写入数据库,变动的是数据库的内容,网页程序并没有任何改变。
另外一种交互式的动态网页主要是在客户端实现。服务端将可执行的程序代码(JavaScript)传送给客户端,客户端的浏览器如果提供JavaScript的功能,那么该程序就可以在客户端的计算机上面工作了;另外一种可在客户端执行的就是flash动画格式,在这种动画格式内还可以进行程序设计
搭建动态网站的需求:LAMP(Linux+Apache+MySQL+PHP)
httpd的配置目录
/etc/httpd # 主配置文件所在目录 /etc/httpd/conf/httpd.conf # 主配置文件 /var/www/html/ # 存储默认网页的目录 /var/log/httpd/ # 存储网站访问日志及错误日志的目录 httpd的主配置文件
34 ServerRoot "/etc/httpd"#服务目录 47 Listen 80#监听端口 61 Include conf.modules.d/*.conf#额外的配置文件 71 User apache#运行服务的用户 72 Group apache#运行服务的组 91 ServerAdmin root@localhost#管理员邮箱 100 ServerName www.example.com:80#域名和端口号 107 #该字段设置目录权限 108 AllowOverride none#不允许覆盖 109 Require all denied/granted#禁止/允许所有来源访问文件或目录 110 124 DocumentRoot "/var/www/html"#*网页存放的默认目录* 第一步:下载httpd,启动服务器
[root@leqingserver ~]# yum install httpd -y [root@leqingserver ~]# systemctl start httpd 第二步:写一个网页,重启服务并访问
[root@leqingserver ~]# echo "welcome to myblog" > /var/www/html/index.html [root@leqingserver ~]# systemctl restart httpd 第三步:输入服务器IP访问
把网页放在默认的目录是不安全的,接下来尝试更改一下配置文件来让网页换一个目录存放
1.创建一个目录,并给这个目录里面放一个网页文件
[root@leqingserver ~]# mkdir /web1 [root@leqingserver ~]# ls /web1/ index.html 2.修改主配置文件,重启服务并测试
DocumentRoot "/web1" AllowOverride None Require all granted [root@leqingserver ~]# systemctl restart httpd 
[root@leqingserver ~]# vim /etc/httpd/conf.d/userdir.conf 17 #UserDir disabled#将这行注释掉开启用户主页功能 24 UserDir public_html#去掉这行的注释,启动用户的默认目录 [root@leqingserver ~]# useradd andy [root@leqingserver ~]# passwd andy Changing password for user andy. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: #123456 passwd: all authentication tokens updated successfully. [root@leqingserver ~]# useradd jenny [root@leqingserver ~]# passwd jenny Changing password for user jenny. New password: #654321 BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully. [root@leqingserver ~]# su andy [andy@leqingserver root]$ cd /home/andy/ [andy@leqingserver ~]$ mkdir public_html [andy@leqingserver ~]$ cd public_html/ [andy@leqingserver public_html]$ echo "andy" > index.html [andy@leqingserver public_html]$ ls index.html [andy@leqingserver public_html]$ cd ~ [andy@leqingserver ~]$ chmod -Rf 755 /home/andy/ [andy@leqingserver ~]$ ll total 0 drwxr-xr-x. 2 andy andy 24 Aug 24 17:06 public_html [andy@leqingserver ~]$ su jenny Password: [jenny@leqingserver andy]$ cd /home/jenny/ [jenny@leqingserver ~]$ mkdir public_html [jenny@leqingserver ~]$ cd public_html/ [jenny@leqingserver public_html]$ echo "jenny" > index.html [jenny@leqingserver public_html]$ ls index.html [jenny@leqingserver public_html]$ cd ~ [jenny@leqingserver ~]$ chmod -Rf 755 /home/jenny/ [jenny@leqingserver ~]$ ll total 0 drwxr-xr-x. 2 jenny jenny 24 Aug 24 17:08 public_html 

[root@leqingserver ~]# htpasswd -c /etc/httpd/passwd andy New password: Re-type new password: Adding password for user andy [root@leqingserver ~]# htpasswd /etc/httpd/passwd jenny New password: Re-type new password: Adding password for user jenny [root@leqingserver ~]# vim /etc/httpd/conf.d/userdir.conf authuserfile "/etc/httpd/passwd" authname "My privately" authtype basic require user andy authuserfile "/etc/httpd/passwd" authname "My privately" authtype basic require user jenny 测试

这里出现的域名均为虚构域名,无法在公网访问使用虚拟机进行测试
#安装软件 yum install httpd mod_ssl -y #本地域名映射 vim /etc/hosts 192.168.110.131 www.open.com mkdir -p /www/open echo 'welcome to open.com' > /www/open/index.html ls /www/open/ index.html [root@server ~]# vim /etc/httpd/conf/httpd.conf documentroot /www/open servername 'www.open.com' allowoverride none require all granted [root@server ~]# systemctl restart httpd 
[root@server ~]# mkdir -p /www/open/data [root@server ~]# echo 'data' > /www/open/data/index.html #编辑配置文件 documentroot /www/open/data alias /data /www/open/data servername 'www.open.com' allowoverride none require all granted 
#创建目录和学生用户 [root@server ~]# mkdir -p /www/open/student [root@server ~]# echo 'student' >/www/open/student/index.html [root@server ~]# ls /www/open/student/ index.html [root@server ~]# useradd song [root@server ~]# passwd song 更改用户 song 的密码 。 新的密码: 无效的密码: 密码少于 8 个字符 重新输入新的密码: passwd:所有的身份验证令牌已经成功更新。 [root@server ~]# useradd tian [root@server ~]# passwd tian 更改用户 tian 的密码 。 新的密码: 无效的密码: 密码少于 8 个字符 重新输入新的密码: passwd:所有的身份验证令牌已经成功更新。 设置网页的密码验证:
[root@server ~]# htpasswd -c /etc/httpd/passwd song New password: Re-type new password: Adding password for user song [root@server ~]# htpasswd /etc/httpd/passwd tian New password: Re-type new password: Adding password for user tian [root@server ~]# vim /etc/httpd/conf/httpd.conf authuserfile /etc/httpd/passwd authname 'student' authtype basic require user song tian 重启验证:

[root@server ~]# mkdir -p /www/open/money [root@server ~]# echo 'money' > /www/open/money/index.html #创建私钥 [root@server ~]# openssl genrsa -aes128 2048 > /etc/pki/tls/private/money.key Enter PEM pass phrase: Verifying - Enter PEM pass phrase: #创建证书 [root@server ~]# openssl req -utf8 -new -key /etc/pki/tls/private/money.key -x509 -days 365 -out /etc/pki/tls/certs/money.crt Enter pass phrase for /etc/pki/tls/private/money.key: You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:86#国家 State or Province Name (full name) []:shanxi#省份 Locality Name (eg, city) [Default City]:xianggang#城市 Organization Name (eg, company) [Default Company Ltd]:open#公司 Organizational Unit Name (eg, section) []:money#部门 Common Name (eg, your name or your server's hostname) []:server#主机名 Email Address []:123@qq.com#管理员邮箱 编写主配置文件vim /etc/httpd/conf/httpd.conf
sslengine on SSLCertificatefile /etc/pki/tls/certs/money.crt sslcertificatekeyfile /etc/pki/tls/private/money.key documentroot /www/open alias /money /www/open/money servername 'www.open.com' allowoverride none require all granted [root@server ~]# systemctl restart httpd 🔐 Enter TLS private key passphrase for www.open.com:443 (RSA) : ****** 
[root@server ~]\# yum install bind -y 配置服务器正向解析
配置DNS主配置文件vim /etc/named.conf
[root@server ~]\# vim /etc/named.conf 11 listen-on port 53 { 192.168.110.131; };#监听的ip和端口号 19 allow-query { any; };#允许访问的地址 编辑区域配置文件的正向解析模块vim /etc/named.rfc1912.zones
[root@server ~]# vim /etc/named.rfc1912.zones zone "zyc.com" IN { type master; file "zyc.com.zone"; allow-update { none; }; }; 
编辑数据配置文件
#将模板复制一份 [root@server ~]\# cp -a /var/named/named.localhost /var/named/zyc.com.zone [root@server ~]\# vim /var/named/zyc.com.zone $TTL 1D zyc.com. IN SOA www.zyc.com. zyc.qq.com ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum zyc.com. IN NS www.zyc.com. www.zyc.com. IN A 192.168.110.131 ftp.zyc.com. IN A 192.168.110.131 www6.zyc.com. IN CNAME www.zyc.com. 
重启服务器
[root@server ~]# systemctl restart named 配置客户端的DNS地址为server的地址,并测试解析结果
编辑网卡配置文件并重启网卡
[root@node2 ~]# vim /etc/NetworkManager/system-connections/ens160.nmconnection [ipv4] address1=192.168.110.133/24,192.168.110.2 dns=192.168.110.131; #这里修改为server的地址 method=manual [root@node2 ~]# nmcli connection reload #重新加载配置文件 [root@node2 ~]# nmcli connection up ens160 #重新激活网卡 连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/3) 测试结果
[root@node2 ~]# nslookup www.zyc.com Server: 192.168.110.131 Address: 192.168.110.131#53 Name: www.zyc.com Address: 192.168.110.131 [root@node2 ~]# nslookup ftp.zyc.com Server: 192.168.110.131 Address: 192.168.110.131#53 Name: ftp.zyc.com Address: 192.168.110.131 [root@node2 ~]# nslookup www6.zyc.com Server: 192.168.110.131 Address: 192.168.110.131#53 www6.zyc.com canonical name = www.zyc.com. Name: www.zyc.com Address: 192.168.110.131 配置服务器反向解析
编辑区域配置文件的反向解析模块
[root@server ~]# vim /etc/named.rfc1912.zones zone "110.168.192.in-addr.arpa" IN { type master; file "192.168.110.arpa"; allow-update { none; }; }; 
编辑反向解析的数据配置文件
[root@server ~]\# cp -a /var/named/named.loopback /var/named/192.168.110.arpa [root@server ~]\# vim /var/named/192.168.110.arpa $TTL 1D @ IN SOA www.zyc.com wzh.qq.com ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS www.zyc.com. 131 IN PTR www.zyc.com. 131 IN PTR ftp.zyc.com. 
重启服务并测试
[root@server ~]# systemctl restart named #重启服务 [root@node2 ~]# nslookup #测试正反向解析 > www.zyc.com Server: 192.168.110.131 Address: 192.168.110.131#53 Name: www.zyc.com Address: 192.168.110.131 > 192.168.110.131 131.110.168.192.in-addr.arpa name = ftp.zyc.com. 131.110.168.192.in-addr.arpa name = www.zyc.com. 
LAMP简介:
LAMP架构是目前成熟的企业网站应用模式之一,指的是协同工作的一整台系统和相关软件,能够提供动态web站点服务及其应用开发环境
1. 搭建LAMP环境
yum install httpd mariadb-server php* -y 2. 上传nextcloud-16版本软件包到服务器,并解压缩
#使用xftp或者其他传输工具进行上传 [root@leqingserver ~]\# cd / [root@leqingserver /]\# ls bin dev home lib64 mnt opt root sbin sys usr boot etc lib media nextcloud-16.0.1.zip proc run srv tmp var [root@leqingserver /]\# unzip /nextcloud-16.0.1.zip #解压缩 3. 设置nextcloud的目录权限
[root@leqingserver /]\# chmod -Rf 777 /nextcloud 4. 设置nextcloud的数据库
[root@leqingserver /]# systemctl start mariadb [root@leqingserver /]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 8 Server version: 10.3.28-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> create database nextcloud; Query OK, 1 row affected (0.003 sec) #创建一个数据库 MariaDB [(none)]> create user 'nextcloud'@'localhost' identified by '********'; Query OK, 0 rows affected (0.003 sec) #创建一个用户和密码,此处*号代替密码 MariaDB [(none)]> grant all on nextcloud.* to 'nextcloud'@'localhost'; Query OK, 0 rows affected (0.004 sec) #设置权限 MariaDB [(none)]> exit #退出 Bye [root@leqingserver /]# systemctl restart mariadb #重启服务 5. 配置httpd服务
[root@leqingserver ~]\# vim /etc/httpd/conf/httpd.conf 122 DocumentRoot "/nextcloud" # 127 128 AllowOverride None 130 Require all granted 131 [root@leqingserver /]# systemctl restart httpd #重启服务 6. 安装nextcoud
点红色箭头标识处,展开数据库配置
填写数据库相关配置,并创建管理员的账户密码
然后点击安装,并等待安装完成
此时我们的私有云存储就部署好了。
SELinux(Security-Enhanced Linux)是美国国家安全局在 Linux 开源社区的帮助下开发的一个强制访问控制(MAC,Mandatory Access Control)的安全子系统,用于各个服务进程都受到约束,使其仅获取到本应获取的资源
作用:
SELinux 域限制:对服务程序的功能进行限制,以确保服务程序做不了出格的事情
SELinux 安全上下文:对文件资源的访问限制,确保文件资源只能被其所属的服务程序访问
SELinux工作原理
主体(Subject):
目标(Object):
策略(Policy):
Linux 系统中进程与文件的数量庞大,限制进程是否可以访问文件的 SELinux 规则数量就更加烦琐,如果每个规则都需要管理员手工设定,那么 SELinux 的可用性就会极低,所以SELinux 默认定义了两个策略来制订规则
2个默认策略
安全上下文(Security Context)

[root@server ~]\# ls -Z system_u:object_r:admin_home_t:s0 anaconda-ks.cfg system_u:object_r:admin_home_t:s0 awk1.txt system_u:object_r:admin_home_t:s0 awk2.txt system_u:object_r:admin_home_t:s0 awk3.txt system_u:object_r:admin_home_t:s0
这个字段使用3个:分成四个部分
system_u:身份标识(identify)
[root@server ~]\# semanage login -l 登录名 SELinux 用户 MLS/MCS 范围 服务 __default__ unconfined_u s0-s0:c0.c1023 * root unconfined_u s0-s0:c0.c1023 * system_u:系统用户身份,其中“_u”代表 user
注意:user 字段只用于标识数据或进程被哪个身份所拥有,系统数据的 user 字段是 system_u,用户数据 user 字段是 user_u
seinfo 命令
作用:查询身份、角色等信息,需要安装才可使用
[root@server ~] \# yum install setools-console -y 参数:
-u: 列出SELinux中所有的身份(user); -r: 列出SELinux中所有的角色(role); -t: 列出SELinux中所有的类型(type); -b: 列出所有的布尔值(也就是策略中的具体规则名称); -x: 显示更多的信息; 角色(role):表示此数据是进程还是文件或目录包含(了解就行)
类型(type):
seinfo -t | more # 5049个类型 最重要,进程是否可以访问文件,主要就是看进程的安全上下文类型字段是否和文件的安全上下文类型字段相匹配
在默认的targeted策略中
类型字段在主体(进程)的安全上下文中被称作域(domain)
类型字段在目标(文件或目录)的安全上下文中被称作类型(type)
进程的域与文件的类型是否匹配需要查询策略规则
灵敏度:用 s0、s1、s2 来命名,数字为灵敏度分级,数值越大,灵敏度越高
例:
[root@server ~]# yum install httpd -y [root@server ~]# ls -Zd /var/www/html system_u:object_r:httpd_sys_content_t:s0 /var/www/html 三种配置模式:
查看当前工作模式
[root@server ~]\# getenforce 临时开启selinux/临时关闭selinux
[root@server ~]# getenforce Enforcing [root@server ~]# setenforce 0 # 临时关闭进入宽容模式 [root@server ~]# getenforce Permissive [root@server ~]# setenforce 1 # 临时开启 [root@server ~]# getenforce Enforcing 永久性关闭
[root@server ~]# vim /etc/selinux/config SELINUX=disabled 注意:
环境准备:
| 主机 | 主机名 | 服务 |
|---|---|---|
| 192.168.110.131 | server | web |
| 192.168.110.132 | server-nfs | nfs |
| 192.168.110.133 | server-dns | dns |
业务需求:
server主机:配置web服务,内网通过域名访问web网站
server-nfs主机:配置nfs服务,将网站资源共享给server主机
server-dns主机:配置dns服务,分配dns给其他主机
#这是一个示例,三台主机用三个ip,同一网关。 [root@server ~]# nmcli connection modify ens160 ipv4.addresses 192.168.110.131/24 ipv4.gateway 192.168.110.2 ipv4.dns 114.114.114.114 ipv4.method manual #修改主机名 hostnamectl set-hostname server-nfs hostnamectl set-hostname server-dns 三台主机写入以下内容 vim /etc/hosts 192.168.110.131 server 192.168.110.132 server-nfs 192.168.110.133 server-dns systemctl start firewalld.service systemctl enable firewalld.service #nfs和dns端开启SElinux vim /etc/selinux/config SELINUX=enforcing # server配置 [root@server ~]# yum install chrony 3 server ntp.aliyun.com iburst 26 allow 192.168.110.0/24 [root@server ~]# systemctl restart chronyd.service [root@server ~]# chronyc sources -V MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 203.107.6.88 2 6 177 5 -1331us[-3093us] +/- 32ms #为保证其他两台主机正常进行时间同步,server端开启chronyd服务要用的123端口 [root@server ~]# firewall-cmd --zone=public --add-port=123/udp --permanent success # nfs和dns配置 [root@server-nfs ~]# yum install chrony 3 server 192.168.110.131 iburst [root@server-nfs ~]# systemctl restart chronyd.service #测试时间同步状态 [root@server-nfs ~]# chronyc sources -V MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* server 3 6 17 2 -47us[ -65us] +/- 37ms [root@server-dns ~]# chronyc sources -V MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* server 3 6 7 1 +4908ns[-8949us] +/- 36ms server端:生成公私钥对 [root@server ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. 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: SHA256:cUNOiqMh/L1+60otU9x1OKbk2gsBZTzyc/oNwGnlKZ4 root@server The key‘s randomart image is: +---[RSA 3072]----+ | .o o | | . .+o=. . | | o . ++o==.= . | | o + +O*+= o | | o .oSB+ | | +E+. | | = +..o | | o o..... | | o+o.. | +----[SHA256]-----+ [root@server ~]# ssh-copy-id 192.168.110.132 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.110.132 (192.168.110.132)' can’t be established. ED25519 key fingerprint is SHA256:iPPpVwQBFSX//xH3pUjZVklEVPozcl+iv4mX1Tb3gZs. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? y Please type 'yes', 'no' or the fingerprint: 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 root@192.168.110.132‘s password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.110.132'" and check to make sure that only the key(s) you wanted were added. #nfs和dns端同理 [root@server-nfs ~]# ssh-keygen -t rsa 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: SHA256:ghfIloGlX3FyXC6ZHBlW3IwDTSEuDdrLjBLoFiEJw7o root@server-nfs The key’s randomart image is: +---[RSA 3072]----+ |*o oo +.OX== | |o+oo * @o*= o | |o.o * = B .. | |o .+ * + . | | +. + * S | |E . . . | | | | | | | +----[SHA256]-----+ [root@server-nfs ~]# ssh-copy-id 192.168.110.131 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.110.131 (192.168.110.131)' can‘t be established. ED25519 key fingerprint is SHA256:iPPpVwQBFSX//xH3pUjZVklEVPozcl+iv4mX1Tb3gZs. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? 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 root@192.168.110.131’s password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.110.131'" and check to make sure that only the key(s) you wanted were added. # dns端同样操作 #在server端安装LAMP环境软件 [root@server ~] yum install httpd mariadb-server php* -y 上传软件包 [root@server-nfs /]# ls afs boot etc lib media opt root sbin sys usr wordpress-6.1-zh_CN.zip bin dev home lib64 mnt proc run srv tmp var [root@server-nfs /]\# unzip wordpress-6.1-zh_CN.zip [root@server-nfs /]\# cd wordpress/ [root@server-nfs wordpress]\# ls index.php wp-admin wp-content wp-load.php wp-signup.php license.txt wp-blog-header.php wp-cron.php wp-login.php wp-trackback.php readme.html wp-comments-post.php wp-includes wp-mail.php xmlrpc.php wp-activate.php wp-config-sample.php wp-links-opml.php wp-settings.php server-nfs端的/wordpress目录共享给server主机
[root@server-nfs ~]\# yum install rpcbind -y [root@server-nfs ~]\# yum install nfs-utils -y [root@server-nfs ~]\# vim /etc/exports #编辑配置文件 /wordpress 192.168.110.131(rw,sync,all_squash) #设置权限 [root@server-nfs ~]\# chmod -Rf 777 /wordpress # 配置防火墙放行规则 [root@server-nfs ~]\# firewall-cmd --permanent --zone=public --add-service=mountd success [root@server-nfs ~]\# firewall-cmd --permanent --zone=public --add-service=rpc-bind success [root@server-nfs ~]\# firewall-cmd --permanent --zone=public --add-service=nfs success [root@server-nfs ~]\# firewall-cmd --reload success [root@server-nfs ~]\# systemctl start rpcbind.service [root@server-nfs ~]\# systemctl start nfs-server.service # 设置SEkinux上下文策略 [root@server-nfs ~]\# chcon -t httpd_sys_content_t /wordpress -Rv [root@server ~]\# yum install rpcbind -y [root@server ~]\# yum install nfs-utils -y [root@server ~]\# showmount -e 192.168.110.132 Export list for 192.168.110.132: /wordpress 192.168.110.131 [root@server ~]\# mkdir /wp #将远程目录挂载本地 [root@server ~]\# mount -t nfs 192.168.110.132:/wordpress /wp [root@server ~]# cd /wp/ [root@server wp]# ls #配置防火墙放行 [root@server ~]\# firewall-cmd --permanent --zone=public --add-service=http success [root@server ~]\# firewall-cmd --reload success #修改httpd配置文件 [root@server ~]\# vim /etc/httpd/conf/httpd.conf 124 DocumentRoot "/wp" 129 130 AllowOverride None 132 Require all granted 133 [root@server ~]\# cd /wp/ # 拷贝模板文件 [root@server wp]\# cp wp-config-sample.php wp-config.php #编辑wp-config.php配置文件 [root@server wp]\# vim wp-config.php 23 define( 'DB_NAME', 'wordpress' ); 26 define( 'DB_USER', 'root' ); 29 define( 'DB_PASSWORD', '123456' ); [root@server ~]# systemctl start mariadb.service [root@server ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 10.5.16-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> create database wordpress; Query OK, 1 row affected (0.000 sec) MariaDB [(none)]> alter user 'root'@'localhost' identified by '123456'; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]> grant all on wordpress.* to 'root'@'localhost'; Query OK, 0 rows affected (0.002 sec) MariaDB [(none)]> exit; Bye #重启数据库和httpd [root@server ~]# systemctl restart mariadb [root@server ~]# systemctl restart httpd 在浏览器输入server主机的ip来进行安装

[root@server-dns ~]\# yum install bind -y [root@server-dns ~]\# firewall-cmd --permanent --zone=public --add-service=dns success [root@server-dns ~]\# firewall-cmd --reload success [root@server-dns ~]\# systemctl restart named #编辑主配置文件 [root@server-dns ~]\# vim /etc/named.conf 11 listen-on port 53 { any; }; 19 allow-query { any; }; #编辑区域配置文件 [root@server-dns ~]\# vim /etc/named.rfc1912.zones zone "yitu.com" IN { type master; file "yitu.com.zone"; allow-update { none; }; }; [root@server-dns named]\# vim yitu.com.zone $TTL 1D @ IN SOA yitu.com. wzh.yitu.com. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS www.yitu.com. www A 192.168.110.131 在浏览器输入域名进行验证
此时可以看到我们通过域名成功进行了网站访问。本次实验我们通过三台主机进行配置,分别承担了httpd、NFS、DNS三种服务,他们都是较为安全的,因为我们开启了防火墙和SELinux在这些主机上面,这种方式是分布式的,可以有效防止服务器宕机造成的影响,减少了宕机后服务恢复的时间。