简体   繁体   English

在shell脚本中以root身份运行命令

[英]Run command as root within shell script

I'm working on a script that will shred a usb drive and install Kali linux with encrypted persistent data. 我正在研究一个脚本,该脚本将切碎USB驱动器并使用加密的持久性数据安装Kali linux。

#! /bin/bash

cd ~/Documents/Other/ISOs/Kali
echo "/dev/sdx x=?"
read x
echo "how many passes to wipe? 1 will be sufficient."
read n
echo "sd$x will be wiped $n times."
read -p "do you want to continue? [y/N] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
        exit 1
fi

echo "Your role in the installation process is not over. You will be prompted to type YES and a passphrase."

sudo shred -vz --iterations=$n /dev/sd$x

echo "Wiped. Installing Kali"
sudo dd if=kali-linux-2.0-amd64.iso of=/dev/sd$x bs=512k

echo "Installed. Making persistence."

y=3

sudo parted /dev/sd$x mkpart primary 3.5GiB 100%
x=$x$y
sudo cryptsetup --verbose --verify-passphrase luksFormat /dev/sd$x
sudo cryptsetup luksOpen /dev/sd$x my_usb
sudo mkfs.ext3 -L persistence /dev/mapper/my_usb
sudo e2label /dev/mapper/my_usb persistence

sudo mkdir -p /mnt/my_usb
sudo mount /dev/mapper/my_usb /mnt/my_usb
sudo -i
echo "/ union" > /mnt/my_usb/persistence.conf
umount /dev/mapper/my_usb

cryptsetup luksClose /dev/mapper/my_usb

echo "Persistence complete. Installation complete."

It works nearly perfectly. 它几乎完美地工作。 These commands individually entered into the terminal will create the desired effect, but the problem comes in at line 37: 这些分别输入到终端中的命令将产生所需的效果,但是问题出在第37行:

sudo echo "/ union" > /mnt/my_usb/persistence.conf

That command won't work unless I'm logged in as root user. 除非我以root用户身份登录,否则该命令将不起作用。 To solve this I tried adding the sudo -i command before, but once I do that all of the following commands are skipped. 为了解决这个问题,我尝试在之前添加sudo -i命令,但是一旦执行,将跳过以下所有命令。

It's okay if the solution suggested requires me to type in the password. 如果解决方案建议要求我输入密码,也可以。 I don't want the password stored in the script, that's just wreckless. 我不希望密码存储在脚本中,那简直是残破的。

Side note, I didn't make a generic form for this question because I want other people to be able use this if they like it. 旁注,我没有为这个问题制作通用表格,因为我希望其他人可以在喜欢的情况下使用它。

The problem is that the echo runs with root privilege but the redirection happens in the original shell as the non-root user. 问题是echo以root特权运行,但是重定向在原始shell中以非root用户身份发生。 Instead, try running an explicit sh under sudo and do the redirection in there 相反,请尝试在sudo下运行显式sh并在其中进行重定向

sudo /bin/sh -c 'echo "/ union" > /mnt/my_usb/persistence.conf'

The problem is that when you type in the following command: 问题是当您键入以下命令时:

sudo echo "/ union" > /mnt/my_usb/persistence.conf

Only the "echo" will be run as root through sudo, but the redirection to the file using > will still be executed as the "normal" user, because it is not a command but something performed directly by the shell. 只有“ echo”将通过sudo以root身份运行,但是使用>重定向到文件的操作仍将以“普通”用户的身份执行,因为它不是命令,而是由Shell直接执行的操作。

My usual solution is to use tee so that it runs as a command and not as a shell built-in operation, like this: 我通常的解决方案是使用tee ,使其作为命令而不是作为shell内置操作运行,如下所示:

echo "/ union" | sudo tee /mnt/my_usb/persistence.conf >/dev/null

Now the tee command will be run as root through sudo and will be allowed to write to the file. 现在, tee命令将通过sudo以root身份运行,并将被允许写入文件。 >/dev/null is just added to keep the output of the script clean. 只是添加了>/dev/null以保持脚本的输出干净。 If you ever want to append instead of overwrite (eg you would be using >> normally), then use tee -a . 如果您想追加而不是覆盖(例如,通常使用>> ),请使用tee -a

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

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