一,安装apache2

sudo apt-get install apache2

二.配置cgi-bin

sudo chmod 777 /var/www/html
sudo vim /etc/apache2/sites-enabled/000-default
#加入
DocumentRoot /var/www/html/
ScriptAlias /cgi-bin/ /var/www/html/cgi-bin/
#:wq 保存并退出
sudo a2enmod cgi
sudo /etc/init.d/apache2 restart

三.测试

test.c

#include <stdio.h>
#include <stdio.h> //getenv
int main(void){
printf("Content-Type: text/html\n\n");
printf("GET:%s\n",getenv("QUERY_STRING"));
return 0;
}
gcc -o test test.c
mv test /var/www/html/cgi-bin/test

访问http://localhost/cgi-bin/test?a=b

得到GET:a=b

四.以root身份运行的方法

1.准备要用到的源文件

visit.c 浏览器访问入口

#include <stdio.h>
#include <stdlib.h> int main(int argc,char* argv[])
{
system("./run_root ./run.sh");
return 0;
}

run_root.c 用来获取root权限

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h> int main(int argc,char* argv[])
{
if(argc>1){
uid_t uid ,euid;
uid = getuid();
euid = geteuid();
if(setreuid(euid, uid)) //交换这两个id以获取root权限
perror("setreuid");
system(argv[1]); //执行一个需root权限运行的脚本,我尝试在这里执行应用程序,结果失败了,不知道是操作失误还是其它原因,还望指教
}
return 0;
}

run.sh

#!/bin/bash
#这里运行需要root权限的程序
mkdir /home/hello

2.编译

gcc -o visit visit.c
sudo gcc -o run_root run_root.c
sudo chmod u+s run_root
chmod +x run.sh
sudo mv run_root /var/www/html/cgi-bin/run_root
sudo mv visit /var/www/html/cgi-bin/visit
sudo mv run.sh /var/www/html/cgi-bin/run.sh

结果

inu1255@inu1255-pc:/var/www/html/cgi-bin$
-rwxrwxr-x 1 inu1255 inu1255 7293 12月 9 23:42 visit*
-rwxr-xr-x 1 inu1255 inu1255 60 12月 9 23:59 run.sh*
-rwsr-xr-x 1 root root 7452 12月 9 23:35 run_root*

3.测试

访问 http://localhost/cgi-bin/visit

ls /home

发现多了一个hello文件夹

ubuntu下apache2的cgi-bin中以root权限运行程序的更多相关文章

  1. Ubuntu下如何将普通用户提升到root权限

    在ubuntu的系统操作中,我们经常会使用到系统权限的,因为权限不足,导致在一些操作当中非常麻烦.要获取权限,最长使用的方法就是使用sudo指令,但是来回使用还是比较麻烦,有没有直接提升我们的用户权限 ...

  2. [Linux]Ubuntu下如何将普通用户提升到root权限

    转至:http://jingyan.baidu.com/album/6181c3e0780131152ef153ff.html?picindex=0&qq-pf-to=pcqq.c2c  在u ...

  3. 在PyCharm中以root权限运行和调试python代码

    python中有的代码可能需要su权限,如 import os os.mkdir('/media/xxx/disk_a') 如果在交互式环境中使用,需要以sudo的方式启动python.而在PyCha ...

  4. ubuntu下Apache2配置

    Ubuntu下Apache2的CGI简单配置:http://blog.csdn.net/a623891391/article/details/47170355 Ubuntu Apache的安装和配置以 ...

  5. Ubuntu下apache2安装配置(内含数字证书配置)

    Ubuntu下apache2安装配置(内含数字证书配置)安装命令:sudo apt-get updatesudo apt-get install apache2 配置1.查看apache2安装目录命令 ...

  6. ubuntu下关于profile和bashrc中环境变量的理解(转)

    ubuntu下关于profile和bashrc中环境变量的理解(转)   (0) 写在前面 有些名词可能需要解释一下.(也可以先不看这一节,在后面看到有疑惑再上来看相关解释) $PS1和交互式运行(r ...

  7. Ubuntu 下 Apache2 和 PHP 服务器环境配置

    Ubuntu 下 Apache2 和 PHP 服务器环境配置 1.简介 本文主要是 Ubuntu 下 Apache2 和 PHP 服务器环境配置方法,同样适用于 Debian 系统:Ubuntu 20 ...

  8. Ubuntu下如何修改文件或者文件夹的权限

    Ubuntu下如何修改文件或者文件夹的权限------chmod的亲身测试   具体原理如下: Linux系统下如何修改文档及文件夹(含子文件夹)权限,我们来看一下.              一 介 ...

  9. android中获取root权限的方法以及原理(转)

    一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. 二. Root 的介绍 1. Root 的目的 可以让我们拥有 ...

随机推荐

  1. hd acm1018

    Problem Description In many applications very large integers numbers are required. Some of these app ...

  2. SpringBoot2.0之整合RabbitMQ

    案例: Springboot 对RabbitMQ的支持 公共的pom: <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  3. Struts2 遇到的问题汇总

    1.报错如下信息: org.apache.jasper.JasperException: The Struts dispatcher cannot be found. This is usually ...

  4. EntityFramework 学习 一 Delete Entity using DBContext in Disconnected Scenario

    Student studentToDelete; . Get student from DB using (var ctx = new SchoolDBEntities()) { studentToD ...

  5. 如何搭建一个GitHub在自己的服务器上?

    摘自:http://blog.csdn.net/yangzhenping/article/details/43937595

  6. codeforces 460D:Little Victor and Set

    Description Little Victor adores the sets theory. Let us remind you that a set is a group of numbers ...

  7. BZOJ 2058 [Usaco2010 Nov]Cow Photographs:逆序对【环上最小逆序对】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2058 题意: 给你一个由1~n组成的排列,首尾相接围成一个环. 你可以任意次交换其中两个相 ...

  8. C#中substring ()的用法

    C#中substring ()的用法:http://www.cnblogs.com/bluespace/archive/2007/12/11/782336.html

  9. hihocoder-1284 机会渺茫(水题)

    机会渺茫 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在追求一名学数学的女生小Z.小Z其实是想拒绝他的,但是找不到好的说辞,于是提出了这样的要求:对于给定的两 ...

  10. FFmpeg 的sws_getContext函数 、sws_scale函数

    FFmpeg里面的sws_scale库可以在一个函数里面同时实现:1.图像色彩空间转换:2.分辨率缩放:3.前后图像滤波处理. 其核心函数主要有三个: // 初始化sws_scalestruct Sw ...