Bash 文件|权限被拒绝,没有此文件或目录

Bash 文件|权限被拒绝,没有此文件或目录

我创建了一个 bash 文件来在 ProFTPD 服务器中注册用户。bash 文件从 PHP 文件运行,发送 5 个变量作为参数,即:姓名、姓氏、用户、密码和一个 4 位随机数(例如 7215)

每次运行它时都会出现这些错误,并且只创建了用户及其文件夹。ProFTPD 配置没有改变。

#!/bin/bash

var1=$(echo $1 | tr [:upper:] [:lower:] | cut -c 1)
var2=$(echo $2 | tr [:upper:] [:lower:] | cut -c 1)

rnumber=$(echo $5)
dir=$(echo "dir")
folder=$(echo $var1$var2$var3$dir$rnumber)
user=$(echo $3)

sudo mkdir -p /var/www/ftpdir/Hostitec/usersftp/$folder

sudo chmod 777 /var/www/ftpdir/Hostitec/usersftp/$folder

command=$(cat -n /etc/proftpd/proftpd.conf | grep DefaultRoot | tail -n 1 | cut -f 1)

sudo head -n $command /etc/proftpd/proftpd.conf > /etc/proftpd/copyconf

echo "DefaultRoot   /var/www/ftpdir/Hostitec/usersftp/$folder $user" >> /etc/proftpd/copyconf

lastone=$(expr $command + 1)

sudo tail -n +$lastone /etc/proftpd/proftpd.conf >> /etc/proftpd/copyconf

sudo cat /etc/proftpd/copyconf > /etc/proftpd/proftpd.conf

sudo rm /etc/proftpd/copyconf

sudo useradd $user

sudo chown $user:$user /var/www/ftpdir/Hostitec/usersftp/$folder

echo "$user:$4" | sudo chpasswd -m

sudo service proftpd restart

checkdir=$(ls /var/www/ftpdir/Hostitec/usersftp/$folder | grep $folder)

checkuser=$(cat /etc/passwd | grep $user)

echo $checkdir > check.txt

echo $checkuser > check.txt

if ! [ -s check.txt ]
then
    rm -r /var/www/ftpdir/Hostitec/usersftp/$folder
    sudo userdel $user
fi

这些是我遇到的错误:

./script.sh: line 17: /etc/proftpd/copyconf: Permission denied
./script.sh: line 19: /etc/proftpd/copyconf: Permission denied
./script.sh: line 23: /etc/proftpd/copyconf: Permission denied
cat: /etc/proftpd/copyconf: No such file or directory
rm: You cannot remove '/etc/proftpd/copyconf': No such file or directory

答案1

shell 重定向未以 root 身份运行。请尝试将重定向替换为tee

替换这些:

# Overwrite
sudo command > /path/file  # Original
sudo command | sudo tee /path/file  # Replacement

# Append
sudo command >> /path/file  # Original
sudo command | sudo tee -a /path/file  # Replacement

另一种方法是运行整个脚本root

答案2

您的脚本存在一些问题,例如, sudo cat /etc/proftpd/copyconf > /etc/proftpd/proftpd.conf只有 行sudo cat /etc/proftpd/copyconf以超级用户权限执行,而 > /etc/proftpd/proftpd.conf部分以调用用户的权限执行,我认为对脚本进行以下更改将解决您的问题。

    #!/bin/bash

var1=$(echo $1 | tr [:upper:] [:lower:] | cut -c 1)
var2=$(echo $2 | tr [:upper:] [:lower:] | cut -c 1)

rnumber=$(echo $5)
dir=$(echo "dir")
folder=$(echo $var1$var2$var3$dir$rnumber)
user=$(echo $3)

sudo mkdir -p /var/www/ftpdir/Hostitec/usersftp/$folder

sudo chmod 777 /var/www/ftpdir/Hostitec/usersftp/$folder

command=$(cat -n /etc/proftpd/proftpd.conf | grep DefaultRoot | tail -n 1 | cut -f 1)

sudo head -n $command /etc/proftpd/proftpd.conf > /tmp/copyconf

echo "DefaultRoot   /var/www/ftpdir/Hostitec/usersftp/$folder $user" >> /tmp/copyconf

lastone=$(expr $command + 1)

sudo tail -n +$lastone /etc/proftpd/proftpd.conf >> /tmp/copyconf

sudo cp /tmp/copyconf /etc/proftpd/proftpd.conf

sudo rm /tmp/copyconf

sudo useradd $user

sudo chown $user:$user /var/www/ftpdir/Hostitec/usersftp/$folder

echo "$user:$4" | sudo chpasswd -m

sudo service proftpd restart

checkdir=$(ls /var/www/ftpdir/Hostitec/usersftp/$folder | grep $folder)

checkuser=$(cat /etc/passwd | grep $user)

echo $checkdir > check.txt

echo $checkuser > check.txt

if ! [ -s check.txt ]
then
    rm -r /var/www/ftpdir/Hostitec/usersftp/$folder
    sudo userdel $user
fi

相关内容