我尝试运行shell_exec("ls -l /dev");
列出所有条目/dev
并得到以下列表:
total 0
drwxr-xr-x 2 root root 180 Oct 10 11:19 char
lrwxrwxrwx 1 root root 11 Oct 10 11:19 core -> /proc/kcore
lrwxrwxrwx 1 root root 13 Oct 10 11:19 fd -> /proc/self/fd
crw-rw-rw- 1 root root 1, 7 Oct 10 11:19 full
drwxr-xr-x 2 root root 0 Oct 8 14:21 hugepages
lrwxrwxrwx 1 root root 28 Oct 10 11:19 log -> /run/systemd/journal/dev-log
drwxrwxrwt 2 root root 40 Oct 8 14:21 mqueue
crw-rw-rw- 1 root root 1, 3 Oct 10 11:19 null
crw-rw-rw- 1 root root 5, 2 Oct 10 11:19 ptmx
drwxr-xr-x 2 root root 0 Oct 8 14:21 pts
crw-rw-rw- 1 root root 1, 8 Oct 10 11:19 random
drwxrwxrwt 2 root root 40 Oct 10 11:38 shm
lrwxrwxrwx 1 root root 15 Oct 10 11:19 stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Oct 10 11:19 stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Oct 10 11:19 stdout -> /proc/self/fd/1
crw-rw-rw- 1 root root 5, 0 Oct 10 11:19 tty
crw-rw-rw- 1 root root 1, 9 Oct 10 11:19 urandom
crw-rw-rw- 1 root root 1, 5 Oct 10 11:19 zero
如果我在终端运行 ls -l /dev,我会得到以下信息:
total 0
crw-r--r-- 1 root root 10, 235 Oct 8 14:21 autofs
drwxr-xr-x 2 root root 200 Oct 8 14:21 block
drwxr-xr-x 2 root root 80 Oct 8 14:21 bsg
crw------- 1 root root 10, 234 Oct 8 14:21 btrfs-control
drwxr-xr-x 3 root root 60 Oct 8 14:21 bus
drwxr-xr-x 2 root root 3660 Oct 9 01:59 char
crw------- 1 root root 5, 1 Oct 8 14:21 console
lrwxrwxrwx 1 root root 11 Oct 8 14:21 core -> /proc/kcore
drwxr-xr-x 2 root root 60 Oct 8 14:21 cpu
crw------- 1 root root 10, 60 Oct 8 14:21 cpu_dma_latency
crw------- 1 root root 10, 203 Oct 8 14:21 cuse
drwxr-xr-x 8 root root 160 Oct 8 14:21 disk
drwxr-xr-x 2 root root 60 Oct 8 14:21 dma_heap
drwxr-xr-x 3 root root 100 Oct 8 14:21 dri
crw-rw---- 1 root video 29, 0 Oct 8 14:21 fb0
lrwxrwxrwx 1 root root 13 Oct 8 14:21 fd -> /proc/self/fd
crw-rw-rw- 1 root root 1, 7 Oct 8 14:21 full
crw-rw-rw- 1 root root 10, 229 Oct 8 14:21 fuse
crw------- 1 root root 239, 0 Oct 8 14:21 hidraw0
crw------- 1 root root 239, 1 Oct 8 14:21 hidraw1
crw------- 1 root root 239, 2 Oct 8 14:21 hidraw2
crw------- 1 root root 239, 3 Oct 8 14:21 hidraw3
crw------- 1 root root 10, 228 Oct 8 14:21 hpet
drwxr-xr-x 2 root root 0 Oct 8 14:21 hugepages
crw------- 1 root root 10, 183 Oct 8 14:21 hwrng
drwxr-xr-x 4 root root 480 Oct 8 14:21 input
crw-r--r-- 1 root root 1, 11 Oct 8 14:21 kmsg
crw-rw-rw- 1 root kvm 10, 232 Oct 8 14:21 kvm
drwxr-xr-x 2 root root 60 Oct 8 14:21 lightnvm
lrwxrwxrwx 1 root root 28 Oct 8 14:21 log -> /run/systemd/journal/dev-log
crw-rw---- 1 root disk 10, 237 Oct 8 14:21 loop-control
drwxr-xr-x 2 root root 60 Oct 8 14:21 mapper
crw-r----- 1 root kmem 1, 1 Oct 8 14:21 mem
drwxrwxrwt 2 root root 40 Oct 8 14:21 mqueue
drwxr-xr-x 2 root root 60 Oct 8 14:21 net
crw-rw-rw- 1 root root 1, 3 Oct 8 14:21 null
crw-rw-rw- 1 root root 195, 0 Oct 8 14:21 nvidia0
crw-rw-rw- 1 root root 195, 255 Oct 8 14:21 nvidiactl
crw-rw-rw- 1 root root 195, 254 Oct 8 14:21 nvidia-modeset
crw------- 1 root root 10, 144 Oct 8 14:21 nvram
crw-r----- 1 root kmem 1, 4 Oct 8 14:21 port
crw------- 1 root root 108, 0 Oct 8 14:21 ppp
crw------- 1 root root 10, 1 Oct 8 14:21 psaux
crw-rw-rw- 1 root tty 5, 2 Oct 10 11:39 ptmx
drwxr-xr-x 2 root root 0 Oct 8 14:21 pts
crw-rw-rw- 1 root root 1, 8 Oct 8 14:21 random
crw------- 1 root root 10, 242 Oct 8 14:21 rfkill
lrwxrwxrwx 1 root root 4 Oct 8 14:21 rtc -> rtc0
crw------- 1 root root 249, 0 Oct 8 14:21 rtc0
brw-rw---- 1 root disk 8, 0 Oct 8 14:21 sda
brw-rw---- 1 root disk 8, 1 Oct 8 14:21 sda1
brw-rw---- 1 root disk 8, 2 Oct 8 14:21 sda2
brw-rw---- 1 root disk 8, 3 Oct 8 14:21 sda3
brw-rw---- 1 root disk 8, 4 Oct 8 14:21 sda4
brw-rw---- 1 root disk 8, 16 Oct 8 14:21 sdb
brw-rw---- 1 root disk 8, 17 Oct 8 14:21 sdb1
brw-rw---- 1 root disk 8, 18 Oct 8 14:21 sdb2
drwxrwxrwt 2 root root 40 Oct 10 11:39 shm
crw------- 1 root root 10, 231 Oct 8 14:21 snapshot
drwxr-xr-x 3 root root 400 Oct 8 14:21 snd
lrwxrwxrwx 1 root root 15 Oct 8 14:21 stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Oct 8 14:21 stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Oct 8 14:21 stdout -> /proc/self/fd/1
crw-rw-rw- 1 root tty 5, 0 Oct 10 11:15 tty
crw--w---- 1 root tty 4, 0 Oct 8 14:21 tty0
crw--w---- 1 root tty 4, 1 Oct 8 14:21 tty1
crw--w---- 1 root tty 4, 10 Oct 8 14:21 tty10
crw--w---- 1 root tty 4, 11 Oct 8 14:21 tty11
crw--w---- 1 root tty 4, 12 Oct 8 14:21 tty12
crw--w---- 1 root tty 4, 13 Oct 8 14:21 tty13
crw--w---- 1 root tty 4, 14 Oct 8 14:21 tty14
crw--w---- 1 root tty 4, 15 Oct 8 14:21 tty15
crw--w---- 1 root tty 4, 16 Oct 8 14:21 tty16
crw--w---- 1 root tty 4, 17 Oct 8 14:21 tty17
crw--w---- 1 root tty 4, 18 Oct 8 14:21 tty18
crw--w---- 1 root tty 4, 19 Oct 8 14:21 tty19
crw--w---- 1 root tty 4, 2 Oct 8 14:21 tty2
crw--w---- 1 root tty 4, 20 Oct 8 14:21 tty20
crw--w---- 1 root tty 4, 21 Oct 8 14:21 tty21
crw--w---- 1 root tty 4, 22 Oct 8 14:21 tty22
crw--w---- 1 root tty 4, 23 Oct 8 14:21 tty23
crw--w---- 1 root tty 4, 24 Oct 8 14:21 tty24
crw--w---- 1 root tty 4, 25 Oct 8 14:21 tty25
crw--w---- 1 root tty 4, 26 Oct 8 14:21 tty26
crw--w---- 1 root tty 4, 27 Oct 8 14:21 tty27
crw--w---- 1 root tty 4, 28 Oct 8 14:21 tty28
crw--w---- 1 root tty 4, 29 Oct 8 14:21 tty29
crw--w---- 1 root tty 4, 3 Oct 8 14:21 tty3
crw--w---- 1 root tty 4, 30 Oct 8 14:21 tty30
crw--w---- 1 root tty 4, 31 Oct 8 14:21 tty31
crw--w---- 1 root tty 4, 32 Oct 8 14:21 tty32
crw--w---- 1 root tty 4, 33 Oct 8 14:21 tty33
crw--w---- 1 root tty 4, 34 Oct 8 14:21 tty34
crw--w---- 1 root tty 4, 35 Oct 8 14:21 tty35
crw--w---- 1 root tty 4, 36 Oct 8 14:21 tty36
crw--w---- 1 root tty 4, 37 Oct 8 14:21 tty37
crw--w---- 1 root tty 4, 38 Oct 8 14:21 tty38
crw--w---- 1 root tty 4, 39 Oct 8 14:21 tty39
crw--w---- 1 root tty 4, 4 Oct 8 14:21 tty4
crw--w---- 1 root tty 4, 40 Oct 8 14:21 tty40
crw--w---- 1 root tty 4, 41 Oct 8 14:21 tty41
crw--w---- 1 root tty 4, 42 Oct 8 14:21 tty42
crw--w---- 1 root tty 4, 43 Oct 8 14:21 tty43
crw--w---- 1 root tty 4, 44 Oct 8 14:21 tty44
crw--w---- 1 root tty 4, 45 Oct 8 14:21 tty45
crw--w---- 1 root tty 4, 46 Oct 8 14:21 tty46
crw--w---- 1 root tty 4, 47 Oct 8 14:21 tty47
crw--w---- 1 root tty 4, 48 Oct 8 14:21 tty48
crw--w---- 1 root tty 4, 49 Oct 8 14:21 tty49
crw--w---- 1 root tty 4, 5 Oct 8 14:21 tty5
crw--w---- 1 root tty 4, 50 Oct 8 14:21 tty50
crw--w---- 1 root tty 4, 51 Oct 8 14:21 tty51
crw--w---- 1 root tty 4, 52 Oct 8 14:21 tty52
crw--w---- 1 root tty 4, 53 Oct 8 14:21 tty53
crw--w---- 1 root tty 4, 54 Oct 8 14:21 tty54
crw--w---- 1 root tty 4, 55 Oct 8 14:21 tty55
crw--w---- 1 root tty 4, 56 Oct 8 14:21 tty56
crw--w---- 1 root tty 4, 57 Oct 8 14:21 tty57
crw--w---- 1 root tty 4, 58 Oct 8 14:21 tty58
crw--w---- 1 root tty 4, 59 Oct 8 14:21 tty59
crw--w---- 1 root tty 4, 6 Oct 8 14:21 tty6
crw--w---- 1 root tty 4, 60 Oct 8 14:21 tty60
crw--w---- 1 root tty 4, 61 Oct 8 14:21 tty61
crw--w---- 1 root tty 4, 62 Oct 8 14:21 tty62
crw--w---- 1 root tty 4, 63 Oct 8 14:21 tty63
crw--w---- 1 root tty 4, 7 Oct 8 14:21 tty7
crw--w---- 1 root tty 4, 8 Oct 8 14:21 tty8
crw--w---- 1 root tty 4, 9 Oct 8 14:21 tty9
crw-rw---- 1 root uucp 4, 64 Oct 8 14:21 ttyS0
crw-rw---- 1 root uucp 4, 65 Oct 8 14:21 ttyS1
crw-rw---- 1 root uucp 4, 66 Oct 8 14:21 ttyS2
crw-rw---- 1 root uucp 4, 67 Oct 8 14:21 ttyS3
crw-rw---- 1 root kvm 10, 61 Oct 8 14:21 udmabuf
crw------- 1 root root 10, 239 Oct 8 14:21 uhid
crw------- 1 root root 10, 223 Oct 8 14:21 uinput
crw-rw-rw- 1 root root 1, 9 Oct 8 14:21 urandom
drwxr-xr-x 2 root root 60 Oct 8 14:21 usb
crw------- 1 root root 10, 240 Oct 8 14:21 userio
crw------- 1 root root 10, 59 Oct 8 14:21 vboxdrv
crw-rw-rw- 1 root root 10, 58 Oct 8 14:21 vboxdrvu
crw------- 1 root root 10, 57 Oct 8 14:21 vboxnetctl
drwxr-x--- 4 root vboxusers 80 Oct 8 14:21 vboxusb
crw-rw---- 1 root tty 7, 0 Oct 8 14:21 vcs
crw-rw---- 1 root tty 7, 1 Oct 8 14:21 vcs1
crw-rw---- 1 root tty 7, 2 Oct 8 14:21 vcs2
crw-rw---- 1 root tty 7, 3 Oct 8 14:21 vcs3
crw-rw---- 1 root tty 7, 4 Oct 8 14:21 vcs4
crw-rw---- 1 root tty 7, 5 Oct 8 14:21 vcs5
crw-rw---- 1 root tty 7, 6 Oct 8 14:21 vcs6
crw-rw---- 1 root tty 7, 63 Oct 9 01:59 vcs63
crw-rw---- 1 root tty 7, 128 Oct 8 14:21 vcsa
crw-rw---- 1 root tty 7, 129 Oct 8 14:21 vcsa1
crw-rw---- 1 root tty 7, 130 Oct 8 14:21 vcsa2
crw-rw---- 1 root tty 7, 131 Oct 8 14:21 vcsa3
crw-rw---- 1 root tty 7, 132 Oct 8 14:21 vcsa4
crw-rw---- 1 root tty 7, 133 Oct 8 14:21 vcsa5
crw-rw---- 1 root tty 7, 134 Oct 8 14:21 vcsa6
crw-rw---- 1 root tty 7, 191 Oct 9 01:59 vcsa63
crw-rw---- 1 root tty 7, 64 Oct 8 14:21 vcsu
crw-rw---- 1 root tty 7, 65 Oct 8 14:21 vcsu1
crw-rw---- 1 root tty 7, 66 Oct 8 14:21 vcsu2
crw-rw---- 1 root tty 7, 67 Oct 8 14:21 vcsu3
crw-rw---- 1 root tty 7, 68 Oct 8 14:21 vcsu4
crw-rw---- 1 root tty 7, 69 Oct 8 14:21 vcsu5
crw-rw---- 1 root tty 7, 70 Oct 8 14:21 vcsu6
crw-rw---- 1 root tty 7, 127 Oct 9 01:59 vcsu63
drwxr-xr-x 2 root root 60 Oct 8 14:21 vfio
crw------- 1 root root 10, 63 Oct 8 14:21 vga_arbiter
crw------- 1 root root 10, 137 Oct 8 14:21 vhci
crw------- 1 root root 10, 238 Oct 8 14:21 vhost-net
crw------- 1 root root 10, 241 Oct 8 14:21 vhost-vsock
crw------- 1 root root 10, 130 Oct 8 14:21 watchdog
crw------- 1 root root 246, 0 Oct 8 14:21 watchdog0
crw-rw-rw- 1 root root 1, 5 Oct 8 14:21 zero
PHP 以与终端用户相同的用户运行:
echo shell_exec("id");
返回:
uid=1000(alien) gid=998(wheel) groups=998(wheel),108(vboxusers),150(wireshark)
在终端运行id
返回:
uid=1000(alien) gid=998(wheel) groups=998(wheel),108(vboxusers),150(wireshark)
请帮忙,为什么我看不到所有条目/dev
以及如何解决?
ps Apache 也以同一用户身份运行
也许与环境有关?如果我运行echo shell_exec("/bin/printenv");
它,它会返回:
PWD=/home/alien/http/localhost
HOME=/home/alien
USER=alien
SHLVL=0
_=/bin/printenv
在终端运行printenv
返回:
SHELL=/bin/bash
SESSION_MANAGER=local/black:@/tmp/.ICE-unix/748,unix/black:/tmp/.ICE-unix/748
WINDOWID=71303175
QT_SCREEN_SCALE_FACTORS=DVI-I-0=1;VGA-0=1;DVI-I-1=1;HDMI-0=1;
COLORTERM=truecolor
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session1
HISTCONTROL=ignoredups
LANGUAGE=
SHELL_SESSION_ID=2cf1621fe1f04785b31888421768dd02
DESKTOP_SESSION=/usr/share/xsessions/plasma
GTK_RC_FILES=/etc/gtk/gtkrc:/home/alien/.gtkrc:/home/alien/.config/gtkrc
XCURSOR_SIZE=24
GTK_MODULES=canberra-gtk-module
XDG_SEAT=seat0
PWD=/home/alien
XDG_SESSION_DESKTOP=KDE
LOGNAME=alien
XDG_SESSION_TYPE=x11
XAUTHORITY=/home/alien/.Xauthority
MOTD_SHOWN=pam
GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/alien/.gtkrc-2.0:/home/alien/.config/gtkrc-2.0
HOME=/home/alien
LANG=en_US.UTF-8
XDG_CURRENT_DESKTOP=KDE
KONSOLE_DBUS_SERVICE=:1.689
KONSOLE_DBUS_SESSION=/Sessions/2
PROFILEHOME=
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
KONSOLE_VERSION=200801
KDE_SESSION_UID=1000
XDG_SESSION_CLASS=user
TERM=xterm-256color
USER=alien
COLORFGBG=15;0
KDE_SESSION_VERSION=5
PAM_KWALLET5_LOGIN=/run/user/1000/kwallet5.socket
DISPLAY=:0
SHLVL=1
XDG_VTNR=1
XDG_SESSION_ID=2
XDG_RUNTIME_DIR=/run/user/1000
QT_AUTO_SCREEN_SCALE_FACTOR=0
XCURSOR_THEME=breeze_cursors
KDE_FULL_SESSION=true
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
KDE_APPLICATIONS_AS_SCOPE=1
MAIL=/var/spool/mail/alien
KONSOLE_DBUS_WINDOW=/Windows/1
_=/usr/bin/printenv
如果我运行以下命令,也会在 php 中得到不同的结果:
echo shell_exec("sudo -u alien /bin/printenv");
MAIL=/var/mail/alien
LOGNAME=alien
USER=alien
HOME=/home/alien
SHELL=/bin/bash
TERM=unknown
PATH=/usr/bin:/bin:/usr/sbin:/sbin
SUDO_COMMAND=/bin/printenv
SUDO_USER=alien
SUDO_UID=1000
SUDO_GID=998
答案1
我明白了!这是php-fpm.service
文件中的默认设置:
# Sets up a new /dev namespace for the executed processes and only adds API pseudo devices
# such as /dev/null, /dev/zero or /dev/random (as well as the pseudo TTY subsystem) to it,
# but no physical devices such as /dev/sda.
PrivateDevices=true
因此我将其设置为false
,然后我突然就可以完全访问目录项了。
花了一整天的时间才找到答案...