linux命令之系统管理命令(下)详解程序员

1.chkconfig:管理开机服务

该命令为linux系统中的系统服务管理工具,可以查询和更新不同的运行等级下系统服务的启动状态。

选项说明
–list(常用)显示不同运行级别下服务的启动状态
–add(常用)添加一个系统服务
–del(常用)删除一个系统服务
–level(常用)指定运行级别

示例:

1)查看系统的服务状态

[[email protected] ~]# chkconfig --list 
NetworkManager  0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭 
abrt-ccpp       0:关闭  1:关闭  2:关闭  3:启用  4:关闭  5:启用  6:关闭 
abrtd           0:关闭  1:关闭  2:关闭  3:启用  4:关闭  5:启用  6:关闭

关于运行级别的说明:0表示关机;1表示单用户模式;2表示没有网络的多用户模式;3表示完全的多用户模式;4表示没有使用的级别;5表示图形界面多用户模式;6表示重启。

2)管理系统服务

[[email protected] ~]# chkconfig --list sshd  # 显示sshd不同运行级别下服务的启动状态 
sshd            0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭 
[[email protected] ~]# chkconfig sshd off  #  关闭2 3 4 5级别下开机自启动 
[[email protected] ~]# chkconfig --list sshd 
sshd            0:关闭  1:关闭  2:关闭  3:关闭  4:关闭  5:关闭  6:关闭 
[[email protected] ~]# chkconfig sshd on  # 打开2 3 4 5级别下开机自启动 
[[email protected] ~]# chkconfig --list sshd 
sshd            0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭 
[[email protected] ~]# chkconfig sshd --level 5 off  # 关闭sshd服务5级别开机自启动 
[[email protected] ~]# chkconfig --list sshd 
sshd            0:关闭  1:关闭  2:启用  3:启用  4:启用  5:关闭  6:关闭 
[[email protected] ~]# chkconfig sshd --level 5 on  # 打开sshd服务5级别开机自启动 
[[email protected] ~]# chkconfig --list sshd 
sshd            0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭

3)了解chkconfig的原理

chkconfig是在runlevel级别的/etc/rc.d/rc*.d目录中将对应服务做一个以S或K开头的软链接。

[[email protected] ~]# ls -l /etc/rc.d/rc3.d/ | grep sysstat  # 运行级别3的目录rc3.d查看sysstat 
lrwxrwxrwx. 1 root root 17 10月 22 04:18 S01sysstat -> ../init.d/sysstat  # S是开启 
[[email protected] ~]# chkconfig --list sysstat 
sysstat         0:关闭  1:启用  2:启用  3:启用  4:启用  5:启用  6:关闭 
[[email protected] ~]# chkconfig sysstat off 
[[email protected] ~]# chkconfig --list sysstat 
sysstat         0:关闭  1:启用  2:关闭  3:关闭  4:关闭  5:关闭  6:关闭 
[[email protected] ~]# ls -l /etc/rc.d/rc3.d/ | grep sysstat 
lrwxrwxrwx. 1 root root 17 4月  12 09:27 K99sysstat -> ../init.d/sysstat  # K是关闭

下面对rc3.d目录进行试验,chkconfig的–level 2345是同时对rc2.d、rc3.d、rc4.d、rc5.d这四个目录进行操作。

[[email protected] rc3.d]# ls -al /etc/rc.d/rc3.d/ | grep sysstat 
lrwxrwxrwx.  1 root root   17 4月  12 09:27 K99sysstat -> ../init.d/sysstat 
[[email protected] rc3.d]# rm -rf K99sysstat  
[[email protected] rc3.d]# chkconfig --list sysstat 
sysstat         0:关闭  1:启用  2:关闭  3:关闭  4:关闭  5:关闭  6:关闭 
[[email protected] rc3.d]# ln -s ../init.d/sysstat S01sysstat 
[[email protected] rc3.d]# chkconfig --list sysstat 
sysstat         0:关闭  1:启用  2:关闭  3:启用  4:关闭  5:关闭  6:关闭 
[[email protected] rc3.d]# rm -rf S01sysstat  
[[email protected] rc3.d]# ln -s ../init.d/sysstat K99sysstat 
[[email protected] rc3.d]# chkconfig --list sysstat 
sysstat         0:关闭  1:启用  2:关闭  3:关闭  4:关闭  5:关闭  6:关闭 
[[email protected] rc3.d]# cat /etc/rc.d/rc3.d/K99sysstat  
#!/bin/sh 
# 
# chkconfig: 12345 01 99  # 01和99的来源出自这里。

2.rpm:RPM包管理器

几乎所有的linux发行版本都是使用rpm命令进行管理,安装,卸载和更新软件。rpm命令包含了安装,卸载,升级,查询和验证等功能。

选项说明
-q(常用)查询软件包
-p(常用)后接以”.rpm”为后缀的软件包
-i(常用)如果配合-qp,则表示显示软件包的概要信息;如果是安装包,则表示安装
-l(常用)显示软件包中的所有文件列表
-R(常用)显示软件包的依赖环境
-v(常用)显示详细信息
-h(常用)用“#”显示安装进度条
-a(常用)与-q参数搭配使用,用于查询所有的软件包
-e(常用)卸载软件包
-f(常用)查询文件或命令属于哪个软件包

实例:

首先下载rpm包作为测试文件:https://mirrors.aliyun.com/centos/6.10/os/x86_64/Packages/lrzsz-0.12.20-27.1.el6.x86_64.rpm

1)查看rpm包信息

[[email protected] ~]# rpm -qpi lrzsz-0.12.20-27.1.el6.x86_64.rpm  
Name        : lrzsz                        Relocations: (not relocatable) 
Version     : 0.12.20                           Vendor: CentOS 
Release     : 27.1.el6                      Build Date: 2010年08月19日 星期四 14时20分40秒 
Install Date: (not installed)               Build Host: c6b3.bsys.dev.centos.org 
Group       : Applications/Communications   Source RPM: lrzsz-0.12.20-27.1.el6.src.rpm 
Size        : 162901                           License: GPLv2+ 
Signature   : RSA/8, 2011年07月03日 星期日 12时43分30秒, Key ID 0946fca2c105b9de 
Packager    : CentOS BuildSystem <http://bugs.centos.org> 
URL         : http://www.ohse.de/uwe/software/lrzsz.html 
Summary     : The lrz and lsz modem communications programs 
Description : 
Lrzsz (consisting of lrz and lsz) is a cosmetically modified 
zmodem/ymodem/xmodem package built from the public-domain version of 
the rzsz package. Lrzsz was created to provide a working GNU 
copylefted Zmodem solution for Linux systems.

2)查看rpm包的内容

[[email protected] ~]# rpm -qpl lrzsz-0.12.20-27.1.el6.x86_64.rpm   
/usr/bin/rb 
/usr/bin/rx 
/usr/bin/rz 
/usr/bin/sb 
/usr/bin/sx 
/usr/bin/sz 
/usr/share/locale/de/LC_MESSAGES/lrzsz.mo 
/usr/share/man/man1/rz.1.gz 
/usr/share/man/man1/sz.1.gz

3)查询rpm包的依赖

[[email protected] ~]# rpm -qpR lrzsz-0.12.20-27.1.el6.x86_64.rpm  
libc.so.6()(64bit)   
libc.so.6(GLIBC_2.11)(64bit)   
libc.so.6(GLIBC_2.2.5)(64bit)   
libc.so.6(GLIBC_2.3)(64bit)   
libc.so.6(GLIBC_2.3.4)(64bit)   
libc.so.6(GLIBC_2.4)(64bit)   
libc.so.6(GLIBC_2.7)(64bit)   
libnsl.so.1()(64bit)   
rpmlib(CompressedFileNames) <= 3.0.4-1 
rpmlib(FileDigests) <= 4.6.0-1 
rpmlib(PartialHardlinkSets) <= 4.0.4-1 
rpmlib(PayloadFilesHavePrefix) <= 4.0-1 
rtld(GNU_HASH)   
rpmlib(PayloadIsXz) <= 5.2-1

4)安装rpm包

[[email protected] ~]# rpm -ivh lrzsz-0.12.20-27.1.el6.x86_64.rpm  
Preparing...                ########################################### [100%] 
        package lrzsz-0.12.20-27.1.el6.x86_64 is already installed

5)查看系统中是否安装指定的rpm包

[[email protected] ~]# rpm -qa lrzsz 
lrzsz-0.12.20-27.1.el6.x86_64

6)卸载rpm包

[[email protected] ~]# rpm -e lrzsz 
[[email protected] ~]# rpm -qa lrzsz

7)查看文件属于哪个包

[[email protected] ~]# rpm -qf $(which ifconfig) 
net-tools-1.60-110.el6_2.x86_64

3.yum:自动化RPM包管理工具

yum主要用于自动安装,升级rpm软件包,它能自动查找并解决rpm包质检的依赖关系。

yum命令的常用参数为-y,表示确认操作,下面以安装httpd软件包为例,给出常用的yum命令。

命令说明
yum install httpd安装httpd软件包
yum update httpd更新httpd软件包
yum list httpd列出软件包
yum search httpd如果不记得软件包的确切名称,可以使用该命令搜索
yum info httpd获取软件包的信息,需要在安装软件包之前知道它的信息
yum deplist httpd查看软件包的依赖
yum clean all清理所有YUM的缓存内容
yum history查看yum的历史记录
yum check-update检查是否有可用的更新rpm软件包
yum list installed列出所有已安装的软件
yum list列出所有可用软件

示例:

1)安装httpd软件包

[[email protected] ~]# yum install httpd 
已加载插件:fastestmirror, refresh-packagekit, security 
设置安装进程 
Loading mirror speeds from cached hostfile 
* base: mirror.jdcloud.com 
* epel: mirrors.yun-idc.com 
* extras: mirror.jdcloud.com 
* updates: mirror.jdcloud.com 
解决依赖关系 
--> 执行事务检查 
---> Package httpd.x86_64 0:2.2.15-53.el6.centos will be 升级 
---> Package httpd.x86_64 0:2.2.15-69.el6.centos will be an update 
--> 处理依赖关系 httpd-tools = 2.2.15-69.el6.centos,它被软件包 httpd-2.2.15-69.el6.centos.x86_64 需要 
--> 执行事务检查 
---> Package httpd-tools.x86_64 0:2.2.15-53.el6.centos will be 升级 
---> Package httpd-tools.x86_64 0:2.2.15-69.el6.centos will be an update 
--> 完成依赖关系计算 
依赖关系解决 
================================================================================================= 
软件包                 架构              版本                             仓库             大小 
================================================================================================= 
正在升级: 
httpd                  x86_64            2.2.15-69.el6.centos             base            836 k 
为依赖而更新: 
httpd-tools            x86_64            2.2.15-69.el6.centos             base             81 k 
事务概要 
================================================================================================= 
Upgrade       2 Package(s) 
总下载量:917 k 
确定吗?[y/N]:y 
下载软件包: 
(1/2): httpd-2.2.15-69.el6.centos.x86_64.rpm                              | 836 kB     00:02      
(2/2): httpd-tools-2.2.15-69.el6.centos.x86_64.rpm                        |  81 kB     00:00      
------------------------------------------------------------------------------------------------- 
总计                                                              69 kB/s | 917 kB     00:13      
运行 rpm_check_debug  
执行事务测试 
事务测试成功 
执行事务 
Warning: RPMDB altered outside of yum. 
正在升级   : httpd-tools-2.2.15-69.el6.centos.x86_64                                       1/4  
正在升级   : httpd-2.2.15-69.el6.centos.x86_64                                             2/4  
清理       : httpd-2.2.15-53.el6.centos.x86_64                                             3/4  
清理       : httpd-tools-2.2.15-53.el6.centos.x86_64                                       4/4  
Verifying  : httpd-tools-2.2.15-69.el6.centos.x86_64                                       1/4  
Verifying  : httpd-2.2.15-69.el6.centos.x86_64                                             2/4  
Verifying  : httpd-tools-2.2.15-53.el6.centos.x86_64                                       3/4  
Verifying  : httpd-2.2.15-53.el6.centos.x86_64                                             4/4  
更新完毕: 
httpd.x86_64 0:2.2.15-69.el6.centos                                                             
作为依赖被升级: 
httpd-tools.x86_64 0:2.2.15-69.el6.centos                                                       
完毕!

2)检查httpd安装列表

[[email protected] ~]# yum list httpd  # 检查httpd安装列表 
已加载插件:fastestmirror, refresh-packagekit, security 
Loading mirror speeds from cached hostfile 
* base: mirrors.zju.edu.cn 
* epel: ftp.riken.jp 
* extras: mirrors.zju.edu.cn 
* updates: ftp.sjtu.edu.cn 
已安装的软件包 
httpd.x86_64                              2.2.15-69.el6.centos                              @base 
[[email protected] ~]# yum search httpd 
已加载插件:fastestmirror, refresh-packagekit, security 
Loading mirror speeds from cached hostfile 
* base: mirror.jdcloud.com 
* epel: mirror.pregi.net 
* extras: mirror.jdcloud.com 
* updates: mirror.jdcloud.com 
====================================== N/S Matched: httpd ======================================= 
iipsrv-httpd-fcgi.noarch : Apache HTTPD files for iipsrv 
libmicrohttpd-devel.i686 : Development files for libmicrohttpd 
libmicrohttpd-devel.x86_64 : Development files for libmicrohttpd 
libmicrohttpd-doc.noarch : Documentation for libmicrohttpd 
lighttpd-fastcgi.x86_64 : FastCGI module and spawning helper for lighttpd and PHP configuration 
lighttpd-mod_authn_gssapi.x86_64 : Authentication module for lighttpd that uses GSSAPI 
lighttpd-mod_authn_mysql.x86_64 : Authentication module for lighttpd that uses a MySQL database 
lighttpd-mod_geoip.x86_64 : GeoIP module for lighttpd to use for location lookups 
lighttpd-mod_mysql_vhost.x86_64 : Virtual host module for lighttpd that uses a MySQL database 
httpd.x86_64 : Apache HTTP Server 
httpd-devel.i686 : Development interfaces for the Apache HTTP server 
httpd-devel.x86_64 : Development interfaces for the Apache HTTP server 
httpd-itk.x86_64 : MPM Itk for Apache HTTP Server 
httpd-manual.noarch : Documentation for the Apache HTTP server 
httpd-tools.x86_64 : Tools for use with the Apache HTTP Server 
libmicrohttpd.i686 : Lightweight library for embedding a webserver in applications 
libmicrohttpd.x86_64 : Lightweight library for embedding a webserver in applications 
lighttpd.x86_64 : Lightning fast webserver with light system requirements 
mirmon-httpd.noarch : Apache configuration for mirmon 
mod_auth_mellon.x86_64 : A SAML 2.0 authentication module for the Apache Httpd Server 
mod_dav_svn.x86_64 : Apache httpd module for Subversion server 
mod_dnssd.x86_64 : An Apache HTTPD module which adds Zeroconf support 
python-mozhttpd.noarch : Basic Python webserver 
python2-sphinxcontrib-httpdomain.noarch : Sphinx domain for documenting HTTP APIs 
sysusage-httpd.noarch : Apache configuration for sysusage 
viewvc-httpd.noarch : ViewVC configuration for Apache/mod_python 
web-assets-httpd.noarch : Web Assets aliases for the Apache HTTP daemon 
Name and summary matches only, use "search all" for everything.

 

注:本文内容为《跟老男孩学linux运维 核心系统命令实践》的学习笔记。

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/2432.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论