Linux运维

本文详细介绍了Linux基础,包括systemd、systemctl、hostnamectl等,以及自动化运维工具Ansible的介绍、安装、配置文件、命令和playbook的使用,强调了Ansible在批量主机管理和应用部署中的优势。

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

一.Linux 基础

二.自动化运维工具Ansible

1.自动化运维工具介绍
2.Ansible介绍
3.Ansible安装
4.Ansible配置文件
5.Ansible相关命令
6.主机与组
7.Ansible模块
8.playbook
9.anisble-playbook命令
10.变量
11.条件选择
12.循环
13.roles
14.Ansible部署容器




一.Linux 基础



1.systemd

计算机在启动一个OS时必须加载并初始化OS后才能运行其它任务,即计算机启动需要一款初始化系统。
systemd是目前Linux OS中最流行的系统初始化工具。

系统初始化工具:

  • systemvinit:传统的系统初始化工具,已被upstart和systemd取代
  • upstart
  • systemd:绝大多数Linux OS的默认系统初始化工具。如fedora,ubuntu,opensuse,arch等。

linux启动系统时需要执行各个任务,每一个任务称为一个配置单元Unit,每一个配置单元有对应的配置单元文件
systemd的配置单元文件分为三部分:Unit,Service,Install

  • [Unit]:配置文件的第一个区块,定义Unit的元数据,配置与其它Unit的关系。
  • [Install]:通常为配置文件的最后一个区块,用来定义如何启动,是否开机自启动。
  • [Service]:用来定义Service的配置,只有Service类型的Unit才有这个区块。

在systemd体系框架中,所有服务程序都可并发启动,systemd系统采用各个服务之间相互依赖的解决方案,该方案的相互依赖分为三种类型:

  • socket依赖
  • D-Bus依赖
  • 文件系统依赖

每一种类型的依赖都可以通过相应的技术解除依赖关系,从而解决了所有服务程序并发启动冲突的问题。
在systemd初始化系统机制中,不管程序的依赖如何,全部可以并行启动,若调用的服务程序存在依赖关系,则自动激活其它程序。

例如ubuntu的systemd的启动顺序:

1.Boot Sequence :启动顺序。例如:硬盘启动,u盘启动。
2.Bootloader :引导加载
3.kernel+initramfs(initrd) :加载内核及initramfs或initrd
4.rootfs :启动文件系统
5./sbin/init :启动init进程



2.systemd-analyze

对于systemd的所有详细启动顺序,可以使用systemd自带的systemd-analyze。

systemd-analyze是一个分析启动性能的工具,用于分析启动时服务时间消耗。
默认显示启动是内核和用户空间的消耗时间。

	systemd-analyze		查看启动耗时
	systemd-analyze blame		查看每个服务的启动耗时
	systemd-analyze critical-chain		显示瀑布状的启动流
	systemd-analyze critical-chain <serviceName.service>		显示指定服务的启动流
	systemd-analyze plot > message.svg		将systemd启动顺序及消耗时间的详细信息生成svg
	systemd-analyze [OPTIONS...] plot [> file.svg]		.svg矢量图
	systemd-analyze [OPTIONS...] dot [PATTERN...] [> file.dot]		.dot位图


3.systemctl

systemctl为一个systemd工具,主要负责控制systemd系统和服务管理器。

systemctl list-units 命令可以查看当前系统的所有配置单元(Unit)
配置单元即为一个任务服务),每一个配置单元都有一个与之对应的配置单元文件。

	systemctl list-units		列出正在运行的Unit
	systemctl list-units --all		列出所有的配置单元
	systemctl list-units --all --state=inactive		列出所有没有运行的Unit
	systemctl list-units --failed		列出所有加载失败的Unit
	systemctl list-units --type=service		列出所有正在运行的,类型为service的Unit

systemctl status 命令用于查看系统状态和每个配置单元的状态。
By default, this function only shows 10 lines of output and ellipsizes lines to fit in the terminal window. This can be changed with –lines and –full.
journalctl --unit=NAME or journalctl --user-unit=NAME use a similar filter for messages and might be more convenient.
systemctl status : generate human-readable output.
systemctl show : computer-parsable output

	systemctl status		显示系统状态
	systemctl status <unitName>		显示单个配置单元的状态
	systemctl -H {user}@{ip} status <unitName>		显示远程主机的某个Unit的状态

systemctl {action} 为用户经常使用的命令。
action可为:

  • show :显示Unit配置参数

  • start :启动

  • stop :停止

  • kill :杀死

  • restart :stop+start 重启

  • reload :重载配置文件
    Asks all units listed on the command line to reload their configuration. Note that this will reload the service-specific configuration, not the unit configuration file of systemd.If you want systemd to reload the configuration file of a unit, use the daemon-reload command. In other words: for the example case of Apache, this will reload Apache’s httpd.conf in the web server, not the apache.service systemd unit file.
    This command should not be confused with the daemon-reload command.

  • Manager Lifecycle Commands
    daemon-reload
    Reload the systemd manager configuration. This will rerun all generators (see systemd.generator(7)), reload all unit files, and recreate the entire dependency tree. While the daemon is being reloaded, all sockets systemd listens on behalf of user configuration will stay accessible.
    This command should not be confused with the reload command.

  • is-active PATTERN…

  • is-failed PATTERN…

  • set-property UNIT PROPERTY=VALUE…

  • list-dependencies [UNIT]

  • enable :激活开机自启动

  • disable :取消开机自启动

  • is-enabled

      systemctl < show | start | stop | restart | kill | reload>	<UnitName>	显示Unit参数 | 启动 | 停止 | 杀死 | 重载配置文件
      systemctl show -p CPUShares mysql.service		显示某个Unit的指定属性的值
      systemctl daemon-reload		
      systemctl reload
      sudo systemctl set-property <UnitName> <key=value>
    

systemctl list-dependencies 命令查看systemctl配置单元的依赖关系。

	systemctl list-dependencies <UnitName>		列出一个配置单元Unit的所有依赖关系
	systemctl list-dependencies --all <UnitName>		展开Target需要使用--all参数,即展开依赖的依赖

systemctl enable | disable命令用于将systemd配置单元(任务)设为开机自启动 | 取消开机自启动。

	systemctl enable <UnitName>		将某个任务(Unit,配置单元)设为开机自启动
	systemctl disable <UnitName>		将某个任务(服务)取消开机自启动

systemctl list-unit-files命令,用于查看配置单元的所有配置单元文件及配置单元状态

	systemctl list-unit-files		列出所有配置文件
	systemctl list-unit-files --type=service		列出指定类型的配置单元文件

Unit的状态包含如下4种情况:

  • enabled :已建立启动链接,即已激活开机自启动功能
  • disabled :没有建立启动链接,或是已经取消开机自启动功能
  • static: 该配置文件没有[Install]部分,即无法被执行,只能作为其他配置文件的依赖
  • masked :该配置文件被禁止建立启动链接

systemd电源管理命令

	systemctl <reboot | poewroff | suspend | hibernate | hybird>		重启 | 关机 | 挂起 | 休眠 | 混合休眠

关于linux电源管理的几个命令会因为发行版或者FS的不同而不同,主要表现在休眠上,一些发行版不支持休眠选项,或是因为使用了不支持休眠选项的FS,例如Btrfs由于不支持swap交换分区,导致系统内存不能挂载到硬盘中,无法实现休眠。但默认的ext4 FS支持swap分区,支持休眠。



4.hostnamectl

hostnamectl用于查看与管理主机信息

	hostnamectl		查看主机信息
	hostnamectl set-hostname <hostName>		设置主机名称


5.timedatectl

timedatectl 命令用于管理时区

	timedatectl		查看时区日期等信息
	timedatectl set-timezone Asia/Shanghai		设置主机时区
	timedatectl set-local-rtc true		将硬件时钟配置为地方时


6.linux权限

在Linux中:一切皆为文件

linux FS区别Windows FS:
Windows通过硬盘分区(C盘,D盘,E盘等)分隔目录
linux/unix的所有目录都在根目录(/)下,根目录是所有目录的起点,所有其他的目录都挂载到根目录上,这些不同子目录可以分布在不同的硬盘分区,甚至不同的设备上。

tree -L <层数> :可以打印树形的目录结构

	/
	/boot					系统引导文件
	/etc					配置文件,系统管理
	/home	/root			用户,超级用户
	/usr	-->/bin,/sbin,/lib,/share,/local,/include,/src
	/lib	/opt			lib程序引用的库文件目录	opt第三方软件包和数据文件
	/bin	/sbin	/usr	二进制目录
	/dev	/media	/mnt	设备,媒体,挂载目录
	/proc	/srv	/run	进程,服务,系统运行目录
	/var					存放经常变化的文件,如日志文件
	/tmp					临时目录
	/sys					OS硬件信息
	/lost+found				保存丢失文件

在linux中,每一个资源文件仅属于一个所有者和一个拥有组,其目的为提高FS的安全性
通过ls -alF打印出的第一位为文件类型:

  • d 目录
  • -文件
  • l 链接文件
  • b 块设备,代表设备文件中可存储的接口设备
  • c 字符设备,代表设备文件中串行端口设备,比如鼠标,键盘
  • n 网络设备

u,g,o
rwx :r=100(4),w=010(2),x=001(1)

文件的全权限值:666。即u,g,o拥有r,w权限

目录的全权限值:777。即u,g,o拥有r,w,x权限

默认文件权限:umask命令。
umask命令用来设置所创建的文件和目录的默认权限。
umask大多设置在 /etc/profile 启动文件中,ubuntu设置在 /etc/login.defs 中。

	umask		显示默认权限
	umask 666		设置文件的全权限为000
	umask 777		设置目录的全权限为000

linux为一个多用户多任务的分时OS,用户ID:UID,组ID:GID。
超级管理员root的UID为0,1-499(1000)为系统保留,其余的用户ID从500往后。
用户分为三种:超级用户(0),系统用户(1-500),普通用户(>500)

为方便用户管理,引入用户组概念。
将一个用户加入一个用户组,该用户就拥有该用户组的相关权限。
linux用户组分为两种:基本组和附加组。
一个用户只能属于一个基本组,但可以加入多个附加组。
系统在创建用户时会默认将用户加入基本组。

用户与组的管理需使用超级权限root权限。
root权限可以通过sudo(或su)命令提权获得,sudo可以切换到其他身份执行命令,默认身份为root。
/etc/sudoers 中设置了可执行sudo命令的用户。
若未经授权的用户使用sudo,则会发出警告邮件给管理员。
用户使用sudo命令时,需要先输入用户的密码,之后默认有5分钟的有效期,超过该有效期必须重新输入密码。

/etc/passwd:linux系统用来将用户的登陆名匹配到对应的UID值

/etc/shadow:密码管理服务,只有root用户才能访问该文件。

/etc/group:保存组信息

	usersdd		创建用户		/usr/sb
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

killingwill

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

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

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

打赏作者

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

抵扣说明:

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

余额充值