一,安装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. ubuntu 部署的mysql无法远程链接

    允许远程用户登录访问mysql的方法 从任何主机上使用root用户,密码:youpassword(你的root密码)连接到mysql服务器: # mysql -u root -proot mysql& ...

  3. SpringCloud-服务的消费者(rest+ribbon)

    SpringCloud-服务的消费者(rest+ribbon) 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring Cloud有两种服务调用 ...

  4. Bootstrap(一)标题

    Bootstrap标题样式进行了以下显著的优化重置: 1.重新设置了margin-top和margin-bottom的值,  h1~h3重置后的值都是20px:h4~h6重置后的值都是10px.2.所 ...

  5. CSS3分享功能

    [代码][CSS]代码  $.fn.share = function(opts) {  var $body, $head;  if ($(this).length === 0) {    consol ...

  6. spring学习(4)

    在spring容器内拼凑bean叫做装配.装配bean的时候,需要告诉容器哪些bean以及容器如何使用依赖注入将它们配合在一起. 上下文定义文件的根元素是<beans>,<beans ...

  7. delphi数据库的备份及还原

    实例应用1: //备份procedure TF_DataBaseBackUp.Btn_bfClick(Sender: TObject); var i:integer; begin if SaveDia ...

  8. java--xml文件读取(SAX)

    SAX解析原理: 使用Handler去逐个分析遇到的每一个节点 SAX方式解析步奏: 创建xml解析需要的handler(parser.parse(file,handler)) package com ...

  9. android之VideoView和视频播放View的扩展

    1.概念及扩展 VideoView 是android 系统提供的一个媒体播放显示和控制的控件.其结构层次如下: 原型:VideoView extends SurfaceView implements  ...

  10. 【leetcode刷题笔记】ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...