有很多不同的方法可以实现这一目标。我将列出几种可能的解决方案之一。
我建议使用几个不同的保护层来防止用户运行他们不应该被允许访问的命令。这里的所有说明都假设用户有自己的/home/[username]
目录,他们的 shell 是/bin/bash
,并且您希望他们在登录系统时使用 bash shell。
-
将用户的 bash 更改为受限 bash 模式,以便他们无法更改目录(如果您的系统上没有受限制的 bash 模式,这个链接 http://www.gnu.org/software/bash/manual/html_node/The-Restricted-Shell.html将为您提供帮助并提供更多信息)chsh -s /bin/rbash [username]
-
更改目录权限,以便只有用户可以编辑其主目录的内容
chmod 755 /home/[username]
- 删除用户的
.bashrc
file
rm /home/[username]/.bashrc
这个网站 http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html有更多信息说明为什么删除该文件可能是一个好主意.bashrc
在这个情况下。
- 创建一个
.bash_profile
并为您要禁用的所有命令添加“安全”别名
./bash_profile 文件示例
alias apt-get="printf ''"
alias aptitude="printf ''"
[...]
alias vi="vi -Z" #this is vi's safe mode and shell commands won't be run from within vi
alias alias="printf ''"
请检查bash 命令的完整列表 http://ss64.com/bash/了解更多信息。您必须确保alias alias="printf ''"
command 是列表中的最后一个命令,否则您将无法为所有这些命令添加别名。
Note运行下面的命令将搜索系统上几乎所有可用的命令,并输出一个现成的文件,其中几乎所有可用的命令都已预先别名。这[
命令是test
command在bash中。因此,如果您在文件中看到这一点,则这不是错误。
#search /bin and /usr/bin for any commands that exist on our system
ls /bin -1 > commands_on_system.txt && ls /usr/bin -1 >> commands_on_system.txt
#format and save this information to a bash variable
IFS=$'\n' GLOBIGNORE='*' command eval 'COMMANDS_ON_SYSTEM=($(cat ./commands_on_system.txt))'
IFS=$'\n' COMMANDS_ON_SYSTEM=($(sort <<<"${COMMANDS_ON_SYSTEM[*]}"))
unset IFS
#save these commands in aliased format for easy usage
for linux_command in "${COMMANDS_ON_SYSTEM[@]}"
do :
#you can change how this works to automatically
#setup the command file for you
echo "alias ${linux_command}=\"printf ''\"" >> ./startup_functions_for_beginners.sh
done
-
通过将 vi 命令别名为受限模式来禁用 vi 中的 shell 命令
语法是alias vi="vi -Z"
,但是请看这个网站 http://vimdoc.sourceforge.net/htmldoc/starting.html#-Z了解更多信息。
-
更改用户的所有权.bash_profile
to root
chown root:root /home/[username]/.bash_profile
-
最后,删除用户的写入权限.bash_profile
chmod 755 /home/[username/.bash_profile]
现在,当用户登录时,他们将无法更改目录,您不希望他们使用的所有命令将输出相同的信息,就像用户按下[ENTER]
没有指定命令的键,以及您的/bin/bash
功能保持完好。
根据您选择以这种方式别名或不以这种方式别名的功能,用户仍然可以绕过您实现的某些控件。然而,由于我们实现了一些安全缓冲区,因此用户确实必须了解计算机系统才能执行任何危险操作。
在相关说明和您可能需要考虑的事情上,如果您直接将这些别名放入每个用户的.bash_profile
您将很难维护哪些函数应该使用别名,哪些函数不应该使用别名,并且如果您需要更改任何内容的别名,则必须单独更改所有函数。另外,由于用户可以使用vim
or vi
要查看文件,他们可以看到其内容.bash_profile
并了解他们有哪些限制和没有哪些限制。
为了解决这个问题,我建议。
- 将所有别名放在用户无法访问的目录中(粘贴
.bash_profile
here)
/[path_to_file]/startup_functions_for_beginners.sh
- 将别名纳入他们的
.bash_profile
改进的 ./bash_profile 文件示例
if [[ -f /[path_to_file]/startup_functions_for_beginners.sh ]]; then
. /[path_to_file]/startup_functions_for_beginners.sh
fi
这应该会让您上路,但请记住,几乎总有办法规避限制。
另外,请随意重新组合此答案中的信息以满足您的需求。这些绝对可以与许多其他限制结合起来。
问:我需要用户有权访问fg
and bg
,但我不希望他们能够访问aptitude
or bash
alias apt-get="printf ''" #the user won't be able to run this
alias aptitude="printf ''" #the user won't be able to run this
alias bash="printf ''" #the user won't be able to run this
#alias fg="printf ''" #this will run as a bash built-in
#alias bg="printf ''" #you actually don't need to include these in your script
常用命令列表如下哈佛网站 http://www.math.harvard.edu/computing/unix/unixcommands.html(并非详尽无遗)
当您将程序安装到 Linux 时,您可用的内容会发生变化。我建议您运行上面步骤 4 中列出的命令,以帮助在安装新命令后查找它们。
使用编辑器时应小心,因为有些编辑器允许从程序内执行 shell 命令
nano
emacs
pico
sed
vi
vim
其他一切
exit
logout
passwd
rlogin
ssh
slogin
yppasswd
mail
mesg
pine
talk
write
as
awk
bc
cc
csh
dbx
f77
gdb
gprof
kill
ld
lex
lint
make
maple
math
nice
nohup
pc
perl
prof
python
sh
yacc
xcalc
apropos
find
info
man
whatis
whereis
cd
chmod
chown
chgrp
cmp
comm
cp
crypt
diff
file
grep
gzip
ln
ls
lsof
mkdir
mv
pwd
quota
rm
rmdir
stat
sync
sort
tar
tee
tr
umask
uncompress
uniq
wc
cat
fold
head
lpq
lpr
lprm
more
less
page
pr
tail
zcat
xv
gv
xpdf
ftp
rsync
scp
alias
chquota
chsh
clear
echo
pbm
popd
pushd
script
setenv
stty
netstat
rsh
ssh
bg
fg
jobs
^y
^z
clock
date
df
du
env
finger
history
last
lpq
manpath
printenv
ps
pwd
set
spend
stty
time
top
uptime
w
who
whois
whoami
gimp
xfig
xv
xvscan
xpaint
kpaint
mplayer
realplay
timidity
xmms
abiword
addbib
col
diction
diffmk
dvips
explain
grap
hyphen
ispell
latex
pdfelatex
latex2html
lookbib
macref
ndx
neqn
nroff
pic
psdit
ptx
refer
roffbib
sortbib
spell
ispell
style
tbl
tex
tpic
wget
grabmode
import
xdpyinfo
xkill
xlock
xterm
xwininfo
html2ps
latex2html
lynx
netscape
sitecopy
weblint