• 主页
  • 相册
  • 随笔
  • 目录
  • 存档
Total 244
Search AboutMe

  • 主页
  • 相册
  • 随笔
  • 目录
  • 存档

实验:Linux服务器系统管理基础

2020-04-25

1. 基础知识

1.1. 远程管理工具箱

  • tmux

    tmux is a terminal multiplexer for Unix-like operating systems. It allows multiple terminal sessions to be accessed simultaneously in a single window. It is useful for running more than one command-line program at the same time.

    It can also be used to detach processes from their controlling terminals, allowing SSH sessions to remain active without being visible

1.2. 用户/组权限管理

In Linux and Unix, everything is a file. Directories are files, files are files and devices are files. Devices are usually referred to as a node; however, they are still files. All of the files on a system have permissions that allow or prevent others from viewing, modifying or executing. If the file is of type Directory then it restricts different actions than files and device nodes. The super user “root” has the ability to access any file on the system. Each file has access restrictions with permissions, user restrictions with owner/group association. Permissions are referred to as bits.

在Linux和Unix中,一切都是文件。目录是文件,文件是文件,设备也是文件。设备通常被称为节点;但是,它们仍然是文件。系统中的所有文件都有权限,允许或阻止他人查看、修改或执行。如果文件是目录类型的,那么它所限制的操作与文件和设备节点不同。超级用户 “root “有能力访问系统中的任何文件。每个文件都有访问限制与权限,用户限制与所有者/组关联。权限以位形式表现。

1.2.1. ACL(访问控制列表)

What is ACL ? Access control list (ACL) provides an additional, more flexible permission mechanism for file systems. It is designed to assist with UNIX file permissions. ACL allows you to give

1
2
3
4
5
6
7
8
9
10
11
12
# 1
> getfacl test/declarations.h

# file: test/declarations.h
# owner: mandeep
# group: mandeep
user::rw-
group::rw-
other::r--

# 2
setfacl -m u:mandeep:rwx test/declarations.h

type

  • r
    • read(read)
  • w
    • write(edit)
  • x
    • execute(execute)
1
2
3
4
5
6
7
8
9
# 查看当前目录(包含隐藏文件)的长格式。
> ls -la

-rw-r--r-- 1 user staff 651 Oct 12 12:53 .gitmodules

# 第1位如果是d则代表目录,是-则代表普通文件。
# 第2到4位代表当前用户的权限。
# 第5到7位代表组用户的权限。
# 第8到10位代表其他用户的权限。

1.2.2. 用户与组

  • su
    • 用于切换当前用户身份到其他用户身

      变更帐号为root并在执行ls指令后退出变回原使用者:

      su -c ls root

  • sudo
    • sudo命令 用来以其他身份来执行命令,预设的身份为root
    • 若其未经授权的用户企图使用sudo,则会发出警告的邮件给管理员。用户使用sudo时,必须先输入密码,之后有5分钟的有效期限,超过期限则必须重新输入密码。
  • passwd
    • 修改密码
  • adduser/useradd
    • 添加用户
  • groupadd/addgroup
    • 添加组
  • usermod
    • 用于修改用户的基本信息
  • setfacl
    • 用来在命令行里设置ACL(访问控制列表)
  • id
    • 可以显示真实有效的用户ID(UID)和组ID(GID)。
      • UID 是对一个用户的单一身份标识。
      • 组ID(GID)则对应多个UID。
  • chmod
    • 用来变更文件或目录的权限
      • 通过符号组合的方式更改目标文件或目录的权限。
      • 通过八进制数的方式更改目标文件或目录的权限。
      • 通过参考文件的权限来更改目标文件或目录的权限。

        当前用户具有所有权限,组用户有读写权限,其他用户只有读权限。

        chmod u=rwx, g=rw, o=r ./test.log

1.3. 不应用sudo

  • shell内部命令无法sudo
  • 编译/home目录下文件时
  • 源代码编译时

2. 文件系统与存储管理

2.1. 文件系统格式

文件系统描 述
ExtLinux 中最早的文件系统,由于在性能和兼容性上具有很多缺陷,现在已经很少使用
Ext2是 Ext 文件系统的升级版本,Red Hat Linux 7.2 版本以前的系统默认都是 Ext2 文件系统。于 1993 年发布,支持最大 16TB 的分区和最大 2TB 的文件(1TB=1024GB=1024x1024KB)
Ext3是 Ext2 文件系统的升级版本,最大的区别就是带日志功能,以便在系统突然停止时提高文件系统的可靠性。支持最大 16TB 的分区和最大 2TB 的文件
Ext4是 Ext3 文件系统的升级版。Ext4 在性能、伸缩性和可靠性方面进行了大量改进。Ext4 的变化可以说是翻天覆地的,比如向下兼容 Ext3、最大 1EB 文件系统和 16TB 文件、无限数量子目录、Extents 连续数据块 概念、多块分配、延迟分配、持久预分配、快速 FSCK、日志校验、无日志模式、在线碎片整理、inode 增强、默认启用 barrier 等。它是 CentOS 6.3 的默认文件系统

2.2. 文件分区

硬盘是用来存储数据的,可以将其想象成柜子,只不过柜子是用来存储衣物的。新买来的硬盘,通常要对其进行分区并格式化,分区就如同把一个大柜按照要求分割成几个小柜子(组合衣柜);格式化就好比在每个小柜子中打入隔断,决定每个隔断的大小和位置,然后在柜门上贴上标签,标签中写清楚每件衣服保存的隔断的位置和这件衣服的一些特性(比如衣服是谁的,衣服的颜色、大小等)。

  • fdisk

    • 查看磁盘使用情况和磁盘分区
  • df

    • 用于显示磁盘分区上的可使用的磁盘空间。默认显示单位为KB。可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息
      1
      2
      3
      4
      5
      6
      7
      8
      9
      # 查看分区挂载情况:
      [root@localhost ~]# df -h
      文件系统 容量 已用 可用 已用% 挂载点
      /dev/mapper/VolGroup00-LogVol00
      6.7G 2.8G 3.6G 44% /
      /dev/sda1 99M 12M 82M 13% /boot
      tmpfs 125M 0 125M 0% /dev/ shm
      /dev/sdb1 773M 808K 733M 1% / oracle
      /dev/sdb6 1.7G 35M 1.6G 3% /web
  • lvextend

    • 用于在线扩展逻辑卷的空间大小,而不中断应用程序对逻辑卷的访问。使用lvextend命令动态在线扩展磁盘空间,整个空间扩展过程对于应用程序来说是完全透明的。
      • 为了解决增加100M空间: lvextend -L +100M /dev/vg1000/lvol0
  • lvreduce

    • 用于减少LVM逻辑卷占用的空间大小。使用lvreduce命令收缩逻辑卷的空间大小有可能会删除逻辑卷上已有的数据,所以在操作前必须进行确认

2.3. 文件系统挂载

linux 通过 /etc/fstab 配置文件来确定开机挂载硬盘的相关信息,这个配置文件对所有用户可读,但只有 root 用户有权修改此文件

3. 开机自启动项设置

3.1. 守护进程(deamon)

在一个多任务的电脑操作系统中,守护进程是一种在后台执行的电脑程序。此类程序会被以进程的形式初始化。守护进程程序的名称通常以字母“d”结尾:例如,syslogd就是指管理系统日志的守护进程。

通常,守护进程没有任何存在的父进程(即PPID=1),且在UNIX系统进程层级中直接位于init之下。守护进程程序通常通过如下方法使自己成为守护进程:对一个子进程运行fork,然后使其父进程立即终止,使得这个子进程能在init下运行。这种方法通常被称为“脱壳”。

在Windows系统中,由称为Windows服务的应用程序来履行守护进程的职责

3.2. systemd

历史上,Linux 的启动一直采用init进程。这种方法有两个缺点:

  • 启动时间长。init进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
  • 二是启动脚本复杂。init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。

systemd是一套Linux系统的基本构件,它提供了系统和服务管理器,作为PID 1运行。它提供了一个系统和服务管理器,作为PID 1运行,并启动系统的其他部分。

  • sysytemctl
    • 系统服务管理器指令
任务旧指令新指令
使某服务自动启动chkconfig –level 3 httpd onsystemctl enable httpd.service
使某服务不自动启动chkconfig –level 3 httpd offsystemctl disable httpd.service
检查服务状态service httpd statussystemctl status httpd.service (服务详细信息) systemctl is-active httpd.service (仅显示是否 Active)
显示所有已启动的服务chkconfig –listsystemctl list-units –type=service
启动某服务service httpd startsystemctl start httpd.service
停止某服务service httpd stopsystemctl stop httpd.service
重启某服务service httpd restartsystemctl restart httpd.service

4. NetPlan

Netplan is a utility for easily configuring networking on a linux system. You simply create a YAML description of the required network interfaces and what each should be configured to do. From this description Netplan will generate all the necessary configuration for your chosen renderer tool.

Netplan是一个在linux系统上轻松配置网络的工具。你只需创建一个YAML描述,说明所需的网络接口以及每个接口应该做什么配置。从这个描述中,Netplan将为你选择的渲染工具生成所有必要的配置。

4.1. Configure

To configure netplan, save configuration files under /etc/netplan/ with a .yaml extension (e.g. /etc/netplan/config.yaml), then run sudo netplan apply. This command parses and applies the configuration to the system. Configuration written to disk under /etc/netplan/ will persist between reboots.


To let the interface named ‘enp3s0’ get an address via DHCP, create a YAML file with the following:

1
2
3
4
5
6
7
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
dhcp4: true

Netplan currently works with these supported renderers

  • NetworkManager
  • Systemd-networkd

5. 自查

  • 如何添加一个用户并使其具备sudo执行程序的权限?

    • sudo adduser username
  • 如何查看当前系统的分区表和文件系统详细信息?

    • sudo fdisk -l
    • df -ah
  • 如何实现开机自动挂载Virtualbox的共享目录分区?

    • Mount with command
      • sudo mount <name_of_share> /path/to/mountpoint vboxsf <options> 0 0
    • Mount with Virtual Box “automatic mounting”

      In recent releases of Virtual Box we can also automatically mount shared folders on creation:

  • 基于LVM(逻辑分卷管理)的分区如何实现动态扩容和缩减容量?

    • 扩容:sudo lvextend -L +100M /dev/vg1000/lvol0
    • 缩容:sudo lvreduce -L -50M /dev/vg1000/lvol0
  • 如何通过systemd设置实现在网络连通时运行一个指定脚本,在网络断开时运行另一个脚本?

    • sudo vim /lib/systemd/system/NetworkManager-dispatcher.service
      • ExecStartPre=/home/exp/success.sh
      • ExecStartPost=/home/exp/fail.sh
  • 如何通过systemd设置实现一个脚本在任何情况下被杀死之后会立即重新启动?实现杀不死?

    • sudo vim /lib/systemd/system/exp.service
      1
      2
      3
      4
      5
      [Service]
      ExecStart=/usr/bin/exp.sh
      ExecStop=/usr/bin/exp.sh
      Restart=always
      RemainAfterExit=yes

      6. 参考文献

  • Access Control Lists(ACL) in Linux - GeeksforGeeks

  • FilePermissions - Community Help Wiki

  • Linux文件系统详解

  • Systemd 入门教程:命令篇 - 阮一峰的网络日志

  • How to mount a VirtualBox shared folder at startup? - Ask Ubuntu

  • Examples | Netplan

7. 补充

7.1. asciinema

7.1.1. 录制

  • asciinema rec

7.1.2. 回放

  • asciinema play https://asciinema.org/a/132560
  • asciinema play /path/132560.json
  • Lab
  • Operating System
  • Linux
  • Lab
Netplan
风险评估及渗透测试实验
  1. 1. 1. 基础知识
    1. 1.1. 1.1. 远程管理工具箱
    2. 1.2. 1.2. 用户/组权限管理
      1. 1.2.1. 1.2.1. ACL(访问控制列表)
      2. 1.2.2. 1.2.2. 用户与组
    3. 1.3. 1.3. 不应用sudo
  2. 2. 2. 文件系统与存储管理
    1. 2.1. 2.1. 文件系统格式
    2. 2.2. 2.2. 文件分区
    3. 2.3. 2.3. 文件系统挂载
  3. 3. 3. 开机自启动项设置
    1. 3.1. 3.1. 守护进程(deamon)
    2. 3.2. 3.2. systemd
  4. 4. 4. NetPlan
    1. 4.1. 4.1. Configure
  5. 5. 5. 自查
  6. 6. 6. 参考文献
  7. 7. 7. 补充
    1. 7.1. 7.1. asciinema
      1. 7.1.1. 7.1.1. 录制
      2. 7.1.2. 7.1.2. 回放
© 2024 何决云 载入天数...