linux 给用户某个命令,如何在Linux中限制用户命令

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

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

1)将用户的bash更改为受限制的bash模式,以便他们无法更改目录(如果您的系统上没有受限制的bash模式,this link将帮助并提供更多信息)

chsh -s / bin / rbash [用户名]

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

chmod 755 / home / [用户名]

3)删除用户的.bashrc文件

rm /home/[username]/.bashrc This site提供了更多信息,说明为什么在这种情况下删除.bashrc可能是个好主意.

4)创建.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 ''"

请查看the full list of bash commands获取更多信息.您必须确保alias alias =“printf””命令是列表中的最后一个命令,否则您将失去对所有这些命令进行别名的能力.

注意

运行以下命令将搜索系统上几乎所有可用的命令,并输出现成的文件,几乎所有可用的命令都会预先设置别名. [命令是bash中的test命令.因此,如果您在文件中看到它,那不是错误.

#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 <<

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

5)通过将vi命令别名化为限制模式来禁用vi中的shell命令

语法为别名vi =“vi -Z”,但请参阅this site以获取更多信息.

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

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

7)最后,删除用户的.bash_profile的写权限

chmod 755 /home/[username/.bash_profile]

现在,当用户登录时,他们将无法更改目录,您不希望他们使用的所有命令将输出相同的信息,就像用户在没有指定命令的情况下按下[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

根据此Harvard Website(非执行)的常用命令列表

当您将程序安装到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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值