chroot()使用
好多的程序,都有使用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()使用的更多相关文章
- vsftpd:500 OOPS: vsftpd: refusing to run with writable root inside chroot ()错误的解决方法
---恢复内容开始--- 最近在安装了vsftpd后 添加了虚拟账户后 新建用户 为新用户创立独立的工作目录 因为虚拟用户在工作目录需要上传文件 所以必须拥有此目录的W权限,但每当给此目录加上W权限后 ...
- 解决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 ...
- livecd环境下chroot修复系统
今天想升级centos5.7的glibc版本,想当然的把新编译的glibc的libc-2.7.so 复制到/lib64/libc-2.5.so lrwxrwxrwx root root Mar : / ...
- Linux chroot 并使用之前系统设备节点
/********************************************************************************* * Linux chroot 并使 ...
- chroot directory
给 /A/B/C的C目录做chroot,要对C能读写,所以C目录不能做ROOT目录,对B做chroot. 设置C目录所有者为sftp 账户a,组也改为sftp组(这里a和sftp组都是之前建立好的sf ...
- 使用jailkit chroot更改ssh用户根目录
安装jailkit cd /tmp wget http://olivier.sessink.nl/jailkit/jailkit-2.16.tar.gz tar xzf jailkit-2 ...
- Linux 容器技术史话:从 chroot 到未来
Linux 容器是一个在单一 Linux 主机上提供多个隔离的 Linux 环境的操作系统级虚拟技术.不像虚拟机(VM),容器并不需要运行专用的访客(guest)操作系统.容器们共享宿主机的(host ...
- 制作具有SSH、MySQL功能的Chroot
由于工作需求,需要在Linux上建立SSH.MySQL两个用户. 使这两个账户连接到跳板机后仅能执行有限的命令(SSH用户只能执行SSH命令,MySQL用户只能执行MySQL命令). MySQL账户C ...
- 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 ...
- 如何在 Ubuntu 14.04 里面配置 chroot 环境
你可能会有很多理由想要把一个应用.一个用户或者一个环境与你的 Linux 系统隔离开来.不同的操作系统有不同的实现方式,而在 Linux 中,一个典型的方式就是 chroot 环境. 在这份教程中,我 ...
随机推荐
- python魔法方法__reduce__()的妙用
一.__reduce__()介绍 当定义扩展类型时(也就是使用Python的C语言API实现的类型),如果你想pickle它们,你必须告诉Python如何pickle它们. __reduce__ 被定 ...
- PHP之面向对象小结
PHP 中创建对象 类创建后,我们可以使用 new 运算符来实例化该类的对象: $runoob = new Site; PHP 构造函数 构造函数 ,是一种特殊的方法.主要用来在创建对象时初始化对象, ...
- 如何稀释 流事件 (如,onscroll、change、input、mouseover 等 事件)
1.问题引入:https://segmentfault.com/q/1010000000707337?_ea=62905 2.javascript中的函数节流和函数去抖:https://www.cnb ...
- mybatis批量生成
使用了mybatis-generator后,寻找只写一个table标签就可以全部生成的方法 下载mybatis-generator-core-1.3.2-bundle.zip 解压后打开docs 发现 ...
- Cent OS (二)常用的命令介绍
1. 常用的Linux命令 序号 命令 对应英文 作用 01 ls list 查看当前文件夹下的内容 02 pwd print work directory 查看当前所在的文件夹 03 cd [目 ...
- MDX members使用
Members (Set) 函数返回该指定层次结构内所有成员(不包括计算成员)的集: Members (String) 函数返回已指定名称的单个成员. 通常,将 Members (String) 函数 ...
- linux远程管理器 - xshell和xftp使用教程(zhuan)
准备好连接linux服务器的工具,推荐用xshell和xftp. xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET ...
- Charles重发请求
1.如下图 2.选中某个接口,右键--选择 Repeat Advanced选项,设置请求多次 3.
- django4-模板,templates
如何使用templates呢? 在views.py文件中,函数或者方法通过return render(request,"userInfor.html",{"info_li ...
- static_cast关键字 dynamic_cast关键字
前言 说起C++中的继承.多态.虚函数等概念,可能很多同学都有所了解,但是要说真正熟知的同学可能就不是很多了.最近在编程过程中了解到C++类型的层次转换(这就涉及到了多态和继承的相关概率),通常C语言 ...