Linux模拟考试

注意,以下答案仅供参考

1、某CentOS系统空间不够,现加一块100G的硬盘(是系统的第二块硬盘),分为一个区99G,挂载点是/data,请写出从分区到挂载并使用的整个步骤及相关命令。

1.创建分区:

sudo fdisk /dev/sdb

2.在fdisk的交互式界面中,执行以下步骤:

输入n创建新分区。

选择p为主分区。

按提示输入分区编号(通常为1)。

直接按回车接受默认的起始扇区。

输入+99G来指定分区大小。

输入w保存并退出。

3.写分区表

partprobe

4.格式化分区:

sudo mkfs -t ext4 /dev/sdb1

5.创建挂载点:

sudo mkdir /data

6.挂载分区:

sudo mount /dev/sdb1 /data

2、创建一个主目录为/home/boss的用户boss,并为该用户设置密码为"123456"。

sudo useradd -d /home/boss -m boss
sudo echo 123456|passwd --stdin boss

3、编写一个shell脚本,从键盘读入一个成绩,并按优秀、良好、中等、及格、不及格输出成绩。

#/bin/bash
read -p "请输入学生成绩(0-100):" score
if [ $sum -gt 100 ] ;then
    echo "输入有误,成绩大于100"
elif [ $sum -ge 90 ] ;then
    echo "优秀"
elif [ $sum -ge 80 ] ;then
    echo "良好"
elif [ $sum -ge 70 ] ;then
    echo "中等"
elif [ $sum -ge 60 ] ;then
    echo "及格"
elif [ $sum -ge 0 ] ;then
    echo "不及格"
elif [ $sum -lt 0 ] ;then
    echo "输入有误,成绩小于0"
fi

4、某公司开发部下载了linux内核源代码压缩包linux-4.10.1.tar.xz,写出从解压压缩包到编译新内核的过程和相应命令。

1.解压内核源代码:

xz -d linux-4.10.1.tar.xz

tar -xvf linux-4.10.1.tar

2.进入源代码目录:

cd linux-4.10.1

3.配置内核: 使用菜单驱动的配置工具:

make menuconfig

4.编译内核: 使用以下命令编译内核:

make -j $(nproc)

注释:-j $(nproc)参数允许make使用所有可用的CPU核心来加速编译过程。

5.编译内核模块:

sudo make modules

6.安装内核模块:

sudo make modules_install

7.安装内核:

sudo make install

8.更新引导加载程序:

sudo update-grub

9.重启系统并选择新编译的内核版本以启动

reboot

5、有一个进程loop出现了死循环,且按ctrl+c也无法终止,现要得到其进程号,请写出得到进程号的命令。

1.使用ps命令: ps aux | grep loop

2.使用top命令: 运行top命令,然后使用方向键或者f键来选择排序和显示的列,或者直接在top界面中查找loop进程。

3.使用pgrep或pidof命令:

pgrep loop

或者

pidof loop

6、有一个IP地址文件ip.txt,内容如下:

10.22.110.1

10.22.110.10

10.22.110.30

10.22.110.20

10.22.110.40

10.22.110.50

10.22.110.60

请写一个shell程序,统计出有多少台主机可ping通,多少台主机不可Ping通。

#!/bin/bash
n1=0
n2=0
while read line
do
  ping $line -c 3
  if [ $? -eq 0 ]
  then
    let n1++
  else
    let n2++
  fi
done<ip.txt
echo "there are $n1 machine reachable"
echo "there are $n2 machine unreachable"

7、请简要描述配置自定义网站目录(/home/wwwroot)的网站服务的步骤。包括创建目录和SELinux配置.

1.下载httpd(yum install httpd)2、测试默认网页 用linux下的firefox访问127.0.0.1(systemctl start httpd)

2.设置自己的网站目录,编写自己的首页

mkdir -p /home/wwwroot

echo “The New Web Directory”>/home/wwwroot/index.html

3.修改httpd配置文件

vi /etc/httpd/conf/httpd.conf

DocumentRoot /home/wwwroot

<Directory “/home/wwwroot”>

4.查看SELinux的安全上下文:

ls -Zd /var/www/html (默认的SELinux认可的httpd目录)

ls -Zd /home/wwwroot (用户修改的httpd目录)

5.设置目录/home/wwwroot及目录下文件的安全上下文为httpd_sys_content_t:

semanage fcontext -a -t httpd_sys_content_t /home/wwwroot

semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*

restorecon -Rv /home/wwwroot

6.配置个人用户主页功能

(1)修改配置:

vi /etc/httpd/conf.d/userdir.conf

<ifModule mod_userdir.c>

UserDir public_html

</IfModule>

<Directory “/home/*/public_html”>

AllowOverride FileInfo AuthConfig Limit Indexes

Options MultiVIews Indexes SymLinksIfOwnerMatch IncludesNoExec

Require method Get POST OPTIONS

</Directory>

(2)创建个人主页目录及首页

useradd -d /home/boss -m boss

echo “123456”|passwd --stdin boss

su - boss

mkdir public_html

echo “This is boss’s website”>public_html/index.html

chmod -Rf 0755 /home/boss

(3)设置访问个人主页所需要的SELinux文件安全上下文:

i)查看:getsebool -a|grep http

httpd_enable_homedirs–>off

ii)设置SELinux安全上下文:

setsebool -P httpd_enable_homedirs=on

(4)访问个人主页

127.0.0.1/~boss

(5)为个人主页设置访问密码

i)生成密码数据库

htpasswd -c /etc/httpd/passwd boss

New password:xxxx(输入用于个人主页验证的密码)

再输入一遍

ii)修改个人主页配置

vi /etc/httpd/conf.d/userdir.conf

<Directory "/home/*/public_html>

AllowOverride all

authuserfile “/etc/httpd/passwd” #(密码库目录)

authname “My privately website”

authtype basic

require user boss

抓包:tcpdump -i ens33 -s 0 ‘tcp and port 80’

查看端口:netstat -alnp|grep tcp

打开防火墙:firewall-config

7.测试网站

8、请用shell程序写出求1~100所有素数的和。

#!/bin/bash
for((i=1;i<=100;i++))
do
   if((i<=2))
   then
       if((i==2))
       then
           let sum+=i
       fi
   else
      flag=0
      for((j=2;j<=i/2;j++))
      do
          if((i%j==0))
          then
               flag=1
               break
          fi
      done
      if((flag==0))
      then
          let sum+=i
      fi
   fi
done
echo "all prime(1~100) sum is $sum"

9、一个机房管理中心管理着各类服务器,由于服务器分散部署在不同的地方,中心采用了ansible方式对各服务器进行管理。现在有两台web服务器,其IP地址分别为172.16.30.10和172.16.30.20。现要求在这两台web服务器上部署ngix,要求在管理中心主控机上用ansible编写playbook脚本完成这个任务。这个任务分为以下几个步骤:(1)下发nginx.repo文件到/etc/yum.repos.d/nginx.conf;(2)安装nginx;(3)下发nginx配置文件到/etc/nginx/nginx.conf;(4)下发网站主页index.html到/usr/share/nginx/html;(5)启动nginx.其中nginx.repo.j2和nginx.conf.j2以及index.html都存放在主机机的/srv/目录下。要求用YAML语法写出这个playbook脚本,脚本文件名为nginx.yml。

1.编辑文件hosts vi /etc/ansible/hosts

添加如下代码

[web_servers]
172.16.30.10
172.16.30.20

2.编写脚本 nginx.yml,内容如下

---
- name: Deploy Nginx on web servers
  hosts: web_servers
  become: yes

  tasks:
    - name: Copy nginx repository file
      copy:
        src: /srv/nginx.repo.j2
        dest: /etc/yum.repos.d/nginx.repo
        owner: root
        group: root
        mode: 0644

    - name: Install Nginx
      yum:
        name: nginx
        state: present

    - name: Copy Nginx configuration file
      template:
        src: /srv/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
        owner: root
        group: root
        mode: 0644
      notify:
        - restart nginx

    - name: Copy index.html to Nginx root
      copy:
        src: /srv/index.html
        dest: /usr/share/nginx/html/index.html
        owner: root
        group: root
        mode: 0644

    - name: Start Nginx
      service:
        name: nginx
        state: started
        enabled: yes

  handlers:
    - name: restart nginx
      service:
        name: nginx
        state: restarted

10、用户cs需要从/root目录复制目录/root/tools,并将该目录下的所有子目录和文件的所用者都改为用户cs,写出由root用户完成该任务的操作命令。

1.复制目录: 将/root/tools目录复制到/home/cs目录下:

cp -R /root/tools /home/cs/

2.更改所有者:

将复制后的目录及其所有子目录和文件的所有者改为用户cs:

chown -R cs:cs /home/cs/tools

  • 27
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qing影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值