RH254-RHEL7学习笔记

1 Controlling Services and Daemons

systemctl 管理两种模块,.service,.socket,模块有8种状态,loaded active(running) active(exited) active(waiting) inactive enabled disabled static.

不只是两种模块,还包括.target .device .mount .automount .path .scope .slice .swap .timer
示例代码

1
2
3
4
5
6
7
8
9
10
systemctl status sshd.service #查看服务状态,-l,详细日志。
ps -up pid #查看进程资源使用
systemctl --type=service #查看启动的服务, --all 所有安装了的服务
systemctl is-active sshd
systemctl is-enabled sshd
systemctl list-units --type=service #查看所有加载的单元,--all,显示不活动的。
systemctl list-unit-files --type=service #显示所有的units,所有状态(disabled,enabled,static-静态的,需要其他uinit调用启动)
systemctl --failed --type=service #查看启动失败的服务
systemctl mask UNIT #完全禁止开机启动或手动开启某服务 unmask 解除
systemctl list-dependencies UNIT #查看服务单元依赖 --reverse 查看启动此服务需要先启动哪些服务。

.target表示组件应该达到某种状态。

1
2
systemctl get-default #查看目前状态
tree /etc/systemd/system/multi-user.target.wants #查看multi-user模式下启动的服务。

2 Managing IPv6 Networking

做物理机网卡绑定需要此章内容,相关命令nmcil teamdctl teamd,需要使用NetworkManager。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
nmcli con add type team con-name team0 ifname team0 config '{"runner":{"name":"activebackup"}}'
nmcli con mod team0 ipv4.addresses '192.168.0.100/24'
nmcli con mod team0 ipv4.method manual
nmcli con add type team-slave con-name team0-port1 ifname eno1 master team0
nmcli con add type team-slave con-name team0-port2 ifname eno2 master team0
teamdctl team0 state
ping -I team0 192.168.0.254
#验证主备
nmcli dev dis eno1
teamdctl team0 state
ping gw
nmcli con up team0-port1
nmcli dev dis eno2
teamdctl team0 state
ping gw
nmcli con up team0-port2
teamdctl team0 state

4 Network Port Security

5 Managing DNS for Servers

host -v -t A www.baidu.com #查询百度域名ip信息
host -v -t PTR 183.232.231.173
getent hosts example.com
gethostip example.com
dig

6 Configuring Email Transmission

7 Providing Remote Block Storage

8 Providing File-based Storage

9 Configuring MariaDB Databases

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
yum groupinstall mariadb mariadb-client -y
systemctl start mariadb
mysql_secure_installation
MariaDB>create user john@localhost identified by 'john-password' ;#@后面为允许连接地址
MariaDB>grant insert,update,delete,select on inventory.* to john@localhosts;#为用户赋权
MariaDB>flush privileges;
firewall-cmd --permanent --add-service=mysql
firewall-cmd --reload
#数据库逻辑备份,特点1)通过查询数据库获得数据结构2)高适应性,可恢复到不同类型数据库3)备份慢4)数据库必须在线5)备份不包括配置文件和日志文件。
mysqldump -u root -p inventory > /buckup/inventory.dump
#数据库物理备份,特点1)raw拷贝数据库目录和文件2)输出更完整3)备份包括日志和配置文件4)恢复到类似配置硬件和软件上5)速度快6)可离线或锁表备份。
mysqladmin variables | grep datadir
df /var/lib/mysql
vgdisplay vg0 |grep Free
mariadb>flush tables with read lock
copy -r /var/lib/mysql /mysqlback

10 Providing Apache HTTPD Web Service

1
2
3
4
5
yum install httpd httpd-manual
systemctl start httpd.service
systemctl enable httpd.service
firewall-cmd --permanent --add-service=http
firewall-cmd --reload

11 Writing Bash Scripts

1
2
3
4
5
6
for HOST in host1 host2 host3;echo $HOST;done
for HOST in host{1,2,3};do echo $HOST;done
for HOST in host{1..4};do echo $HOST;done
for PACKAGE in $(rpm -qa | grep kernel);do echo "$PACKAGE was installed on $(date -d @$(rpm -q --qf "%{INSTALLTIME}\n" $PACKAGE))";done
seq #产生一个序列,并从大到小打印出来
bash -x ./filesize #执行shell时打印详细步骤 -v类似

12 Bash conditionals and Control Structures

$0 脚本本身 ${1..10},外部变量,$* 变量作为一个word,$@ 输入作为多个变量 $# 变量个数
shell 退出码 保存在$? 中,echo $?查看。退出码在0-255之间,0是正常退出。可在shell中使用“exit 0”设置退出码。

输入测试 [ <比较符> ],[abc==def];echo $?.-z 字符串是否为空 -n 字符串不为空。STRING='';[-z "$STRING"];echo $?,还有文件和目录判断,可判断一个文件是否为0字节,是否可执行。判断的与运算符”&&”,或运算符”||”

条件判断结构 :if/then,if/then/else,if/then/elif/then/else,case in.case语句中的判断条件可以使用|或运算符。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#case example
case "$1" in
start)
start
;;
stop)
rm -f $lockfile
stop
;;
restart)
restart
;;
reload)
reload
;;
status)
status
;;
*)
echo "Usage: $0 (start|stop|restart|reload|status)"
;;
esac

13 Configuring the Shell Environment

vi /etc/profile vi ~/.bashrc

14 Linux Containers and Docker

15 Comprehensive Review