限制linux用户执行命令,如何在Linux中限制用户命令

有很多不同的方法可以实现这一点。 我将列出几种可能的解决方案之一。

我会建议使用几个不同的保护层来防止用户运行他们不应该被允许访问的命令。 这里的所有方向都假设用户有他们自己的/home/[username]目录,他们的shell是/bin/bash ,你希望他们在登录到系统时使用bash shell。

1)更改目录权限,以便只有用户可以编辑其主目录的内容

chmod 755 /home/[username]

2)删除用户的.bashrc文件

rm /home/[username]/.bashrc 这个网站有更多的信息,为什么在这种情况下删除.bashrc是一个好主意。

3)创建一个.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命令的完整列表以获取更多信息。 您必须确保alias alias="printf ''"命令是列表中的最后一个命令,否则您将失去所有这些命令的别名能力。

4)通过将vi命令别名到禁止模式来禁用vi中的shell命令

语法是alias vi="vi -Z" ,但请参阅本网站以获取更多信息。

5)将用户的.bash_profile的所有权更改为root

chown root:root /home/[username]/.bash_profile

6)删除用户的.bash_profile写入权限

chmod 755 /home/[username/.bash_profile]

7)最后,将用户bash更改为受限制的bash模式,以便它们不能更改目录(如果您的系统上没有受限制的bash模式,则此链接将有所帮助并提供更多信息)

chsh -s /bin/rbash [username]

现在,当用户登录时,他们将不能改变目录,所有你不希望他们使用的命令将输出相同的信息,就像用户没有指定命令时按[ENTER]键一样,你的/bin/bash函数保持不变。

根据您选择或不以这种方式别名的功能,用户仍然可以绕过您实施的一些控制。 但是,由于我们实施了一些安全缓冲器,用户将不得不知道计算机系统做任何危险的事情。

在一个相关的说明和你可能想要考虑的事情上,如果你直接把这些别名放到每个用户的.bash_profile你将难以维护哪些函数应该被别名化,如果你需要改变别名对任何事情你都不得不单独改变他们。 另外,由于用户可以使用vim或vi来查看文件,所以他们可以看到他们的.bash_profile的内容,并理解他们拥有和没有的限制。

为了解决这个问题,我会建议。

1)将所有别名放在用户不可访问的目录中(在此处粘贴.bash_profile的内容)

/[path_to_file]/startup_functions_for_beginners.sh

2)将别名发送到他们的.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和bg ,但我不希望他们能够访问aptitude或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

根据哈佛网站的常用命令清单(不完整)

谨慎的编辑应该注意,因为有些允许在程序内部执行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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值