系统相关
首页 > 系统相关> > linux系统服务管理1-9

linux系统服务管理1-9

作者:互联网

案例01:补充命令使用
1)采用数值形式将目录/root的权限调整为 rwx------
1.查看原来的权限
[root@svr7 ~]# ls -ld /root/
dr-xr-x—. 22 root root 4096 3月 26 14:59 /root/
2.修改为新权限
[root@svr7 ~]# chmod 700 /root/
3.确认权限设置结果
[root@svr7 ~]# ls -ld /root/
drwx------. 22 root root 4096 3月 26 14:59 /root/
2)将记录的历史命令条数更改为 200 条
1.调整记录条数
修改配置文件/etc/profile,找到HISTSIZE行,将此变量的值修改为200:
[root@svr7 ~]# vim /etc/profile
… …
HISTSIZE = 200
2.确认设置结果
所有用户重新登录以后即可生效:
[root@svr7 ~]# su - root
[root@svr7 ~]# echo $HISTSIZE
200
3)统计 /boot、/etc/pki 目录占用的空间大小
1.分别统计结果
[root@svr7 ~]# du -sh /boot/ /etc/pki/
130M /boot/
1.5M /etc/pki/
2.比较du与ls查看文件大小的差异(默认块大小4096字节):
[root@svr7 ~]# ls -lh /etc/inittab //数据大小511字节
-rw-r–r--. 1 root root 511 Sep 16 2015 /etc/inittab
[root@svr7 ~]# du -sh /etc/inittab //实际占用4KB磁盘空间
4.0K /etc/inittab
4)以格式“yyyy-mm-dd HH:MM” 显示系统时间
[root@svr7 ~]# date +"%F %R"
2026-12-26 16:23

案例02:软连接与硬连接
1)新建文件 file1,内容为 linux.tedu.cn
2)为 file1 建立软连接 file1-s,对比两文件内容
3)为 file1 建立硬连接 file1-h,对比两文件内容
4)删除文件 file1 ,再查看文件 file1-s、file1-h 内容
1.新建一个测试文件
[root@svr7 ~]# vim /root/file1
AAAA
2.为文件file1建立软连接file1-s并测试
[root@svr7 ~]# ln -s /root/file1 /root/file1-s
[root@svr7 ~]# cat /root/file1-s
linux.tedu.cn
3.为文件file1建立硬连接file1-h并测试
[root@svr7 ~]# ln /root/file1 /root/file1-h
[root@svr7 ~]# cat /root/file1-h
linux.tedu.cn
4.对比原始文件、软连接、硬连接的属性
[root@svr7 ~]# ls -l /root/f*
-rw-r–r--. 2 root root 14 Jan 6 12:14 file1-h
lrwxrwxrwx. 1 root root 12 Jan 6 12:16 file1-s -> file1
-rw-r–r--. 2 root root 14 Jan 6 12:14 file1
步骤二:原始文件删除测试
1.当原始文件被删除时,软连接将会失效,而硬连接仍然可访问文件数据
[root@svr7 ~]# rm -rf file1
[root@svr7 ~]# cat /root/file1-s
cat: file1-s: No such file or directory
[root@svr7 ~]# cat /root/file1-h
linux.tedu.cn
2.不支持为目录创建硬连接,但可以为目录建立软连接
[root@svr7 ~]# ln /etc/sysconfig/network-scripts/ /etc/network
ln: ‘/etc/sysconfig/network-scripts/’: hard link not allowed for directory
[root@svr7 ~]# ln -s /etc/sysconfig/network-scripts/ /etc/interface
[root@svr7 ~]# ls -l /etc/interface
lrwxrwxrwx. 1 root root 31 Jan 6 12:28 /etc/interface -> /etc/sysconfig/network-scripts/

案例03:man手册、zip备份
1)查阅passwd命令、/etc/passwd配置文件的手册页
1.查看passwd命令的手册页
[root@svr7 ~]# man passwd
PASSWD(1) User utilities PASSWD(1)

NAME
passwd - update user’s authentication tokens

SYNOPSIS
passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w
warndays] [-i inactivedays] [-S] [–stdin] [username]

DESCRIPTION
The passwd utility is used to update user’s authentication token(s).

   This task is achieved through calls to the Linux-PAM and Libuser API.
   Essentially,  it initializes itself as a "passwd" service with Linux-
   PAM and utilizes configured password modules to authenticate and then
   update a user's password.

… …
2.查看/etc/passwd配置文件的手册页
[root@svr7 ~]# man 5 passwd
PASSWD(5) Linux Programmer’s Manual PASSWD(5)

NAME
passwd - password file

DESCRIPTION
The /etc/passwd file is a text file that describes user login
accounts for the system. It should have read permission allowed for
all users (many utilities, like ls(1) use it to map user IDs to user‐
names), but write access only for the superuser.

   In the good old days there was no great  problem  with  this  general
   read  permission.   Everybody could read the encrypted passwords, but
   the hardware was too slow to crack a well-chosen password, and  more‐
   over  the  basic assumption used to be that of a friendly user-commu‐
   nity.  These days many people run some version of the shadow password
   suite,  where /etc/passwd has an 'x' character in the password

… …

2)使用zip打包/usr/share/doc/qemu-kvm/目录
1.将目录/usr/share/doc/qemu-kvm/备份为/root/qemu-kvm.zip
[root@svr7 ~]# zip -r /root/qemu-kvm.zip /usr/share/doc/qemu-kvm/
adding: usr/share/doc/qemu-kvm/ (stored 0%)
adding: usr/share/doc/qemu-kvm/COPYING (deflated 62%)
adding: usr/share/doc/qemu-kvm/COPYING.LIB (deflated 65%)
adding: usr/share/doc/qemu-kvm/Changelog (deflated 61%)
adding: usr/share/doc/qemu-kvm/LICENSE (deflated 45%)
adding: usr/share/doc/qemu-kvm/README (deflated 4%)
[root@svr7 ~]# ls /root

案例04:虚拟机svr7中,自定义yum软件仓库
1)虚拟机svr7创建目录 /myrpm
2)tools.tar.gz中的RPM包,复制到/myrpm目录下
3)利用/myrpm目录的RPM包进行搭建自定义Yum仓库

  1. 真机的数据传递到虚拟机svr7中
    [root@room9pc01 ~]# scp /root/桌面/tools.tar.gz root@192.168.4.7:/root/

2.在虚拟机svr7中验证
[root@svr7 ~]# ls /root

3.进行tar解包
[root@svr7 ~]# mkdir /myrpm

[root@svr7 ~]# tar -xf /root/tools.tar.gz -C /myrpm
[root@svr7 ~]# ls /myrpm/tools/
[root@svr7 ~]# ls /myrpm/tools/other/

4.生成仓库数据文件
[root@svr7 ~]# createrepo /myrpm/tools/other/
[root@svr7 ~]# ls /myrpm/tools/other/

5.书写yum客户端配置文件
[root@svr7 ~]# vim /etc/yum.repos.d/rhel7.repo

[myrpm]
name=myrpm
baseurl=file:///myrpm/tools/other/ #指定本地路径
enabled=1
gpgcheck=0

[root@svr7 ~]# yum repolist

案例05:vim效率操作
1)将文件 /etc/passwd 复制为 /opt/nsd.txt,然后打开 /opt/nsd.txt 文件,练习命令模式下的切换/复制/删除/查找操作
步骤一:vim命令模式下的切换/复制/删除/查找
1.建立练习文件
将文件 /etc/passwd 复制为 /opt/nsd.txt:
[root@svr7 ~]# cp /etc/passwd /opt/nsd.txt
2.使用vim打开练习文件,默认处于命令模式
[root@svr7 ~]# vim /opt/nsd.txt
… …
3.在命令模式下完成下列操作
切换操作:G 最后一行,5G 第5行,gg 第一行。
复制操作:按2yy复制2行,7G移动到第7行,p 粘贴。
删除操作:25G 移动到第25行,200dd 从此行开始删除200行(不够就剩下全删)。
查找操作:gg 第一行,/adm 查找关键词adm,n 跳转到下一个结果。
4.保存并退出编辑器
ZZ 保存退出。
2)将文件 /etc/man_db.conf 复制到 /opt 目录下,然后打开 /opt/man_db.conf 文件,将第50~100行内的“man”替换为“MAN”,在 vim 中设置显示行号查看效果
步骤二:vim末行模式下的替换/设置操作
1.建立练习文件
将文件 /etc/man_db.conf 复制到 /opt/ 目录下:
[root@svr7 ~]# cp /etc/man_db.conf /opt/
2.使用vim打开练习文件,输入:切换到末行模式
[root@svr7 ~]# vim /opt/man_db.conf
… …
:
3.在末行模式下完成下列操作
输入 :set nu ,确认后显示行号。
输入 :50,100 s/man/MAN/g ,确认将第50~100行内的“man”替换为“MAN”。
4.保存并退出编辑器
输入 :wq ,确认后保存并退出编辑器。

案例06:搭建单区域DNS服务器
1)在虚拟机svr7上,搭建DNS服务器,可以解析以下域名
pc207.tedu.cn —> 192.168.4.207
www.tedu.cn —> 192.168.4.100
1.安装软件包
[root@svr7 ~]# yum -y install bind-chroot bind
bind-9.9.4-29.el7.x86_64 //域名服务包
bind-chroot-9.9.4-29.el7.x86_64 //提供虚拟根支持

2.修改配置文件/etc/named.conf
[root@svr7 ~]# cp /etc/named.conf /root/named.bak
[root@svr7 ~]# vim /etc/named.conf
options {
directory “/var/named”; #指定地址库文件路径
};
zone “tedu.cn” IN { #指定本机负责解析的域名
type master; #本机为权威主DNS服务器
file “tedu.cn.zone”; #指定地址库文件名字
};

3.创建地址库文件/var/named/tedu.cn.zone
[root@svr7 ~]# cd /var/named
[root@svr7 ~]# cp -p named.localhost tedu.cn.zone #保持属性不变
[root@svr7 ~]# ls -l tedu.cn.zone

[root@svr7 ~]# vim tedu.cn.zone #没有以点结尾,默认补全tedu.cn.
………
tedu.cn. NS svr7 #tedu.cn.区域有svr7负责
svr7 A 192.168.4.7 #svr7的IP地址为192.168.4.7
www A 192.168.4.100
pc207 A 192.168.4.207

4.重起named服务
[root@svr7 ~]# systemctl restart named

客户端验证:
1.指定DNS服务器位置
[root@pc207 ~]# echo nameserver 192.168.4.7 > /etc/resolv.conf
2.域名解析测试
[root@pc207 ~]# nslookup www.tedu.cn
[root@pc207 ~]# nslookup pc207.tedu.cn

案例07:特殊DNS解析
1)为站点 www.tedu.cn 提供DNS轮询解析,三台Web服务器节点的IP地址分别为:192.168.4.100、192.168.4.110、192.168.4.120
1.修改DNS服务器上tedu.cn区域的地址库文件,在末尾添加轮询地址记录
[root@svr7 ~]# vim /var/named/tedu.cn.zone
… …
www A 192.168.4.100
www A 192.168.4.110
www A 192.168.4.120
2.重启系统服务named
[root@svr7 named]# systemctl restart named
3.在客户机pc207上测试轮询记录
针对目标www.tedu.cn执行多次查询,观察第1条结果的变化:
[root@pc207 ~]# host www.tedu.cn
www.tedu.cn has address 192.168.4.100 //第1个结果为192.168.4.100
www.tedu.cn has address 192.168.4.110
www.tedu.cn has address 192.168.4.120

[root@pc207 ~]# host www.tedu.cn
www.tedu.cn has address 192.168.4.120 //第1个结果为192.168.4.120
www.tedu.cn has address 192.168.4.110
www.tedu.cn has address 192.168.4.100

[root@pc207 ~]# host www.tedu.cn
www.tedu.cn has address 192.168.4.110 //第1个结果为192.168.4.110
www.tedu.cn has address 192.168.4.120
www.tedu.cn has address 192.168.4.100

2)配置泛域名解析实现以下解析记录:任意名称.tedu.cn —> 119.75.217.56
1.修改DNS服务器上指定区域的地址库文件,在末尾添加*通配地址记录
[root@svr7 ~]# vim /var/named/tedu.cn.zone
… …

2.重启系统服务named
[root@svr7 named]# systemctl restart named
3.在客户机pc207上测试多对一的泛域名解析记录
当查询未知站点(地址库中没有明确记录)时,以 * 对应的IP地址反馈:
[root@pc207 ~]# host station123.tedu.cn
station123.tedu.cn has address 119.75.217.56
[root@pc207 ~]# host movie.tedu.cn
movie.tedu.cn has address 119.75.217.56
[root@pc207 ~]# host tts8.tedu.cn
tts8.tedu.cn has address 119.75.217.56

3)利用解析记录的别名,实现tts.tedu.cn记录解析结果与pc207.tedu.cn结果一致
1.修改DNS服务器上指定区域的地址库文件
[root@svr7 ~]# vim /var/named/tedu.cn.zone
… …
tts CNAME pc207
2.重启系统服务named
[root@svr7 named]# systemctl restart named
3.在客户机pc207上测试
[root@pc207 ~]# nslookup tts.tedu.cn

案例08:配置DNS子域授权
1)构建父DNS(tedu.cn)服务器
2)构建子DNS(bj.tedu.cn)服务器
3)在父DNS上配置子域授权
4)测试子域授权查询
虚拟机pc207
1.安装软件包
[root@pc207 ~]# yum -y install bind-chroot bind
2.修改配置文件/etc/named.conf
[root@pc207 ~]# cp /etc/named.conf /root/named.bak
[root@pc207 ~]# vim /etc/named.conf
options {
directory “/var/named”; #指定地址库文件路径
};
zone “bj.qq.com” IN { #指定本机负责解析的域名
type master; #本机为权威主DNS服务器
file “bj.qq.com.zone”; #指定地址库文件名字
};
3.创建地址库文件/var/named/bj.qq.com.zone
[root@pc207 ~]# cd /var/named
[root@pc207 ~]# cp –p named.localhost bj.qq.com.zone
[root@pc207 ~]# vim bj.qq.com.zone
bj.qq.com. NS pc207
pc207 A 192.168.4.207
www A 5.5.5.5
4.重起named服务
[root@pc207 ~]# systemctl restart named

子域授权:通过父域的DNS服务器为虚拟机svr7,能够解析www.bj.qq.com
虚拟机svr7:
[root@svr7 /]# vim /var/named/qq.com.zone
qq.com. NS svr7
bj.qq.com. NS pc207
svr7 A 192.168.4.7
pc207 A 192.168.4.207
………
[root@svr7 /]# systemctl restart named
[root@svr7 /]# nslookup www.bj.qq.com 192.168.4.7
Server: 192.168.4.7
Address: 192.168.4.7#53

Non-authoritative answer: #非权威解答
Name: www.bj.qq.com
Address: 5.5.5.5

案例09:搭建并测试缓存DNS
利用真机安装 bind、bind-chroot 包,搭建并测试基于全局转发器的缓存DNS
真机搭建缓存DNS服务器
1.搭建Yum仓库
[root@room9pc01 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@room9pc01 ~]# ls /iso/
CentOS-7-x86_64-DVD-1708.iso
[root@room9pc01 ~]# mkdir /dvd/
[root@room9pc01 ~]# mount /iso/CentOS-7-x86_64-DVD-1708.iso /dvd/

[root@room9pc01 ~]# ls /dvd/
[root@room9pc01 ~]# cd /etc/yum.repos.d/
[root@room9pc01 ~]# mkdir repo
[root@room9pc01 ~]# mv *.repo repo
[root@room9pc01 ~]# vim dvd.repo
[dvd]
name=CentOS7.4
baseurl=file:///dvd #指定本地Yum
enabled=1
gpgcheck=0

[root@room9pc01 ~]# yum -y install bind bind-chroot

2.查看达内的DNS服务器地址
[root@room9pc01 /]# cat /etc/resolv.conf
nameserver 172.40.1.10
[root@room9pc01 /]#

3.修改DNS服务器主配置文件,指定转发给内网DNS服务器
[root@room9pc01 /]# vim /etc/named.conf
options {
directory “/var/named”;
forwarders { 172.40.1.10; };
};

  1. 重起named服务
    [root@room9pc01 /]# systemctl restart named

5.在虚拟机上验证:
[root@svr7 /]# nslookup www.taobao.com 192.168.4.254

标签:named,服务,cn,root,系统,svr7,etc,linux,tedu
来源: https://blog.csdn.net/HYFarashi/article/details/115217460