有些特殊情況下需要實現將系統內普通用戶限定在指定目錄下,並且只能使用系統管理員設定的命令。lshell就是實現這樣功能的一個神器。
lshell提供了一個針對每個用戶可配置的限制性shell,lshell的配置文件非常的簡單,可以和ssh
的authorized_keys
或者/etc/shell
、/etc/passwd
耦合使用,lshell可以很容易的嚴格限制用戶可以訪問哪些命令。
項目地址: https://github.com/ghantoos/lshell
lshell安裝
RHEL、CentOS
$ yum install lshell #EPEL源
Debian、Ubuntu
$ apt-get install lshell
lshell使用
lshell語法格式
$ lshell --help
Usage: lshell [OPTIONS]
--config <file> : Config file location (default /etc/lshell.conf) #指定配置文件
--log <dir> : Log files directory #指定日誌目錄
-h, --help : Show this help message #顯示幫助信息
--version : Show version #顯示版本信息
lshell配置
Linux下配置文件爲/etc/lshell.conf
# lshell.py configuration file
#
# $Id: lshell.conf,v 1.27 2010/10/18 19:05:17 ghantoos Exp $
[global]
## log directory (default /var/log/lshell/ )
logpath : /var/log/lshell/
## set log level to 0, 1, 2, 3 or 4 (0: no logs, 1: least verbose,
## 4: log all commands)
loglevel : 2
## configure log file name (default is %u i.e. username.log)
#logfilename : %y%m%d-%u
#logfilename : syslog
## in case you are using syslog, you can choose your logname
#syslogname : myapp
[default]
## a list of the allowed commands or 'all' to allow all commands in user's PATH
allowed : ['ls','echo','cd','ll']
## a list of forbidden character or commands
forbidden : [';', '&', '|','`','>','<', '$(', '${']
## a list of allowed command to use with sudo(8)
#sudo_commands : ['ls', 'more']
## number of warnings when user enters a forbidden value before getting
## exited from lshell, set to -1 to disable.
warning_counter : 2
## command aliases list (similar to bash’s alias directive)
aliases : {'ll':'ls -l', 'vi':'vim'}
## introduction text to print (when entering lshell)
#intro : "== My personal intro ==\nWelcome to lshell\nType '?' or 'help' to get the list of allowed commands"
## configure your promt using %u or %h (default: username)
#prompt : "%u@%h"
## a value in seconds for the session timer
#timer : 5
## list of path to restrict the user "geographicaly"
#path : ['/home/bla/','/etc']
## set the home folder of your user. If not specified the home_path is set to
## the $HOME environment variable
#home_path : '/home/bla/'
## update the environment variable $PATH of the user
#env_path : ':/usr/local/bin:/usr/sbin'
## add environment variables
#env_vars : {'foo':1, 'bar':'helloworld'}
## allow or forbid the use of scp (set to 1 or 0)
#scp : 1
## forbid scp upload
#scp_upload : 0
## forbid scp download
#scp_download : 0
## allow of forbid the use of sftp (set to 1 or 0)
#sftp : 1
## list of command allowed to execute over ssh (e.g. rsync, rdiff-backup, etc.)
#overssh : ['ls', 'rsync']
## logging strictness. If set to 1, any unknown command is considered as
## forbidden, and user's warning counter is decreased. If set to 0, command is
## considered as unknown, and user is only warned (i.e. *** unknown synthax)
#strict : 1
## force files sent through scp to a specific directory
#scpforce : '/home/bla/uploads/'
## history file maximum size
#history_size : 100
## set history file name (default is /home/%u/.lhistory)
#history_file : "/home/%u/.lshell_history"
lshell的配置文件詳解
配置文件一共有四個小節
[global] -lshell的系統配置(只能有一個)
[default] -lshell的默認用戶配置(只能有一個)
[foo] -指定UNIX的系統用戶”foo”的特別的配置
[grp:bar] -指定UNIX用戶組”bar”的特別的配置當加載參數的時候遵循以下順序
1.User configuration
2.Group configuration
3.Default configurationlogpath
日誌路徑(默認是/var/log/lshell/)loglevel
日誌記錄級別,0, 1, 2, 3 or 4 (0: no logs -4: logs everything)logfilename
如果設置成syslog關鍵字,則表示日誌記錄到syslog中
如果設置成一個文件名, e.g. %u-%y%m%d (i.e foo-20091009.log):%u -username
%d -day [1..31]
%m -month [1..12]
%y -year [00..99]
%h -time [00:00..23:59]syslogname
如果你打算記錄進syslog中,則要設置你的syslog名稱,默認是lshell[default]或者[username]或者[grp:groupname] 三個小節可用的配置項
aliases
命令別名allowed
一個允許執行的命令列表,或者設置成all,則允許在user PATH中的所有命令可用allowed_cmd_path
一個路徑組成的列表,所有在路徑中的可執行文件都被允許env_path
更新用戶的環境變量PATHenv_vars
設置用戶的環境變量forbidden
一個非法字符或者命令組成的列表history_file
history的文件名,%u -username (e.g. ‘/home/%u/.lhistory’)history_size
history文件記錄的maximum size(in lines)home_path (deprecated)
默認是$HOME,不贊成使用,下一版會取消。%u -username (e.g. ‘/home/%u’)intro
在登陸時打印出入門信息login_script
用戶登陸時執行的腳本passwd
指定用戶的密碼(默認爲空)path
嚴格限制用戶可以去的系統路徑,可以使用通配符(e.g. ‘/var/log/ap*’)prompt
設置用戶的prompt格式(default: username)
%u -username
%h -hostnamescp
允許或者禁止使用scp連接(0禁止、1允許)。scpforce
強制文件通過scp傳輸到一個特定目錄scp_download
允許或者禁止使用scp下載(0禁止、1允許)。scp_upload
允許或者禁止使用scp上傳(0禁止、1允許,默認爲1)。sftp
允許或者禁止使用sftp連接(0禁止、1允許)。sudo_commands
一組命令組成的列表,用戶可以執行sudotimer
會話維持的秒數strict
日誌嚴格記錄,如果設置成1,任何unknow的命令都被禁止,並且降低用戶警告數,如果設置成0,unknow命令只是警告。 (i.e. * unknown synthax)warning_counter
警告次數,如果用戶達到該警告次數,則會被強制退出lshell,設置成-1,則禁止計數。
lshell下始終可使用的指令
清屏
clear
打印可用命令
help, ?
打印命令歷史
history
列出所有允許和禁止的路徑
lpath
列出所有允許sudo的命令
lsudo
lshell實例
爲了記錄用戶日誌,首先需要創建相關目錄
$ groupadd --system lshell
$ mkdir /var/log/lshell
$ chown :lshell /var/log/lshell
$ chmod 770 /var/log/lshell
添加test用戶
$ useradd test -d /home/test -s /usr/bin/lshell
然後增加test用戶到lshell group
$ usermod -aG lshell test
改變test用戶默認shell,使用lshell作爲默認shell
$ chsh -s /usr/bin/lshell test
修改配置文件讓test用戶只能使用受限命令
[test]
allowed : ['ls','echo','cd','ll'] ##允許使用的命令
home_path : '/home/test' ##設置用戶的家目錄
path : ['/home/test','/tmp'] ##限制用戶的目錄
home_path
和path
註釋掉則限制用戶只能訪問自己的家目錄及其子目錄。如果需要能訪問其他目錄,則需要在path中加入相應的目錄,當前設置下用戶可以訪問家目錄及其子目錄,也可以訪問/tmp
目錄及其子目錄,但不能訪問這以外的目錄,比如/etc
。
allowed
中添加我們限定用戶所能使用的命令,這裏限定只能使用ls
、echo
、cd
、ll
四個命令。
測試登陸
$ ssh [email protected]
[email protected]'s password:
You are in a limited shell.
Type '?' or 'help' to get the list of allowed commands
test:~$
命令使用
test:~$ cd /etc
*** forbidden path -> "/etc/"
*** You have 1 warning(s) left, before getting kicked out.
This incident has been reported.
test:~$ touch test.txt
*** unknown command: touch