Linux常用命令

用户和组的管理

运行级别和找回root密码

帮助指令

 

进程管理

1. ps 

ps -a : 显示当前终端的所有进程信息

ps -u : 以用户的格式显示进程信息

ps -x : 显示后台进程运行的参数

ps -aux 

[root@localhost ~]# ps -aux
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.1  19352  1548 ?        Ss   01:01   0:00 /sbin/init
root          2  0.0  0.0      0     0 ?        S    01:01   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    01:01   0:00 [migration/0]
root          4  0.0  0.0      0     0 ?        S    01:01   0:00 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S    01:01   0:00 [stopper/0]
root          6  0.0  0.0      0     0 ?        S    01:01   0:00 [watchdog/0]
......
root         16  0.0  0.0      0     0 ?        S    01:01   0:00 [sync_supers]
root         17  0.0  0.0      0     0 ?        S    01:01   0:00 [bdi-default]
root         18  0.0  0.0      0     0 ?        S    01:01   0:00 [kintegrityd/]
root         19  0.0  0.0      0     0 ?        S    01:01   0:00 [kblockd/0]
root         20  0.0  0.0      0     0 ?        S    01:01   0:00 [kacpid]
root         21  0.0  0.0      0     0 ?        S    01:01   0:00 [kacpi_notify]
root         22  0.0  0.0      0     0 ?        S    01:01   0:00 [kacpi_hotplu]
root         23  0.0  0.0      0     0 ?        S    01:01   0:00 [ata_aux]
root         24  0.0  0.0      0     0 ?        S    01:01   0:00 [ata_sff/0]
root       1593  0.0  0.3 577700  3620 ?        Sl   01:02   0:00 /usr/sbin/cons
root       1660  0.0  0.1 108316  1908 tty1     Ss+  01:02   0:00 -bash
root       1700  1.3  0.4 102084  4144 ?        Ds   01:30   0:00 sshd: root@pts
root       1704  0.6  0.1 108316  1860 pts/0    Ss   01:30   0:00 -bash
root       1717  3.0  0.1 110240  1180 pts/0    R+   01:30   0:00 ps -aux

# 通常使用 ps -aux | grep xxx 进行过滤

 [root@localhost ~]# ps -aux | grep sshd
 Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
 root 1424 0.0 0.1 66236 1204 ? Ss 01:02 0:00 /usr/sbin/sshd
 root 1700 0.0 0.4 102084 4144 ? Ss 01:30 0:00 sshd: root@pts/0
 root 1721 0.0 0.0 103328 880 pts/0 S+ 01:37 0:00 grep sshd

 

ps -ef  查看父进程

[root@localhost ~]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 01:01 ?        00:00:00 /sbin/init
root          2      0  0 01:01 ?        00:00:00 [kthreadd]
root          3      2  0 01:01 ?        00:00:00 [migration/0]
root          4      2  0 01:01 ?        00:00:00 [ksoftirqd/0]
root          5      2  0 01:01 ?        00:00:00 [stopper/0]
root          6      2  0 01:01 ?        00:00:00 [watchdog/0]
root         35      2  0 01:01 ?        00:00:00 [ksmd]
root         36      2  0 01:01 ?        00:00:00 [khugepaged]
root         37      2  0 01:01 ?        00:00:00 [aio/0]
root         38      2  0 01:01 ?        00:00:00 [crypto/0]
root         45      2  0 01:01 ?        00:00:00 [kthrotld/0]
root         46      2  0 01:01 ?        00:00:00 [pciehpd]
root         48      2  0 01:01 ?        00:00:00 [kpsmoused]
......
root        267      2  0 01:01 ?        00:00:00 [mpt/0]
root        268      2  0 01:01 ?        00:00:00 [scsi_eh_2]
root        411      2  0 01:01 ?        00:00:00 [jbd2/sda3-8]
root        412      2  0 01:01 ?        00:00:00 [ext4-dio-unwrit]
root       1573      1  0 01:02 ?        00:00:00 login -- root
root       1582    514  0 01:02 ?        00:00:00 /sbin/udevd -d
root       1584      1  0 01:02 tty5     00:00:00 /sbin/mingetty /dev/tty5
root       1586      1  0 01:02 tty6     00:00:00 /sbin/mingetty /dev/tty6
root       1593      1  0 01:02 ?        00:00:00 /usr/sbin/console-kit-daemon --no-daemon
root       1660   1573  0 01:02 tty1     00:00:00 -bash
root       1700   1424  0 01:30 ?        00:00:00 sshd: root@pts/0
root       1704   1700  0 01:30 pts/0    00:00:00 -bash
root       1727   1704  0 01:43 pts/0    00:00:00 ps -ef

 

pstree 以树状的形式展示进程信息

常用选项

-p :显示进程的PID

-u :显示进程所属的用户

[root@localhost ~]# pstree -u
init─┬─abrtd
     ├─acpid
     ├─anacron
     ├─atd
     ├─auditd───{auditd}
     ├─console-kit-dae───63*[{console-kit-da}]
     ├─crond
     ├─dbus-daemon(dbus)───{dbus-daemon}
     ├─hald(haldaemon)─┬─hald-runner(root)─┬─hald-addon-acpi(haldaemon)
     │                 │                   ├─hald-addon-inpu
     │                 │                   └─hald-addon-rfki
     │                 └─{hald}
     ├─login───bash
     ├─master─┬─pickup(postfix)
     │        └─qmgr(postfix)
     ├─5*[mingetty]
     ├─rsyslogd───3*[{rsyslogd}]
     ├─sshd───sshd───bash───pstree
     └─udevd───2*[udevd]

 

2. kill

终止进程

kill 和 killall 

kill [选项] 进程ID

killall 进程名 (支持通配符),如 killall gedit* 

常用选项 kill -9 强制停止进程

 

3. service

服务管理  

服务本质是一个个进程

服务名 start | stop | restart | reload | status

[root@localhost ~]# service network status
Configured devices:
lo eth0
Currently active devices:
lo eth0
[root@localhost ~]#

[root@localhost ~]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
num target prot opt source destination

 

查看服务

方式一:

setup --> 系统服务

命令行输入setup

 

 

 

 

方式二:查看/etc/init.d 下有哪些文件

[root@localhost ~]# ll /etc/init.d/
总用量 264
-rwxr-xr-x. 1 root root  1288 5月  12 2016 abrt-ccpp
-rwxr-xr-x. 1 root root  1628 5月  12 2016 abrtd
-rwxr-xr-x. 1 root root  1642 5月  12 2016 abrt-oops
-rwxr-xr-x. 1 root root  1818 2月  17 2016 acpid
-rwxr-xr-x. 1 root root  2062 2月  20 2015 atd
-rwxr-xr-x. 1 root root  3580 5月  11 2016 auditd
-r-xr-xr-x. 1 root root  1343 5月  11 2016 blk-availability
-rwxr-xr-x. 1 root root 11864 7月  24 2015 cpuspeed
-rwxr-xr-x. 1 root root  2826 11月 10 2015 crond
-rw-r--r--. 1 root root 25419 4月  12 2016 functions
-rwxr-xr-x. 1 root root  1801 10月 15 2014 haldaemon
-rwxr-xr-x. 1 root root  5985 4月  12 2016 halt
-rwxr-xr-x. 1 root root 11169 7月  24 2015 ip6tables
-rwxr-xr-x. 1 root root 11048 7月  24 2015 iptables
-rwxr-xr-x. 1 root root  1938 2月   2 2016 irqbalance
-rwxr-xr-x. 1 root root 20634 5月  11 2016 kdump
-rwxr-xr-x. 1 root root   652 4月  12 2016 killall
-r-xr-xr-x. 1 root root  2137 5月  11 2016 lvm2-lvmetad
-r-xr-xr-x. 1 root root  3045 5月  11 2016 lvm2-monitor
-rwxr-xr-x. 1 root root  2571 12月 11 2015 mdmonitor
-rwxr-xr-x. 1 root root  2200 4月  22 2015 messagebus
-rwxr-xr-x. 1 root root  2989 4月  12 2016 netconsole
-rwxr-xr-x. 1 root root  5309 4月  12 2016 netfs
-rwxr-xr-x. 1 root root  6406 4月  12 2016 network
-rwxr-xr-x. 1 root root  3570 5月  11 2016 nfs-rdma
-rwxr-xr-x. 1 root root  1923 1月  26 2016 ntpd
-rwxr-xr-x. 1 root root  2043 1月  26 2016 ntpdate
-rwxr-xr-x. 1 root root  3912 11月 10 2015 postfix
-rwxr-xr-x. 1 root root  1556 7月  17 2012 psacct
-rwxr-xr-x. 1 root root  2034 1月   7 2015 quota_nld
-rwxr-xr-x. 1 root root  1513 11月 13 2015 rdisc
-rwxr-xr-x. 1 root root 12709 5月  11 2016 rdma
-rwxr-xr-x. 1 root root  1822 5月  11 2016 restorecond
-rwxr-xr-x. 1 root root  1808 9月  22 2015 rngd
-rwxr-xr-x. 1 root root  2011 12月 10 2014 rsyslog
-rwxr-xr-x. 1 root root  1698 5月  11 2016 sandbox
-rwxr-xr-x. 1 root root  2056 2月  27 2015 saslauthd
-rwxr-xr-x. 1 root root   647 4月  12 2016 single
-rwxr-xr-x. 1 root root  3002 2月  22 2013 smartd
-rwxr-xr-x. 1 root root  4621 5月  11 2016 sshd
-rwxr-xr-x. 1 root root  1144 5月  11 2016 sysstat
-rwxr-xr-x. 1 root root  2294 5月  11 2016 udev-post

 

4. chkconfig 命令

可以给每个服务设置每个级别的开机自启动

chkconfig --list | grep xxx 

查看指定服务的开机自启动

chkconfig 服务名 --list

[root@localhost ~]# chkconfig iptables --list
iptables           0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
[root@localhost ~]#

chkconfig --level 3 服务名 on/off   

# 设置服务的哪个运行级别的开机自启动,如果不指定运行级别,即当前运行级别下的开机自启动

# 查看系统的默认运行级别: vi /etc/inittab

# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
# 默认运行级别
id:3:initdefault:

 

例如,关闭防火墙服务,操作如下:

[root@localhost ~]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[root@localhost ~]# chkconfig iptables --list
iptables           0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
[root@localhost ~]# chkconfig iptables off
[root@localhost ~]# chkconfig iptables --list
iptables           0:关闭    1:关闭    2:关闭    3:关闭    4:关闭    5:关闭    6:关闭

 

5. top

监控服务

top [-选项]

选项主要有以下3个:

top -d 秒数: 默认是3秒,指定每隔几秒更新

top -i : 不显示任何闲置或者僵尸进程

top -p : 仅仅监控指定进程id的状态

如: top -p 1424 (sshd 进程ID为1424)

top - 03:39:06 up  2:37,  2 users,  load average: 0.00, 0.03, 0.05
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1004136k total,   196464k used,   807672k free,    11580k buffers
Swap:  1048572k total,        0k used,  1048572k free,    56184k cached

   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
  1424 root      20   0 66236 1204  484 S  0.0  0.1   0:00.01 sshd

 

top 交付操作说明:

P:以CPU使用频率排序,默认此项

M:以内存的使用率排序

N:以PID排序

q:退出

u:监控特定用户下的状态,先输入u, 然后在输入用户名

 

 

 

k:监控状态下输入k , 输入对应的pid , 则杀死该进程

 

 

 

6. netstat

网络监控 

netstat -an : 按照一定顺序排列输出

netstat -p :显示哪个进程在调用

netstat -anp 常用的命令格式,查看所有的网络服务

[root@localhost ~]# netstat -anp | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program nam
e
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1424/sshd

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1504/master

tcp        0      0 172.16.9.101:22             172.16.9.1:54119            ESTABLISHED 1700/sshd

tcp        0      0 :::22                       :::*                        LISTEN      1424/sshd

tcp        0      0 ::1:25                      :::*                        LISTEN      1504/master

Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     12900  1271/dbus-daemon    /var/run/dbus/system_bus_s
ocket
unix  2      [ ACC ]     STREAM     LISTENING     13004  1310/acpid          /var/run/acpid.socket
unix  2      [ ACC ]     STREAM     LISTENING     13654  1504/master         public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     13661  1504/master         private/tlsmgr

 

查看指定的网络服务

[root@localhost ~]# netstat -anp |grep sshd
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1424/sshd
tcp        0      0 172.16.9.101:22             172.16.9.1:54119            ESTABLISHED 1700/sshd
tcp        0      0 :::22                       :::*                        LISTEN      1424/sshd
unix  2      [ ]         DGRAM                    14966  1700/sshd
[root@localhost ~]#

 

RPM和YUM管理

rpm -qa | grep xxx 

查询已经安装的rpm包

[root@localhost ~]# rpm -qa | grep vi
sysvinit-tools-2.87-6.dsf.el6.x86_64
virt-what-1.11-1.2.el6.x86_64
vim-common-7.4.629-5.el6.x86_64
vim-filesystem-7.4.629-5.el6.x86_64
device-mapper-libs-1.02.117-7.el6.x86_64
device-mapper-event-libs-1.02.117-7.el6.x86_64
device-mapper-event-1.02.117-7.el6.x86_64
device-mapper-persistent-data-0.6.2-0.1.rc7.el6.x86_64
vim-enhanced-7.4.629-5.el6.x86_64
device-mapper-1.02.117-7.el6.x86_64
vim-minimal-7.4.629-5.el6.x86_64

 

rpm -qi 包名: 查看安装的rpm软件信息

[root@localhost ~]# rpm -qi sysvinit-tools-2.87-6.dsf.el6.x86_64
Name        : sysvinit-tools               Relocations: (not relocatable)
Version     : 2.87                              Vendor: CentOS
Release     : 6.dsf.el6                     Build Date: 2015年07月24日 星期五 06时21分21秒
Install Date: 2021年03月07日 星期日 07时03分59秒      Build Host: c6b8.bsys.dev.centos.org
Group       : System Environment/Base       Source RPM: sysvinit-2.87-6.dsf.el6.src.rpm
Size        : 114115                           License: GPLv2+
Signature   : RSA/SHA1, 2015年07月25日 星期六 04时42分14秒, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem <http://bugs.centos.org>
URL         : https://alioth.debian.org/projects/pkg-sysvinit/
Summary     : Tools used for process and utmp management.
Description :
The sysvinit-tools package contains various tools used for process
management.

 

rpm -ql 包名:查询rpm软件包文件安装到哪里

[root@localhost ~]# rpm -ql sysvinit-tools-2.87-6.dsf.el6.x86_64
/bin/mountpoint
/sbin/killall5
/sbin/pidof
/sbin/sulogin
/usr/bin/last
/usr/bin/lastb
/usr/bin/mesg
/usr/bin/utmpdump
/usr/bin/wall
/usr/share/doc/sysvinit-tools-2.87
/usr/share/doc/sysvinit-tools-2.87/COPYRIGHT
/usr/share/doc/sysvinit-tools-2.87/Changelog
/usr/share/man/man1/last.1.gz
/usr/share/man/man1/lastb.1.gz
/usr/share/man/man1/mesg.1.gz
/usr/share/man/man1/mountpoint.1.gz
/usr/share/man/man1/wall.1.gz
/usr/share/man/man8/killall5.8.gz
/usr/share/man/man8/pidof.8.gz
/usr/share/man/man8/sulogin.8.gz

 

rpm -qf 文件名: 查询某个文件属于哪个rpm包

[root@localhost ~]# rpm -qf /etc/passwd
setup-2.8.14-20.el6_4.1.noarch
[root@localhost ~]#

 

卸载软件包

rpm -e 软件包:卸载已安装的软件包

rpm -e --nodeps 软件包 : 强制删除,不推荐,会破坏其他包的被依赖性

 

安装软件包

rpm -ivh rpm包全路径名称

参数说明:

-i : install 安装

-v:verbose 提示

-h: hash 进度条

 

YUM

yum 是一个shell 前端软件包管理器,基于RPM包管理,能够从指定的服务器自动下载rpm包并安装,可以自动处理依赖关系,并且一次安装所有依赖的软件包。

yum list | grep 软件名

查询服务器是否有需要安装的软件

 

yum install 软件名 

下载并安装软件

 

posted @ 2021-03-06 23:59  Justins999  阅读(76)  评论(0)    收藏  举报