简体   繁体   English

在 bash 脚本中使用 sudo:权限被拒绝

[英]Using sudo inside a bash script: permission denied

This is my first real bash script, called makevhost, and I'm running into a permissions error when trying to use sudo to execute commands in the script.这是我的第一个真正的 bash 脚本,称为 makevhost,当我尝试使用 sudo 执行脚本中的命令时遇到权限错误。 A site name is supplied as an argument to the script call in the CLI.站点名称作为参数提供给 CLI 中的脚本调用。 The file is executable and the bin directory in which it lives is added to $PATH.该文件是可执行的,并且它所在的 bin 目录被添加到 $PATH。

The concept is simple: It creates the directories and sample templates for a web site on my apache development server (Debian 11 VM on a Windows host), then creates the virtual host file and restarts the server.这个概念很简单:它在我的 apache 开发服务器(Windows 主机上的 Debian 11 VM)上为网站创建目录和示例模板,然后创建虚拟主机文件并重新启动服务器。 I've tried all the suggestionshere , most of which I found repeated on numerous sites and forums.我已经尝试了这里的所有建议,其中大部分我发现在许多网站和论坛上重复出现。

When I run the script it seems to run fine until line 31, where I get a "Permission Denied" error.当我运行脚本时,它似乎运行良好,直到第 31 行,我收到“Permission Denied”错误。 I get another error on line 43, but the permissions error is what I'm working on now.我在第 43 行遇到另一个错误,但权限错误是我现在正在处理的问题。

Here's my code:这是我的代码:

#!/bin/bash

if mkdir -p /var/www/"$1"/public_html; then
    echo "Creating site directories ..."
    cd /var/www/"$1"/public_html
    mkdir style images js
    chown -R $USER:$USER /var/www/"$1"
else
    echo "Could not create site directories"
    exit
fi

echo "Creating index.html and style.css"
cat <<- EOF > index.html
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="style/style.css"></link>
</head>
<body>

</body>
</html>
EOF

touch style/style.css
echo "Site files created"

sudo cat <<- EOF > /etc/apache2/sites-available/"$1".com.conf
<VirtualHost *:8080>
    ServerAdmin eric@sabresong.com
    ServerName sabresong.local
    ServerAlias "$1"
    DocumentRoot /var/www/"$1"/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF
echo "Virtual Host File created."

if [ sudo a2ensite "$1".com.conf ]; then
    echo "$1 enabled"
    sudo a2dissite 000-default.conf
    echo "Restarting Apache"
    sudo systemctl restart apache2
    echo "Finished!"
else
    echo 'WARNING! Could not enable new site "$1"'
fi

My /etc/sudoers.d/eric has only one line: eric ALL=(root) NOPASSWD: /home/eric/bin/我的 /etc/sudoers.d/eric 只有一行: eric ALL=(root) NOPASSWD: /home/eric/bin/

In visudo, I have added this: eric ALL=(ALL:ALL) NOPASSWD: /home/eric/bin/ I also tried that same line with ALL=(ALL)在 visudo 中,我添加了以下内容: eric ALL=(ALL:ALL) NOPASSWD: /home/eric/bin/我还尝试了 ALL=(ALL) 的同一行

I know that using NOPASSWD isn't best practice, but I'm the only person with access to ths machine, and I haven't yet worked out how to use the sudo password for specific lines in the script but not for others, so I'm going this route, mostly for the learning experience.我知道使用 NOPASSWD 不是最佳实践,但我是唯一可以访问这台机器的人,而且我还没有弄清楚如何将 sudo 密码用于脚本中的特定行,但不适用于其他行,所以我走这条路,主要是为了学习经验。

I think you actually have to run the script with the sudo command.我认为您实际上必须使用 sudo 命令运行脚本。 so:所以:

sudo ./makevhost

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM