linux应用管理28-37

案例28:vim文本编辑器练习,命令模式下的基本操作(重点知识)
1)将/etc/passwd的前10行,写入到文件/opt/pass30.txt
[root@server0 ~]# head -10 /etc/passwd
[root@server0 ~]# head -10 /etc/passwd > /opt/pass30.txt
2)将/etc/shadow的前20行,追加写入到文件/opt/pass30.txt
[root@server0 ~]# tail -20 /etc/passwd
[root@server0 ~]# tail -20 /etc/passwd >> /opt/pass30.txt
3)利用/opt/pass30.txt,进项如下操作:(练习操作,练习三遍)
– 命令模式下移动光标:键盘上下左右键、Home键、End键
– 命令模式下行间跳转:到全文的第一行(1G或gg)、到全文的最后一行(G)、到全文的第12行(10G)
命令模式下复制、粘贴:(练习操作,练习三遍)
-复制2行(yy)、复制4行(4yy)
-粘贴到当前行之后(小写p)
4)命令模式下删除:(练习操作,练习三遍)
-删除单个字符(x)
-删除到行首(d^)、删除到行尾(d$)
-删除第3行(光标在第三行dd)、同时删除3行(3dd)
5)命令模式下查找关键词:(练习操作,练习三遍)
-搜索(/word)切换结果(n、N)
6)进入插入模式操作(练习操作,练习三遍)
-在命令模式下大写的C,可以删除光标之后,并且进入插入模式

案例29:在 server0、desktop0 上操作
将防火墙默认区域设置为trusted
虚拟机Server0
[root@server0 ~]# firewall-cmd --set-default-zone=trusted
虚拟机Desktop0
[root@desktop0 ~]# firewall-cmd --set-default-zone=trusted

案例30:在server上操作,搭建mariadb数据库系统
在 server0 上安装 MariaDB 数据库系统
-安装 mariadb-server软件包
-启动 mariadb 服务
1.安装mariadb-server:提供服务端有关的系统程序
[root@server0 ~]# yum -y install mariadb-server

2.重起服务
[root@server0 ~]# systemctl restart mariadb
[root@server0 ~]# systemctl enable mariadb

案例31:在server上操作,配置一个数据库
1)为mariadb数据库root设置登陆密码为 haxi
在Linux系统命令行,为数据库管理员设置密码haxi
[root@server0 ~]# mysqladmin -u root password ‘haxi’
2)新建一个数据库名为 nsd
[root@server0 ~]# mysql -u root –phaxi

show databases; #显示所有的库
create database nsd; #创建nsd库
show databases; #显示所有的库
exit #退出
3)除了 root 用户,此nsd数据库只能被用户 lisi 查询,此用户的密码为123(用户的授权)
[root@server0 ~]# mysql -u root –phaxi
grant select on nsd.* to lisi@localhost identified by ‘123’;
4)数据库 nsd 中应该包含来自数据库复制的内容(导入数据)
-数据库文件的 URL为:http://classroom/pub/materials/users.sql
1.Linux命令行,下载备份文件
[root@server0 ~]# wget http://classroom.example.com/pub/materials/users.sql
[root@server0 ~]# ls

2.Linux命令行,导入数据到nsd库
[root@server0 ~]# mysql -u root -phaxi nsd < users.sql
[root@server0 ~]# mysql -u root -phaxi
MariaDB [(none)]> use nsd; #进入nsd库
MariaDB [nsd]> show tables; #显示所有表格
±--------------+
| Tables_in_nsd |
±--------------+
| base |
| location |
±--------------+
2 rows in set (0.00 sec)

案例32:在server上操作,使用数据库查询
1)密码是 solicitous 的人的名字?(写出查询命令)
[root@server0 ~]# mysql -u root -phaxi
MariaDB [(none)]> use nsd; #进入nsd库
MariaDB [nsd]> select * from base where password=‘solicitous’;
2)有多少人的姓名是 Barbara 同时居住在 Sunnyvale? (写出查询命令及结果)
[root@server0 ~]# mysql -u root -phaxi
MariaDB [(none)]> use nsd; #进入nsd库
MariaDB [nsd]> select * from base,location
where base.name=‘Barbara’ and
location.city=‘Sunnyvale’ and
base.id=location.id;

MariaDB [nsd]> select count(*) from base,location #统计匹配条件记录
where base.name=‘Barbara’ and
location.city=‘Sunnyvale’ and
base.id=location.id;
3)在base表中追加记录 id为6,name为Barbara,password为900(写出插入表记录命令)
[root@server0 ~]# mysql -u root -phaxi
MariaDB [(none)]> use nsd; #进入nsd库
MariaDB [nsd]> insert base values (‘6’,‘Barbara’,‘900’);
MariaDB [nsd]> select * from base;
4)在location表中追加记录 id为6,city为Sunnyvale(写出插入表记录命令)
[root@server0 ~]# mysql -u root -phaxi
MariaDB [(none)]> use nsd; #进入nsd库
MariaDB [nsd]> insert location values (‘6’,‘Sunnyvale’);
MariaDB [nsd]> select * from location;
5)再次查询有多少人的姓名是 Barbara 同时居住在 Sunnyvale? (写出查询命令及结果)
[root@server0 ~]# mysql -u root -phaxi
MariaDB [(none)]> use nsd; #进入nsd库
MariaDB [nsd]> select * from base,location
where base.name=‘Barbara’ and
location.city=‘Sunnyvale’ and
base.id=location.id;

MariaDB [nsd]> select count(*) from base,location #统计匹配条件记录
where base.name=‘Barbara’ and
location.city=‘Sunnyvale’ and
base.id=location.id;

案例33:虚拟机 server0操作, /dev/vdb 上按以下要求建立分区:
采用默认的 msdos 分区模式
– 第1个分区 /dev/vdb1 的大小为 3G
– 第2个分区 /dev/vdb2 的大小为 200M
– 第3个分区 /dev/vdb3 的大小为 100M
– 第4个分区 /dev/vdb4为扩展分区
– 在划分二个分区逻辑分区/dev/vdb[5-6],
– 分区大小依次为500M、2000M
[root@server0 ~]# lsblk
[root@server0 ~]# fdisk /dev/vdb
……
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分区号 (1-4,默认 1):#回车
起始 扇区 (2048-167772159,默认为 2048): #回车
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-167772159,默认为 167772159):+3G

命令(输入 m 获取帮助):n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): #回车
Using default response p
分区号 (2-4,默认 2): #回车
起始 扇区 (20973568-167772159,默认为 20973568): #回车
将使用默认值 20973568
Last 扇区, +扇区 or +size{K,M,G} (20973568-167772159,默认为 167772159):+200M

命令(输入 m 获取帮助):n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): #回车
Using default response p
分区号 (3,4,默认 3): #回车
起始 扇区 (41945088-167772159,默认为 41945088): #回车
将使用默认值 41945088
Last 扇区, +扇区 or +size{K,M,G} (41945088-167772159,默认为 167772159):+100M

命令(输入 m 获取帮助):n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): #回车
Using default response e
已选择分区 4
起始 扇区 (67110912-167772159,默认为 67110912): #回车
将使用默认值 67110912
Last 扇区, +扇区 or +size{K,M,G} (67110912-167772159,默认为 167772159): #回车
将使用默认值 167772159

命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 5
起始 扇区 (67112960-167772159,默认为 67112960): #回车
将使用默认值 67112960
Last 扇区, +扇区 or +size{K,M,G} (67112960-167772159,默认为 167772159):+500M

命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 6
起始 扇区 (67112960-167772159,默认为 67112960): #回车
将使用默认值 67112960
Last 扇区, +扇区 or +size{K,M,G} (67112960-167772159,默认为 167772159):+2000M

命令(输入 m 获取帮助):p
命令(输入 m 获取帮助):w
[root@server0 ~]# lsblk

案例34:发布iSCSI网络磁盘
1)配置 server0 提供 iSCSI 服务,要求如下:
-磁盘名为iqn.2016-02.com.example:server0
-服务端口为 3260
-使用 iscsi_store(后端存储的名称) 作其后端卷,其大小为 3GiB
-此磁盘服务只能被 desktop0.example.com 访问,在Server0上配置客户端ACL为iqn.2016-02.com.example:desktop0
1.安装软件targetcli(服务端软件)
[root@server0 ~]# yum -y install targetcli

2.运行targetcli可以再交互式界面,配置iSCSI共享存储
[root@server0 ~]# targetcli

A:创建与命名后端存储
/> backstores/block create name= iscsi_store dev=/dev/vdb1
后端存储 块设备 创建 命名 具体设备
/> ls

B:创建target,磁盘组
/> iscsi/ create iqn.2016-02.com.example:server0

/> ls

C:lun,逻辑单元
/> iscsi/iqn.2016-02.com.example:server0/tpg1/
luns create /backstores/block/iscsi_store

/> ls

D:配置访问控制,设置访问服务时,客户端声称的名字
/> iscsi/iqn.2016-02.com.example:server0/tpg1/acls
create iqn.2016-02.com.example:desktop0

/> ls

E:客户端访问本机的IP地址及端口
[root@server0 ~]# targetcli
/> iscsi/iqn.2016-02.com.example:server0/tpg1/
portals create 172.25.0.11

/> ls
/> exit 退出

三、重起服务target
[root@server0 ~]# systemctl restart target
[root@server0 ~]# systemctl enable target

2)配置虚拟机 desktop0 使用 server0 提供 iSCSI 服务
一、安装所需软件包iscsi-initiator-utils
yum安装能够补全包名:
1、前提由Yum的缓存(yum repolist)
2、当前系统没有安装的,才可以补全

[root@desktop0 ~]# rpm -q iscsi-initiator-utils

二、修改配置文件,用来指定客户端声称的名字

1.修改配置文件

vim /etc/iscsi/initiatorname.iscsi

InitiatorName=iqn.2016-02.com.example:desktop0

2.需重启服务 iscsid 以更新IQN标识
[root@desktop0 ~]# systemctl restart iscsid
Warning: Unit file of iscsid.service changed on
disk, ‘systemctl daemon-reload’ recommended.
[root@desktop0 ~]# systemctl daemon-reload
[root@desktop0 ~]# systemctl restart iscsid

三、发现服务端共享存储

1.书写发现的命令, 参考# man iscsiadm 搜索全文 /example

Ctrl - :减小字体
Ctrl Shift + :变大字体

iscsiadm --mode discoverydb --type sendtargets –

portal 172.25.0.11 --discover

2.本机识别服务端共享
[root@desktop0 ~]# lsblk
[root@desktop0 ~]# systemctl restart iscsi
[root@desktop0 ~]# lsblk
[root@desktop0 ~]# systemctl enable iscsi

案例35:为虚拟机 server 配置以下虚拟Web主机
实现三个网站的部署
-客户端访问server0.example.com网页内容为 大圣归来
-客户端访问www0.example.com网页内容为 大圣又归来
-客户端访问webapp0.example.com网页内容为 大圣累了
1.建立新的调用配置文件
[root@server0 ~]# vim /etc/httpd/conf.d/nsd01.conf
<VirtualHost *:80> #在所有网卡启用80端口
ServerName www0.example.com #指定网站域名
DocumentRoot /var/www/nsd01 #指定网页文件路径

<VirtualHost *:80>
ServerName webapp0.example.com
DocumentRoot /var/www/nsd02

<VirtualHost *:80>
ServerName server0.example.com
DocumentRoot /var/www/nsd03

[root@server0 ~]# mkdir /var/www/nsd01 /var/www/nsd02 /var/www/nsd03
[root@server0 ~]# echo 大圣归来 > /var/www/nsd01/index.html
[root@server0 ~]# echo 大圣又归来 > /var/www/nsd02/index.html
[root@server0 ~]# echo 大圣累了 > /var/www/nsd03/index.html

[root@server0 ~]# systemctl restart httpd
[root@server0 ~]# firefox www0.example.com
[root@server0 ~]# firefox webapp0.example.com
[root@server0 ~]# firefox server0.example.com

案例36:为虚拟机 server 配置Web访问控制
在 Web 网站 http://server0.example.com 的 DocumentRoot 目录下创建一个名为 private 的子目录,要求如下:
-在server0.example.com的DocumentRoot目录下,private的子目录里书写网页文件index.html内容为 大圣偷偷归来
-此页面只能在本机浏览,但是从其他系统不能访问这个目录的内容
虚拟机Server0
[root@server0 ~]# mkdir /var/www/myweb/private
[root@server0 ~]# echo大圣偷偷归来 > /var/www/myweb/private/index.html

[root@server0 ~]# vim /etc/httpd/conf.d/nsd02.conf
<Directory “/var/www/myweb/private”>
Require ip 172.25.0.11

[root@server0 ~]# systemctl restart httpd

客户端虚拟机Desktop:访问测试
[root@desktop0 ~]# firefox server0.example.com/private
网页报错信息:
Forbidden
You don’t have permission to access /private/ on this server.

案例37:虚拟机 server 使用自定Web根目录
调整 Web 站点 http://server0.example.com 的网页目录,要求如下:
-新建目录 /webroot,作为此站点新的网页目录
-确保站点 http://server0.example.com 仍然可访问
虚拟机Server0:
1.新建目录 /webroot,作为此站点新的网页目录
[root@server0 ~]# mkdir /webroot
[root@server0 ~]# echo wo shi webroot > /webroot/index.html

2.修改配置文件/etc/httpd/conf.d/nsd01.conf
<VirtualHost *:80>
ServerName www0.example.com
DocumentRoot /var/www/nsd01

<VirtualHost *:80>
ServerName webapp0.example.com
DocumentRoot /var/www/nsd02

<VirtualHost *:80>
ServerName server0.example.com
DocumentRoot /webroot

3.修改配置文件/etc/httpd/conf.d/nsd02.conf(访问控制)
<Directory “/var/www/myweb/private”>
Require ip 172.25.0.11

<Directory “/webroot”>
Require all granted

4.SELinux安全上下文(标签值)
• 参照标准目录,重设新目录的属性
– chcon [-R] --reference=模板目录 新目录
[root@server0 ~]# ls -Zd /webroot/
[root@server0 ~]# chcon -R --reference=/var/www /webroot/
[root@server0 ~]# ls -Zd /webroot/

5.重起服务
[root@server0 ~]# systemctl restart httpd

客户端desktop:访问测试
[root@server0 ~]# firefox server0.example.com

案例38:为虚拟机 server 部署动态WSGI站点
为站点 webapp0.example.com 配置提供动态Web内容,要求如下:
-此虚拟主机侦听在端口8909
-测试网页从以下地址下载,不要作任何更改
http://classroom/pub/materials/webinfo.wsgi
-从浏览器访问 http://webapp0.example.com:8909 可接收到动态生成的 Web 页面
1.部署Python的页面
[root@server0 ~]# vim /etc/httpd/conf.d/nsd01.conf
查看webapp0.example.com的DocumentRoot路径

cd /var/www/nsd02/

wget http://classroom.example.com/pub/materials/webinfo.wsgi

2.方便用户访问,网页跳转(网页别名)
[root@server0 /]# vim /etc/httpd/conf.d/nsd01.conf

<VirtualHost *:80>
ServerName webapp0.example.com
DocumentRoot /var/www/nsd02
Alias / /var/www/nsd02/webinfo.wsgi

#当用户访问网页文件根目录时,将webinfo.wsgi呈现

...... [root@server0 /]# systemctl restart httpd

[root@server0 /]# firefox webapp0.example.com

3.翻译Python页面
[root@server0 /]# yum -y install mod_wsgi
[root@server0 /]# vim /etc/httpd/conf.d/nsd01.conf
<VirtualHost *:80>
ServerName webapp0.example.com
DocumentRoot /var/www/nsd02
WsgiScriptAlias / /var/www/nsd02/webinfo.wsgi

[root@server0 /]# systemctl restart httpd [root@server0 /]# firefox webapp0.example.com

UNIX时间戳:自1970-1-1 0:0:0算起,到达当前时间所经历的秒数

4.修改虚拟Web主机的端口
[root@server0 /]# vim /etc/httpd/conf.d/nsd01.conf
<VirtualHost *:80>
ServerName www0.example.com
DocumentRoot /var/www/nsd01

Listen 8909
<VirtualHost *:8909>
ServerName webapp0.example.com
DocumentRoot /var/www/nsd02
WsgiScriptAlias / /var/www/nsd02/webinfo.wsgi

<VirtualHost *:80>
ServerName server0.example.com
DocumentRoot /var/www/nsd03

5.修改SELinux开放的端口,-a添加 -t 类型 -p 协议

semanage port -l | grep http

semanage port -a -t http_port_t -p tcp 8909

semanage port -l | grep http #查看是否添加成功

systemctl restart httpd

[root@desktop0 ~]# firefox webapp0.example.com:8909

案例39:配置安全Web服务
为站点 http://server0.example.com 配置TLS加密
一个已签名证书从以下地址获取 http://classroom/pub/tls/certs/server0.crt
此证书的密钥从以下地址获取 http://classroom/pub/tls/private/server0.key
此证书的签名授权信息从以下地址获取 http://classroom/pub/example-ca.crt
虚拟机Server0:
1.部署网站证书(营业执照)
[root@server0 /]# cd /etc/pki/tls/certs/
[root@server0 certs]# wget http://classroom.example.com/pub/tls/certs/server0.crt
[root@server0 certs]# ls

2.部署数字证书授权中心信息(根证书)(*局信息)
[root@server0 /]# cd /etc/pki/tls/certs/
[root@server0 certs]# wget http://classroom.example.com/pub/example-ca.crt
[root@server0 certs]# ls

3.部署私钥文件,用于解密数据
[root@server0 certs]# cd /etc/pki/tls/private/
[root@server0 private]# wget http://classroom.example.com/pub/tls/private/server0.key
[root@server0 certs]# ls

4.安装支持安全Web的软件
[root@server0 /]# yum -y install mod_ssl

5.修改配置文件
[root@server0 /]# echo security example > /var/www/html/index.html
[root@server0 /]# vim /etc/httpd/conf.d/ssl.conf
补充:vim末行模式下 输入 : set nu 显示行号

59 DocumentRoot “/var/www/html”
60 ServerName server0.example.com:443

指定网站证书
100 SSLCertificateFile /etc/pki/tls/certs/server0.crt

指定私钥文件
107 SSLCertificateKeyFile /etc/pki/tls/private/server0.key

指定根证书(数字授权中心信息)
122 SSLCACertificateFile /etc/pki/tls/certs/example-ca.crt

6.重起 httpd 服务
[root@server0 /]# systemctl restart httpd
7.验证
[root@desktop0 ~]# firefox https://www0.example.com

案例40:虚拟机 server0上操作,(GPT分区模式)规划分区
关闭虚拟机,图形添加一块60G的硬盘并规划分区:
-划分4个10G的主分区
-划分1个12G的主分区
[root@server0 ~]# lsblk

[root@server0 ~]# parted /dev/sdb

(parted) mktable gpt #设置为GPT分区模式
(parted) print #显示分区表信息

(parted) mkpart
分区名称? []? nsd
文件系统类型? [ext2]? ext4
起始点? 0
结束点? 10G
警告: The resulting partition is not properly aligned for best performance.
忽略/Ignore/放弃/Cancel? i #选择忽略
(parted) print

Number Start End Size File system Name 标志
1 17.4kB 10.0GB 10000MB nsd

(parted) mkpart nsd ext4 10G 20G
(parted) mkpart nsd ext4 20G 30G
(parted) mkpart nsd ext4 30G 40G
(parted) mkpart nsd ext4 40G 52G
(parted) print
Number Start End Size File system Name 标志
1 17.4kB 10.0GB 10000MB nsd
2 10.0GB 20.0GB 9999MB nsd
3 20.0GB 30.0GB 10.0GB nsd
4 30.0GB 40.0GB 10.0GB nsd
5 40.0GB 52.0GB 12.0GB nsd

案例41:虚拟机 server0上操作,交换分区练习
利用案例2划分的分区,制作10G的交换分区,此交换分区开机自启动
1.格式化交换文件系统
[root@server0 ~]# mkswap /dev/vdc1 #格式化交换文件系统
[root@server0 ~]# blkid /dev/vdc1 #查看文件系统类型
2.启用交换分区
[root@server0 ~]# swapon /dev/vdc1 #启用交换分区
[root@server0 ~]# swapon -s #查看交换空间信息
3.停用交换分区
[root@server0 ~]# swapoff /dev/vdc1
[root@server0 ~]# swapon -s
4.开机自动启用交换分区
[root@server0 ~]# vim /etc/fstab

/dev/vdc1 swap swap defaults 0 0

[root@server0 ~]# swapon -s
[root@server0 ~]# swapon -a #专用于检测swap分区开机自动启动
[root@server0 ~]# swapon -s

上一篇:配置SMB文件夹共享 多用户Samba挂载 普通NFS共享的实现


下一篇:Swapon交换空间: 缓解真实物理内存的压力