Linux系统管道命令符

时间:2022-09-27 09:49:49
  • 管道命令符“|”的作用是将前一个命令的标准输出作为后一个命令的标准输入,格式为“命令A | 命令B”

以下实例中,通过grep命令搜索关键字“/sbin/nologin”在/etc/passwd中查找所有被系统限制登录的的用户,

通常的思路是

(1) 先找出被限制的用户:grep "/sbin/nologin" /etc/passwd

(2) 计算行数:wc -l

而使用管道命令符则为:

grep "/sbin/nologin" /etc/passwd | wc -l

实例1:
[root@localhost ~]# grep "/sbin/nologin" /etc/passwd
bin:x:::bin:/bin:/sbin/nologin
daemon:x:::daemon:/sbin:/sbin/nologin
adm:x:::adm:/var/adm:/sbin/nologin
lp:x:::lp:/var/spool/lpd:/sbin/nologin
mail:x:::mail:/var/spool/mail:/sbin/nologin
operator:x:::operator:/root:/sbin/nologin
games:x:::games:/usr/games:/sbin/nologin
ftp:x:::FTP User:/var/ftp:/sbin/nologin
nobody:x:::Nobody:/:/sbin/nologin
avahi-autoipd:x:::Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
ods:x:::softhsm private keys owner://var/lib/softhsm:/sbin/nologin
pegasus:x:::tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
systemd-bus-proxy:x:::systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:::systemd Network Management:/:/sbin/nologin
dbus:x:::System message bus:/:/sbin/nologin
polkitd:x:::User for polkitd:/:/sbin/nologin
sssd:x:::User for sssd:/:/sbin/nologin
colord:x:::User for colord:/var/lib/colord:/sbin/nologin
apache:x:::Apache:/usr/share/httpd:/sbin/nologin
tss:x:::Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
unbound:x:::Unbound DNS resolver:/etc/unbound:/sbin/nologin
usbmuxd:x:::usbmuxd user:/:/sbin/nologin
abrt:x::::/etc/abrt:/sbin/nologin
saslauth:x:::Saslauthd user:/run/saslauthd:/sbin/nologin
libstoragemgmt:x:::daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
geoclue:x:::User for geoclue:/var/lib/geoclue:/sbin/nologin
memcached:x:::Memcached daemon:/run/memcached:/sbin/nologin
rpc:x:::Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
postfix:x::::/var/spool/postfix:/sbin/nologin
setroubleshoot:x::::/var/lib/setroubleshoot:/sbin/nologin
rtkit:x:::RealtimeKit:/proc:/sbin/nologin
chrony:x::::/var/lib/chrony:/sbin/nologin
mysql:x:::MariaDB Server:/var/lib/mysql:/sbin/nologin
qemu:x:::qemu user:/:/sbin/nologin
ntp:x::::/etc/ntp:/sbin/nologin
rpcuser:x:::RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:::Anonymous NFS User:/var/lib/nfs:/sbin/nologin
radvd:x:::radvd user:/:/sbin/nologin
named:x:::Named:/var/named:/sbin/nologin
pcp:x:::Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
pulse:x:::PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
hsqldb:x::::/var/lib/hsqldb:/sbin/nologin
pkiuser:x:::Certificate System:/usr/share/pki:/sbin/nologin
gdm:x::::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x::::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:::Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
dovecot:x:::Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:::Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
sshd:x:::Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
oprofile:x:::Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x::::/:/sbin/nologin

现在用管道|合并执行计算符合条件的行数:
[root@localhost ~]# grep "/sbin/nologin" /etc/passwd | wc -l [root@localhost ~]#

实例2: 用翻页的形式查看/etc下有哪些目录和文件

[root@localhost ~]# ls -l /etc/ | more 

total 1672
drwxr-xr-x. 3 root root 97 Sep 6 16:24 abrt
-rw-r--r--. 1 root root 16 Sep 6 22:02 adjtime
drwxr-xr-x. 2 root root 112 Sep 6 21:55 akonadi
-rw-r--r--. 1 root root 1518 Jun 7 2013 aliases
-rw-r--r--. 1 root root 12288 Sep 6 13:07 aliases.db
drwxr-xr-x. 2 root root 49 Sep 6 21:51 alsa
drwxr-xr-x. 2 root root 4096 Sep 6 16:28 alternatives
drwxr-xr-x. 3 root root 43 Sep 6 21:56 amanda
-rw-------. 1 root root 541 Mar 31 23:09 anacrontab
-rw-r--r--. 1 root root 391 Jan 22 2014 ant.conf
drwxr-xr-x. 2 root root 6 Jan 22 2014 ant.d
-rw-r--r--. 1 root root 55 Sep 16 2014 asound.conf
-rw-r--r--. 1 root root 1 Jun 22 2015 at.deny
drwxr-xr-x. 2 root root 31 Sep 6 21:54 at-spi2
drwxr-x---. 3 root root 41 Sep 6 21:51 audisp
drwxr-x---. 3 root root 79 Sep 6 22:07 audit
-rw-r--r--. 1 root root 12706 Sep 17 2015 autofs.conf
-rw-------. 1 root root 232 Sep 17 2015 autofs_ldap_auth.conf
-rw-r--r--. 1 root root 795 Sep 17 2015 auto.master
drwxr-xr-x. 2 root root 6 Sep 17 2015 auto.master.d
-rw-r--r--. 1 root root 524 Sep 17 2015 auto.misc
-rwxr-xr-x. 1 root root 1260 Sep 17 2015 auto.net
-rwxr-xr-x. 1 root root 687 Sep 17 2015 auto.smb
drwxr-xr-x. 4 root root 94 Sep 6 16:24 avahi
drwxr-xr-x. 2 root root 4096 Sep 6 16:25 bash_completion.d
-rw-r--r--. 1 root root 2835 May 24 2015 bashrc
drwxr-xr-x. 2 root root 6 Aug 3 01:08 binfmt.d
drwxr-xr-x. 2 root root 41 Sep 6 21:47 bonobo-activation
drwxr-xr-x. 2 root root 12288 Sep 6 21:52 brltty
-rw-r--r--. 1 root root 21929 Oct 30 2014 brltty.conf
drwxr-xr-x. 2 root root 28 Sep 6 21:50 certmonger
-rw-r--r--. 1 root root 676 Sep 21 2014 cgconfig.conf
drwxr-xr-x. 2 root root 6 Sep 21 2014 cgconfig.d
-rw-r--r--. 1 root root 265 Sep 6 21:56 cgrules.conf
-rw-r--r--. 1 root root 131 Sep 21 2014 cgsnapshot_blacklist.conf

--more--

实例3:向pentest用户发一封邮件

[root@localhost ~]# echo "test content" | mail -s "testmail" pentest
[root@localhost ~]# su - pentest
[pentest@localhost ~]$ mail
Heirloom Mail version 12.5 //. Type ? for help.
"/var/spool/mail/pentest": message new
>N root Thu Sep : / "testmail"
&
Message :
From root@localhost.localdomain Thu Sep ::
Return-Path: <root@localhost.localdomain>
X-Original-To: pentest
Delivered-To: pentest@localhost.localdomain
Date: Thu, Sep :: +
To: pentest@localhost.localdomain
Subject: testmail
User-Agent: Heirloom mailx 12.5 //
Content-Type: text/plain; charset=us-ascii
From: root@localhost.localdomain (root)
Status: R test content &

实例4:使用非交互式方式更改pentest用户密码为toor

[root@localhost ~]# echo "toor" | passwd --stdin pentest
Changing password for user pentest.
passwd: all authentication tokens updated successfully.