好多的程序,都有使用chroot来是程序chroot到一个目录下面,来保护文件系统,今天在看snort代码的时候,看到了实现,就贴出一个测试程序来,实际上是比较简单的。

    chroot()在linux下面需要使用root权限,这一点需要注意了。

   

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int main(void)
{
    char chroot_path[] = "/tmp";
    char *pwd;
    int ret;

    /*chroot 需要root权限*/
    ret = chroot(chroot_path);
    if (ret != 0) {
        perror("chroot:");
        exit(-1);
    }
    pwd = getcwd(NULL, 80);
    printf("After chroot,getcwd is [%s]\n",pwd);
    free(pwd);

    /*可以建立/tmp/test,测试一下是否可以改变目录 /tmp/test <==> /test*/
    ret = chdir("/test");
    if( ret < 0 )
    {
            perror("chdir /test");
            //exit(-1);
    }
    else
         /*由于chroot之后,当前工作目录还没有改变,所以需要再调用chdir来改变到/目录*/
         if( chdir("/") < 0 )
         {
                 perror("chdir /");
                 exit(-1);
         }

    pwd = getcwd(NULL, 80);
    printf("After chdir /,getcwd is [%s]\n",pwd);
    free(pwd);

    return 0;
}

以普通用户的身份运行程序:
wangyao@fisherman:~$ ./a.out
chroot:: Operation not permitted
以root用户运行,刚开始/tmp下面没有test目录:
fisherman:/home/wangyao# ./a.out
After chroot,getcwd is [/home/wangyao]
chdir /test: No such file or directory
After chdir /,getcwd is [/home/wangyao]
fisherman:/home/wangyao# mkdir -p /tmp/test
fisherman:/home/wangyao# ./a.out
After chroot,getcwd is [/home/wangyao]
After chdir /,getcwd is [/]
fisherman:/home/wangyao#

chroot()执行成功之后,根目录是进行了切换,但是当前工作目录没有变化,还是chroot()之前的,需要再一次调用chdir()来实现更改当前工作目录,如果chdir失败,当前工作目录不变。

snort中的一段代码:

   /* Don't need root privs anymore, so lets drop ownership
     * and chroot if requested....
     */

    if(chrootdir != NULL)
    {
        if(chdir(chrootdir) < 0)
            FatalError("Can not chdir to \"%s\"\n", chrootdir);
        if(chroot(chrootdir) < 0)
            FatalError("Can not chroot to %s\n", chrootdir);
        if(chdir("/") < 0)
            FatalError("Can not chdir to \"/\"\n");

        free(chrootdir);
        chrootdir = NULL;        /* we don't need chrootdir anymore so all
                                  * other routines should use fullpath. */
    }

以后写程序的时候,尤其是网络程序的时候,可以加上这个特性 :-)

chroot()使用的更多相关文章

  1. vsftpd:500 OOPS: vsftpd: refusing to run with writable root inside chroot ()错误的解决方法

    ---恢复内容开始--- 最近在安装了vsftpd后 添加了虚拟账户后 新建用户 为新用户创立独立的工作目录 因为虚拟用户在工作目录需要上传文件 所以必须拥有此目录的W权限,但每当给此目录加上W权限后 ...

  2. 解决vsftpd的refusing to run with writable root inside chroot错误

    参考 http://www.cnblogs.com/CSGrandeur/p/3754126.html 在Ubuntu下用 vsftpd 配置FTP服务器,配置 “ sudo chmod a-w /h ...

  3. livecd环境下chroot修复系统

    今天想升级centos5.7的glibc版本,想当然的把新编译的glibc的libc-2.7.so 复制到/lib64/libc-2.5.so lrwxrwxrwx root root Mar : / ...

  4. Linux chroot 并使用之前系统设备节点

    /********************************************************************************* * Linux chroot 并使 ...

  5. chroot directory

    给 /A/B/C的C目录做chroot,要对C能读写,所以C目录不能做ROOT目录,对B做chroot. 设置C目录所有者为sftp 账户a,组也改为sftp组(这里a和sftp组都是之前建立好的sf ...

  6. 使用jailkit chroot更改ssh用户根目录

    安装jailkit cd /tmp    wget http://olivier.sessink.nl/jailkit/jailkit-2.16.tar.gz    tar xzf jailkit-2 ...

  7. Linux 容器技术史话:从 chroot 到未来

    Linux 容器是一个在单一 Linux 主机上提供多个隔离的 Linux 环境的操作系统级虚拟技术.不像虚拟机(VM),容器并不需要运行专用的访客(guest)操作系统.容器们共享宿主机的(host ...

  8. 制作具有SSH、MySQL功能的Chroot

    由于工作需求,需要在Linux上建立SSH.MySQL两个用户. 使这两个账户连接到跳板机后仅能执行有限的命令(SSH用户只能执行SSH命令,MySQL用户只能执行MySQL命令). MySQL账户C ...

  9. 500 OOPS: vsftpd: refusing to run with writable root inside chroot()

    Ubuntu 12.04 64bit系统下安装的vsftpd,在登陆时提示500 OOPS: vsftpd: refusing to run with writable root inside chr ...

  10. 如何在 Ubuntu 14.04 里面配置 chroot 环境

    你可能会有很多理由想要把一个应用.一个用户或者一个环境与你的 Linux 系统隔离开来.不同的操作系统有不同的实现方式,而在 Linux 中,一个典型的方式就是 chroot 环境. 在这份教程中,我 ...

随机推荐

  1. 多次读取HttpEntity内容

    有时,需要重复读取HttpEntity,直接使用是行不通的,这时需要使用BufferedHttpEntity类将其进行包装一下. public static void main(String[] ar ...

  2. JS中算法之检索算法(查找算法)

    顺序查找 查找指定值 function seqSearch(arr, data) { for (var i = 0; i < arr.length; ++i) { if (arr[i] == d ...

  3. JS中数据结构之列表

    列表是一组有序的数据.每个列表中的数据项称为元素.在 JavaScript 中,列表中的元素可以是任意数据类型.列表中可以保存多少元素并没有事先限定并可以不断壮大,实际使用时元素的数量受到程序内存的限 ...

  4. Majordomo Info VGER.KERNEL.ORG

    This is VGER.KERNEL.ORG Majordomo Info The mission of vger.kernel.org is to provide email list servi ...

  5. QString的arg方法

    第一个参数是要填充的数字,第二个参数为最小宽度,第三个参数为进制,第四个参数为当原始数字长度不足最小宽度时用于填充的字符,如 QString name=QString("R%1C%2&quo ...

  6. error C2065: “CString”: 未声明的标识符 ;fatal error C1189: #error : afxstr.h can only be used in MFC proje

    转自VC错误:http://www.vcerror.com/?p=1388 问题描述: error C2065: "CString": 未声明的标识符 fatal error C1 ...

  7. jmeter添加自定义扩展函数之大写转换小写

    1,打开eclipse,新建maven工程,在pom中引用jmeter核心jar包,具体请看---https://www.cnblogs.com/guanyf/p/10863033.html---,这 ...

  8. python的三种创建字典的方法

    #创建一个空字典 empty_dict = dict() print(empty_dict) #用**kwargs可变参数传入关键字创建字典 a = dict(one=,two=,three=) pr ...

  9. 微服务-技术专区-监控专区(Skywalking与Pinpoint) - 监控对比分析

    由于公司目前有200多微服务,微服务之间的调用关系错综复杂,调用关系人工维护基本不可能实现,需要调研一套全链路追踪方案,初步调研之后选取了skywalking和pinpoint进行对比; 选取skyw ...

  10. CentOS 7 下配置 Nginx + PHP7.1 + MariaDB 以及 Laravel 框架 2018.3.11

    CentOS 7 下配置 Nginx + PHP7.1 + MariaDB 以及 Laravel 框架 阿里云服务器的选择 当然是选择学生优惠啦.这里阿里云还提供了轻量级服务器这个选项,可以预装 LA ...