受限的用户shell环境
有些特殊情况下需要实现将系统内普通用户限定在指定目录下,并且只能使用系统管理员设定的命令。lshell就是实现这样功能的一个神器。
lshell提供了一个针对每个用户可配置的限制性shell,lshell的配置文件非常的简单,可以和ssh
的authorized_keys
或者/etc/shell
、/etc/passwd
耦合使用,lshell可以很容易的严格限制用户可以访问哪些命令。
项目地址: https://github.com/ghantoos/lshell
lshell安装
- $ yum install lshell #EPEL源
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 --help
- 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)

- lshell的配置文件详解
配置文件一共有四个小节
[global] -lshell的系统配置(只能有一个)
[default] -lshell的默认用户配置(只能有一个)
[foo] -指定UNIX的系统用户”foo”的特别的配置
[grp:bar] -指定UNIX用户组”bar”的特别的配置
当加载参数的时候遵循以下顺序
1.User configuration
2.Group configuration
3.Default configuration
logpath
日志路径(默认是/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
更新用户的环境变量PATH
env_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 -hostname
scp
允许或者禁止使用scp连接(0禁止、1允许)。
scpforce
强制文件通过scp传输到一个特定目录
scp_download
允许或者禁止使用scp下载(0禁止、1允许)。
scp_upload
允许或者禁止使用scp上传(0禁止、1允许,默认为1)。
sftp
允许或者禁止使用sftp连接(0禁止、1允许)。
sudo_commands
一组命令组成的列表,用户可以执行sudo
timer
会话维持的秒数
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 test@127.0.0.1
- test@127.0.0.1'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
转载自:http://www.ttlsa.com/safe/restricted-user-shell-environment/
受限的用户shell环境的更多相关文章
- shell环境改变引起的命令提示符改变
1. 故障现象与背景 1.1 背景 开发早上找我说root环境变得异常,跟平时不太一样.其他用户没有改变,就root用户发生变化 1.2故障现象 root用户命令提示符 :➜ ~ 命令行上命令提示符发 ...
- windows python3.2 shell环境(python叫做解释器)
[进入python的shell 环境:](python里称作命令解释器,windows叫做cmd,unix叫做shell) cmd 输入set path=%path%;e:\python2.7然后输 ...
- 9. shell环境
• printenv –打印部分或所有的环境变量 • set –设置 shell 选项 • export —导出环境变量,让随后执行的程序知道. • alias –创建命令别名 1.shell环境:s ...
- Linux命令之env:显示当前用户的环境变量
Linux系统里的env命令可以显示当前用户的环境变量,还可以用来在指定环境变量下执行其他命令.下面来比较一下set,env和export命令的异同:set命令显示当前shell的变量,包括当前用户的 ...
- shell环境
1 引言 一个进程运行在shell环境中,理解进程运行的环境是十分重要的.环境影响着进程的行为,利用环境提供的便利,可以极大地提高开发效率.本节深入讨论shell中与进程有关的环境问题,包括命令行参数 ...
- Linux为用户设定环境变量
今天在做从将MySQL中的数据同步到ES的时候,当启动脚本程序报错: Exception in thread "main" java.lang.UnsupportedClassVe ...
- Cygwin 是一个用于 Windows 的类 UNIX shell 环境
cygwin的安装使用 Cygwin 是一个用于 Windows 的类 UNIX shell 环境. 它由两个组件组成:一个 UNIX API 库,它模拟 UNIX 操作系统提供的许多特性:以及 ...
- shell 环境变量的相关配置文件和配置方法
shell 环境变量的相关配置文件和配置方法: bash 的配置文件: 全局配置: /etc/profile, /etc/profile.d/*.sh, /etc/bashrc 个人配置 ~/.bas ...
- Linux编程 12 (默认shell环境变量, PATH变量重要讲解)
一 .概述 默认情况下, bash shell会用一些特定的环境变量来定义系统的环境.这些默认环境变量可以理解是上篇所讲的系统全局环境变量. 1.1 bash shell支持的Bourne变量 Bo ...
随机推荐
- 修改Apache访问权限
You don't have permission to access / on this server.错误,居然说我此台服务器上无权限,ok解决办法如下: 找到:apache文件,进入conf文件 ...
- hdu 2821(dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2821 思路:一开始的时候没注意到,必须从map[i][j]==0的位置开始,然后就是dfs了,回溯的时 ...
- sublime window 配置记录 (转)
大家好,今天给大家分享一款编辑器:sublime text2 我用过很多编辑器,EditPlus.EmEditor.Notepad++.Notepad2.UltraEdit.Editra.Vim ...
- ios应用, 设置不自己主动备份到iCloud
原创文章,转载请注明出处 ios项目,假设有内置下载或者程序动态生成文件的话,就要注意所下载或生成的文件,要不要自己主动备份到iCloud 假设没有合适的理由就自己主动上传大文件的话,可能在不能通过应 ...
- UE4关于编译配置的参考(Debug,DebugGame,Development,Shipping,Test等)
https://docs.unrealengine.com/latest/CHN/Programming/Development/BuildConfigurations/index.html 编译配置 ...
- 数据库为什么要用B+树结构--MySQL索引结构的实现(转)
B+树在数据库中的应用 { 为什么使用B+树?言简意赅,就是因为: 1.文件很大,不可能全部存储在内存中,故要存储到磁盘上 2.索引的结构组织要尽量减少查找过程中磁盘I/O的存取次数(为什么使用B-/ ...
- oracle的存储过程如何返回结果集
CREATE OR REPLACE PACKAGE pkg_test AS TYPE myrctype IS REF CURSOR; PROCEDURE get (p_id NUM ...
- HDU 3450 Counting Sequences(线段树)
Counting Sequences Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Other ...
- Cookies Client Identification
HTTP The Definitive Guide Cookies are the best current way to identify users and allow persistent se ...
- 剑指Offer——数组中重复的数字
题目描述: 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度 ...