allenle@backupabc:~$ cat retrictedshell.sh
#!/bin/bash
echo
echo "I am in `pwd`"
echo "Changing directory."
cd /usr
echo "New dirctory in `pwd`"
echo "Coming back home."
cd -
echo "I am again in `pwd`"
echo "--------------------restricted shells------------------------------"
set -r
#or set --restricted
echo "==> Now in restricted mode. <=="
echo
echo
echo "Tring directory change in restricted mode."
cd ..
echo "I am in `pwd`"
echo
echo
echo "\$SHELL = $SHELL"
echo "Attempting to change shell in restricted mode."
SHELL="/bin/ash"
echo
echo "\$SHELL= $SHELL"
echo
echo
echo "Attempting to redirect output in restricted mode."
ls -l /usr/bin > bin.files
ls -l bin.files # Try to list attempted file creation effort.
echo
exit 0
从结果图看,在非受限模式下,变更目录的操作成功了。
当改变了shell模式为受限模式(set -r),cd命令变更目录的操作就不成功了。
不仅如此,当试图修改$shell变量时,也是失败的。
注:脚本开头以"#!/bin/bash -r"来调用,则以受限模式运行脚本。