有些特殊情况下需要实现将系统内普通用户限定在指定目录下,并且只能使用系统管理员设定的命令。lshell就是实现这样功能的一个神器。

lshell提供了一个针对每个用户可配置的限制性shell,lshell的配置文件非常的简单,可以和sshauthorized_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配置

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_pathpath注释掉则限制用户只能访问自己的家目录及其子目录。如果需要能访问其他目录,则需要在path中加入相应的目录,当前设置下用户可以访问家目录及其子目录,也可以访问/tmp目录及其子目录,但不能访问这以外的目录,比如/etc

allowed中添加我们限定用户所能使用的命令,这里限定只能使用lsechocdll四个命令。

测试登陆

$ 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环境的更多相关文章

  1. shell环境改变引起的命令提示符改变

    1. 故障现象与背景 1.1 背景 开发早上找我说root环境变得异常,跟平时不太一样.其他用户没有改变,就root用户发生变化 1.2故障现象 root用户命令提示符 :➜ ~ 命令行上命令提示符发 ...

  2. windows python3.2 shell环境(python叫做解释器)

    [进入python的shell 环境:](python里称作命令解释器,windows叫做cmd,unix叫做shell) cmd  输入set path=%path%;e:\python2.7然后输 ...

  3. 9. shell环境

    • printenv –打印部分或所有的环境变量 • set –设置 shell 选项 • export —导出环境变量,让随后执行的程序知道. • alias –创建命令别名 1.shell环境:s ...

  4. Linux命令之env:显示当前用户的环境变量

    Linux系统里的env命令可以显示当前用户的环境变量,还可以用来在指定环境变量下执行其他命令.下面来比较一下set,env和export命令的异同:set命令显示当前shell的变量,包括当前用户的 ...

  5. shell环境

    1 引言 一个进程运行在shell环境中,理解进程运行的环境是十分重要的.环境影响着进程的行为,利用环境提供的便利,可以极大地提高开发效率.本节深入讨论shell中与进程有关的环境问题,包括命令行参数 ...

  6. Linux为用户设定环境变量

    今天在做从将MySQL中的数据同步到ES的时候,当启动脚本程序报错: Exception in thread "main" java.lang.UnsupportedClassVe ...

  7. Cygwin 是一个用于 Windows 的类 UNIX shell 环境

    cygwin的安装使用   Cygwin 是一个用于 Windows 的类 UNIX shell 环境. 它由两个组件组成:一个 UNIX API 库,它模拟 UNIX 操作系统提供的许多特性:以及 ...

  8. shell 环境变量的相关配置文件和配置方法

    shell 环境变量的相关配置文件和配置方法: bash 的配置文件: 全局配置: /etc/profile, /etc/profile.d/*.sh, /etc/bashrc 个人配置 ~/.bas ...

  9. Linux编程 12 (默认shell环境变量, PATH变量重要讲解)

    一 .概述 默认情况下, bash shell会用一些特定的环境变量来定义系统的环境.这些默认环境变量可以理解是上篇所讲的系统全局环境变量. 1.1 bash  shell支持的Bourne变量 Bo ...

随机推荐

  1. ChemDraw Pro绘制无环链结构的两种方法

    ChemDraw Pro 14是一款专门针对化学图形绘制而开发制作的编辑软件,是目前工科类常用的绘制化学结构工具,用于快速绘制常用的环结构组成.以下教程讲解ChemDraw Pro绘制无环链结构的两种 ...

  2. vue+webpack2实现路由的懒加载

    当打包构建应用时,Javascript 包会变得非常大,影响页面加载.如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了. 结合 Vue 的异步组 ...

  3. Django学习笔记第十篇--实战练习六--发送邮件

    一.发送邮件需要引入的包依赖文件(Django1.8 Python2.7) from django.core.mail import send_mail,send_mass_mail 其中send_m ...

  4. 160503、onunload、onbeforeunload事件详解

    最近项目中做到一个功能:在上传页面用户开始上传文件之后用户点击任意跳转都需要弹出提示层进行二次确定才允许他进行跳转,这样做的目的是为了防止用户的错误操作导致这珍贵的UGC 流失(通常用户在一次上传不成 ...

  5. web测试点--摘录

    转载地址:Web测试到底是在测什么 一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&am ...

  6. Code Force 21B Intersection

    B. Intersection time limit per test1 second memory limit per test256 megabytes inputstandard input o ...

  7. content: "\e600"

    w图标生成原理. <style> @font-face { font-family: iconfont-sm; src: url(//at.alicdn.com/t/font_143340 ...

  8. Spark源码分析 – Deploy

    参考, Spark源码分析之-deploy模块   Client Client在SparkDeploySchedulerBackend被start的时候, 被创建, 代表一个application和s ...

  9. 【转】Keepalived无法绑定VIP故障排查经历

    一 故障描述 我在台湾合作方给定的两台虚拟机上部署HAProxy+Keepalived负载均衡高可用方案.在配置完Keepalived后,重新启动Keepalived,Keepalived没有绑定VI ...

  10. python基础-第七篇-7.2面向对象(进阶篇)

    进入到今天的探索前,我先对上节内容进行一下回顾: 面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用 类是一个模板,模板中包装了多个函数可供使用 对象是基于类创建的,实例用于调用被包装在类 ...