rhce第五周作业

本文详细介绍了如何通过firewalld配置端口转发,让80端口映射到8081,并使用Ansible进行用户创建、脚本执行、文件复制及配置变更,包括shell模块、command模块、copy模块和blockinfile模块的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.firewall-cmd: 设置访问web服务器的80端口时转发到8081端口

进入/etc/httpd/conf.d目录下配置额外的子配置文件

[root@server ~]# cd /etc/httpd/conf.d/

添加配置文件myhost.conf(名字可自取),并进入配置文件进行配置

[root@server conf.d]# vim myhost.conf
listen 8081                                  
<Directory "/www/ip">                
    AllowOverride None                
    Require all granted                
</Directory>

<VirtualHost 192.168.18,129:8081> 
    DocumentRoot "/www/ip"
    ServerName 192.168.18.129
</VirtualHost>

保存并退出

创建首页文件以及他所在的目录
[root@cotenos conf.d]# mkdir -p /www/ip
[root@cotenos conf.d]# vim /www/ip/index.html
This is port 8081

[root@server conf.d]# mkdir -p /www/ip
[root@server conf.d]# vim /www/ip/index.html
This is port 8081

重启httpd服务(注意要关闭SElinux)

查看SELinux状态

[root@server conf.d]# getenforce
Enforcing

此时是开启状态,我们进行修改(注意这里是临时修改,想要永久修改进入配置文件修改)

[root@server conf.d]# setenforce 0
[root@server conf.d]# getenforce
Permissive

重启服务

[root@server conf.d]# systemctl restart httpd

此时并不能访问,这是因为我们的防火墙是开启状态(默认设置是拒绝)
对防火墙进行配置

同意httpd服务的访问

[root@server conf.d]# firewall-cmd --add-service=http 
success

这是临时生效,想要永久生效需添加 --permanent命令

查看所有规则:

[root@server conf.d]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources: 
services: dhcpv6-client http ssh                # 已开启
ports: 
protocols: 
masquerade: no
forward-ports: 
source-ports: 
icmp-blocks: 
rich rules: 

使任何人都可以访问8081端口

[root@server conf.d]# firewall-cmd --permanent --add-prot=8081/tcp
[root@server conf.d]# firewall-cmd --reload

–permanent:永久生效
–reload:让“永久生效”的配置规则立即生效,并覆盖当前的配置规则

将本机80端口转发到192.168.112.35:8081端口上

[root@server conf.d]# firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8081:toaddr=192.168.112.135
success

2.ansible: 使用shell或者command模块创建用户:testuser1 使用script/shell/command模块执行脚本: 脚本内容为:echo “Hello World” 使用copy模块:将管理主机(manage)上 /root/ansible/copyfile 文件拷贝到 被管理主机node1上的/home/testuser1目录下 使用blockinfile模块:向/home/testuser1/copyfile文件中插入:Hello World 在Hello World之后插入: Hello Shaanxi 在Hello World之前插入:Hello China 删除 Hello World 替换 Hello Shaanxi 为 Hello Xian

使用shell或者command模块创建用户:testuser1

[root@server ansible]# ansible localhost -m shell -a "useradd testuser1"
localhost | CHANGED | rc=0 >>

使用script/shell/command模块执行脚本: 脚本内容为:echo “Hello World”

[root@server ansible]# ansible localhost -m shell -a 'echo "Hello world"'
localhost | CHANGED | rc=0 >>
Hello world

使用copy模块:将管理主机(manage)上 /root/ansible/copyfile 文件拷贝到 被管理主机node1上的/home/testuser1目录下

[root@server ansible]# ansible node1 -m copy -a 'src=/root/ansible/copyfile dest=/home/testuser1 '

查看node1主机的/home/test目录:

[root@server ansible]# ansible node1 -m shell -a 'ls -l /home/testuser1'
node1 | CHANGED | rc=0 >>
total 4
-rw-r--r--. 1 root root 13 Nov  14 15:21 copyfile

使用blockinfile模块:向/home/testuser1/copyfile文件中 插入:Hello World 在Hello World之后插入: Hello Shaanxi 在Hello World之前插入:Hello China 删除 Hello World

替换 Hello Shaanxi 为 Hello Xian

[root@server ansible]# ansible node1 -m blockinfile -a 'path=/home/testuser1/copyfile block="Hello World" marker="#{mark} Hello" insertafter="Hello World"'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Block inserted"
}
 
[root@server ansible]# ansible node1 -m blockinfile -a 'path=/home/testuser1/copyfile block="Hello Shaani" marker="#{mark} Shaanxi"'
node1 | CHANGED => {
   "ansible_facts": {
       "discovered_interpreter_python": "/usr/libexec/platform-python"
   },
   "changed": true,
   "msg": "Block inserted"
}
[root@server ansible]# ansible node1 -m blockinfile -a 'path=/home/testuser1/copyfile block="Hello China" marker="#{mark} Shaanxi" insertbefore=BOF'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Block inserted"
}
[root@server ansible]# ansible node1 -m blockinfile -a 'path=/home/testuser1/copyfile block="Hello Xian" marker="#{mark} Shaanxi" insertbefore=BOF'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Block inserted"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值