限制SSH用户访问Linux中指定的目录

http://os.51cto.com/art/201703/534895.htm#topx

http://www.cnblogs.com/lykyl/archive/2011/03/10/1980304.html

采用ChrootDirectory设置需要注意
ChrootDirectory设置的目录及其所有的上级目录属主必须是root且只有属主能拥有写权限,也就是说权限最大设置只能是755。否则会报“fatal: bad ownership or modes for chroot directory”这样的错误。
用户目录下的链接文件失去作用。

useradd readonlyuser;echo -e "123456\n123456\n" |passwd  readonlyuser

mkdir -p /ngbs/readonlyuser/dev/  
cd /ngbs/readonlyuser/dev/
mknod -m 666 null c 1 3
mknod -m 666 tty c 5 0
mknod -m 666 zero c 1 5
mknod -m 666 random c 1 8

chown root:root /ngbs/readonlyuser
chmod 0755 /ngbs/readonlyuser

mkdir -p /ngbs/readonlyuser/bin
cp  /bin/bash /ngbs/readonlyuser/bin/  
mkdir -p /ngbs/readonlyuser/lib64/  
cp  /lib64/{libtinfo.so.5,libdl.so.2,libc.so.6,ld-linux-x86-64.so.2} /ngbs/readonlyuser/lib64/

mkdir /ngbs/readonlyuser/etc
cp -f /etc/{passwd,group} /ngbs/readonlyuser/etc/  
tail -1 /ngbs/readonlyuser/etc/passwd  >/tmp/1.txt ;cat /tmp/1.txt  > /ngbs/readonlyuser/etc/passwd

注意:每次向系统添加更多 SSH 用户时,都需要将更新的帐户文件复制到 /ngbs/readonlyuser/etc 目录中。

vi /etc/ssh/sshd_config
在此文件中添加或修改下面这些行。
# override default of no subsystems
Subsystem    ssh        /usr/bin/ssh  #这里改为ssh

# Example of overriding settings on a per-user basis
Match User readonlyuser  改为Match User readonlyuser
Match User readonlyuser 下面加上这一行 ChrootDirectory /ngbs/readonlyuser

也就是下面这样
# override default of no subsystems
Subsystem        ssh        /usr/bin/ssh

# Example of overriding settings on a per-user basis
Match User readonlyuser
ChrootDirectory /ngbs/readonlyuser
#       X11Forwarding no
#       AllowTcpForwarding no

重启 sshd 服务:
/etc/init.d/sshd restart

接下来,在 bin 目录中安装几个用户命令
cp /bin/ls /ngbs/readonlyuser/bin/  
cp /bin/cat  /ngbs/readonlyuser/bin/
cp /bin/more  /ngbs/readonlyuser/bin/
cp /usr/bin/less  /ngbs/readonlyuser/bin/
cp /usr/bin/head  /ngbs/readonlyuser/bin/
cp /usr/bin/tail  /ngbs/readonlyuser/bin/

ldd /bin/ls
ldd /bin/cat
ldd /bin/more
ldd /usr/bin/less
ldd /usr/bin/head
ldd /usr/bin/tail

/bin/cp  /lib64/{libselinux.so.1,librt.so.1,libcap.so.2,libacl.so.1,libc.so.6,libdl.so.2,ld-linux-x86-64.so.2,libpthread.so.0,libattr.so.1,libtinfo.so.5,libpcre.so.0}  /ngbs/readonlyuser/lib64/

测试 SSH 的 chroot 监狱
ssh readonlyuser@192.168.0.10

*/1 * * * *   /usr/bin/rsync -a /ngbs/local/JmCash/log/*   /ngbs/readonlyuser/log/
*/1 * * * *  /usr/bin/rsync -a /ngbs/local/JmCash/logs/*   /ngbs/readonlyuser/logs/
*/1 * * * *   /usr/bin/rsync -a /ngbs/local/JmCash/phonelog/*   /ngbs/readonlyuser/phonelog/

注意:如果是目录外做一个软链接到目录内是不行的,会报错 no such file  or  directory
ln -s /data/download  /home/test


注意:sftp

30)外部子系统
 
我们可以配置一个外部的子系统,仅用于SSH-V2协议,一般这里使用sftp,如下:
Subsystem       sftp    /usr/libexec/openssh/sftp-server
如关闭该选项,将无法使用sftp.
我们看一下使用sftp的通讯过程,如下:
sftp -v 192.168.27.142        /*采用sftp的方式连接ssh服务端*/
Connecting to 192.168.27.142...
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 192.168.27.142 [192.168.27.142] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type 1
debug1: identity file /root/.ssh/id_dsa type 2
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host '192.168.27.142' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: gssapi-keyex,gssapi-with-mic,password,hostbased
debug1: Next authentication method: password
root@192.168.27.142's password: 
debug1: Authentication succeeded (password).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending subsystem: sftp         /*在这里启用了sftp子系统*/
sftp> 
 
我们看一下服务端的进程:
ps -ef|grep ssh
avahi     1133     1  0 03:08 ?        00:00:00 avahi-daemon: registering [ssh-server.local]
root      1718     1  0 03:14 ?        00:00:00 sshd: root@pts/0 
root      2005     1  0 03:50 ?        00:00:00 /usr/sbin/sshd
root      2023  2005  0 03:52 ?        00:00:00 sshd: root@notty 
root      2025  2023  0 03:52 ?        00:00:00 /usr/libexec/openssh/sftp-server
注:
我们看到服务端启用了sftp-server为sftp客户请求服务.
 

限制SSH用户访问Linux中指定的目录的更多相关文章

  1. 限制SSH远程登录用户仅能只读访问Linux中指定的目录

    资料参考:http://os.51cto.com/art/201703/534895.htm 背景需求: 在TOMCAT服务器上建立一个普通帐号log_user,只能查看TOMCAT日志,不能删改任何 ...

  2. [转]Linux中文件权限目录权限的意义及权限对文件目录的意义

    转自:http://www.jb51.net/article/77458.htm linux中目录与文件权限的意义 一.文件权限的意义 r:可以读这个文件的具体内容: w:可以编辑这个文件的内容,包括 ...

  3. windows怎么访问linux的samba共享目录

    windows怎么访问linux的samba共享目录 听语音 原创 | 浏览:6976 | 更新:2018-07-31 13:20 | 标签:LINUX WINDOWS 1 2 3 4 5 6 7 分 ...

  4. 通过window(Navicat)访问linux中的mysql数据库

    Centos安装Mysql数据库 查看我们的操作系统上是否已经安装了mysql数据库 [root@centos~]# rpm -qa | grep mysql // 这个命令就会查看该操作系统上是否已 ...

  5. VisualGDB系列9:配置VS直接通过SSH方式访问Linux项目

    根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何使用VS和VisualGDB ...

  6. 【转】完美解读Linux中文件系统的目录结构

    一.前 言 接触Linux也有一段时间了,不过这几天在编译开源程序时,才发现自己对linux文件系统的目录结构了解的不够透彻,很多重要目录都说不清楚是用来干嘛的,于是在网上百度了一下这方面的介绍,根据 ...

  7. 【转】linux中的常见目录及文件

    1. /proc目录 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...

  8. 在Linux中设置共享目录

    1.  是否安装了samba服务 sudo apt-get install samba sudo apt-get install smbfs 2.  创建需要共享的目录/ home/usr/share ...

  9. linux中的重要目录

    1./boot 引导程序,内核的存放的目录. 此目录,包含了在引导过程中所必须的文件,引导程序的相关文件(如:grub,lilo以及相应的配置文件及linux操作系统内核相关文件). 2./sbin/ ...

随机推荐

  1. 仿迅雷播放器教程 -- duilib界面(13)

    经过了这么多篇文章的讲解,相信大家也对界面库有一定了解了,用一个新的界面库,肯定要对它进行全方位考察.鉴于公司目前所有的产品都是MFC做的,全部转换成duilib肯定不现实,并且公司的很多项目逻辑和界 ...

  2. [Stats385] Lecture 04: Convnets from Probabilistic Perspective

    本篇围绕“深度渲染混合模型”展开. Lecture slices Lecture video Reading list A Probabilistic Framework for Deep Learn ...

  3. Using Information Fragments to Answer the Questions Developers Ask

    content : 1.采访了11个开发者,获得78个常问的问题:2.对78个问题进行分类,分为8类:These questions span eight domains of information ...

  4. 理解syslinux,SYSLINUX和PXELINUX

    在研究网络装机的过程中,菜菜地被Syslinux.SYSLINUX和PXELINUX这些定义折磨了一下 它们有什么区别和联系?为什么配置PXELINUX要安装的是Syslinux而不是Pxelinux ...

  5. 关于ICO的一些理解

    第一次看到ICO,估计很多人都处于懵逼的状态,感觉很抽象. 提到IOC可能想到的下一个词语就是DI IOC:控制反转 DI:依赖注入 那么什么是控制反转呢? 我以前对这个概念也很模糊,最近在知乎上看到 ...

  6. informix数据库触发器的写法

    虽然有各种数据库,但触发器的原理都是一样的,懂一种数据库的写法就可以了解其他的. 以前写过mysql数据库的触发器,这次写informix的,还顺带看了oracle的,除了语法上的不同,informi ...

  7. 主席树||可持久化线段树||离散化||[CQOI2015]任务查询系统||BZOJ 3932||Luogu P3168

    题目: [CQOI2015]任务查询系统 题解: 是一道很经典的题目.大体思路是抓优先级来当下标做主席树,用时刻作为主席树的版本.然而优先级范围到1e7去了,就离散化一遍.然后把每个事件的开始(s). ...

  8. imu_tk标定算法原理

    imu_tk代码地址 https://bitbucket.org/alberto_pretto/imu_tk II. S ENSOR E RROR M ODEL 对于理想的IMU,加速度计三元组的3个 ...

  9. c# 主窗体更新子窗体 进程间通信

    1.窗体间数据传输 主窗体连续不断更新给子窗体  本文章来源于网络 年代久远 如有侵犯 请联系删除 1.通过 在windows form之间传值,我总结了有四个方法:全局变量.属性.窗体构造函数和de ...

  10. vim保存只读文件时获得sudo权限

    有时使用vim时忘记输入sudo,导致编辑了一大堆文字,最后保存时发现是只读文件,无法保存,每次保存都会提示read only.这时可以使用如下命令代替原有的 :wq 命令 :w !sudo tee ...