好多的程序,都有使用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. Linux任务计划at

    Linux任务计划at 一Linux任务计划介绍 Linux任务计划.周期性任务执行at:未来的某时间点执行一次任务batch:系统自行选择空闲时间去执行此处指定的任务cron:周期性运行某任务 二a ...

  2. Spring学习总结(2)- AOP

    一,什么是AOP AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中 ...

  3. 【Linux】grub引导修复

    将系统盘挂载上并设置开机从光盘启动(U盘也可以) 进入系统安装引导初始界面,然后选择最后一项Troubleshooting 然后选择第二项Rescue a CentOS system进入系统救援模式选 ...

  4. TCP/IP协议 和 如何实现 互联网上点对点的通信

    1.参考:https://www.cnblogs.com/onepixel/p/7092302.html   TCP/IP 协议采用4层结构,分别是应用层.传输层.网络层 和 链路层   http 属 ...

  5. (转)JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解

    转:https://my.oschina.net/feichexia/blog/196575?p=3 现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不 ...

  6. webform将一个usercontrol作为模态框在page上弹出

    弹窗 public static void RegisterJQueryDialogScript(Page page, string dialogDivId, string title, int wi ...

  7. soj#547 bzoj5046 分糖果游戏

    分析 代码 #include<bits/stdc++.h> using namespace std; #define int long long ; ][],s[],p[],v[]; si ...

  8. sqlserver数据库里怎样设置datetime类型的小数位数为3位

    要用datetime2(7)这种类型!将7改为3就行了

  9. centos7运行yum报如下提示:Run "yum repolist all" to see the repos you have

    centos7运行yum报如下提示: There are no enabled repos. Run "yum repolist all" to see the repos you ...

  10. SQL 批量插入数据

    后面进行完善修改. /*批量插入数据*/ 这个比较完善.直接插入数据库表. INSERT INTO `goods_transverter` ( `code`,`es_id`,`barcode`, `n ...