限制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. 阿里云oss上传文件如何支持https?

    let client = new OSS.Wrapper({ accessKeyId: res.data.accessKeyId, accessKeySecret: res.data.accessKe ...

  2. java导出json格式文件

    生成json文件代码: import java.io.File; import java.io.FileWriter; import java.io.Writer; public class Crea ...

  3. 编译你的第一个Java虚拟机--Centos 7 编译openJdk1.7源码

    一.前言 最近在看<深入java虚拟机>,看完后,打算自己实际编译一个jvm出来看看,实践一下. 书上提到了Oracle JDK和OpenJdk的关系,Oracle Jdk7 和OpenJ ...

  4. python-Levenshtein几个计算字串相似度的函数解析

    linux环境下,没有首先安装python_Levenshtein,用法如下: 重点介绍几个该包中的几个计算字串相似度的几个函数实现. 1. Levenshtein.hamming(str1, str ...

  5. vue 数据管道

    文档https://cn.vuejs.org/v2/guide/filters.html html 片段 <div class="app"> <div>{{ ...

  6. LogisticRegression 和 LogisticRegressionCV

    在scikit-learn中,与逻辑回归有关的主要是这3个类.LogisticRegression, LogisticRegressionCV 和logistic_regression_path.其中 ...

  7. GEM5安装

    转自:http://blog.sina.com.cn/s/blog_548b0a230101cagk.html GEM5在ubuntu13.10下安装过程,首先下载gem5_stable最新安装包,解 ...

  8. Runstats,Reorgchk,Reorg,Rebind

    Runstats:收集统计信息,为DB2优化器提供最佳路径选择.runstats命令只能针对单表执行.命令格式:db2 runstats on table schema.tabnamerunstats ...

  9. 部署不能产生class文件的问题

    项目clean和重新部署项目之后,还是不能产生class文件:查看“Problem”视图,是lib路径有问题,右击项目→“Build Path”→“Configure Build Path”,Libr ...

  10. iOS 问答时间

    runloop 的 model作用是什么? 答案: model 主要是用来指定事件在运行循环中的优先级,分为: NSDefaultRunLoopMode(kCFRunLoopDefaultMode): ...