简体   繁体   English

如何使用脚本自动挂载 Luks 加密驱动器

[英]How to Auto Mount Luks Encrypted Drive With Script

How would I do this with Python or bash script:我将如何使用 Python 或 bash 脚本执行此操作:

admin_pass ="admin pass"
drive_password = "password"
auto_mount_path = "/dev/sdc1"
mount_point = "backups1"

sudo cryptsetup luksOpen auto_mount_path mount_point
admin_pass
drive_password

I want the script to mount the encrypted drive without user having to enter the password or admin pass.我希望脚本安装加密驱动器,而无需用户输入密码或管理员密码。

Is it possible with bash or Python? bash 或 Python 是否可行?

Thanks谢谢

You don't need a script.你不需要脚本。 You have everything on board in your system.您拥有系统中的所有内容。 And you said your system drive is also encrypted so we can put our files there without risking that the passphrase of your storage drive will be compromised.你说你的系统驱动器也是加密的,所以我们可以把我们的文件放在那里,而不会冒着你的存储驱动器的密码被泄露的风险。

I remember that I have done this with a passphrase years ago too.我记得几年前我也用密码来做到这一点。 But the easier way is by using a keyfile and by that you also have a second keyslot in your LUKS file as backup to be able to open your encrypted storage if you forget your passphrase.但更简单的方法是使用密钥文件,这样您的 LUKS 文件中还有第二个密钥槽作为备份,以便在您忘记密码时能够打开加密存储。

In short: You will need the terminal, something like leafpad opened with root rights, 10min of your time – maybe a few minutes more for starters – and the following instructions:简而言之:您将需要终端,例如以 root 权限打开的 Leafpad,您需要 10 分钟的时间(对于初学者来说可能需要几分钟)以及以下说明:

All choosen names (keyfile, decrypted drivename, mountpoint) can be defined by yourself.所有选择的名称(密钥文件、解密的驱动器名称、挂载点)都可以自己定义。 They are just an example to understand where to put the names.它们只是了解名称放置位置的示例。

To set up a unique keyfile you use your terminal要设置唯一的密钥文件,请使用终端

~$ sudo -i
~# dd if=/dev/urandom of=/root/storage.keyfile bs=8 count=256
~# chown root:root /root/storage.keyfile
~# chmod 000 /root/storage.keyfile

Leave the terminal open.让终端保持打开状态。 There is more to do... Now we put this keyfile into a free keyslot of your encrypted storage drive.还有更多工作要做... 现在我们将此密钥文件放入加密存储驱动器的空闲密钥槽中。 Look up your storage drive first.首先查找您的存储驱动器。

~# lsblk -f

Let's asume your storage drive is /dev/sdc1 as you quoted in your script.假设您的存储驱动器是您在脚本中引用的/dev/sdc1

~#cryptsetup -v luksAddKey /dev/sdc1 /root/storage.keyfile

You have to enter your passphrase to let cryptseptup do the adding.您必须输入密码才能让 cryptseptup 进行添加。

Then you open up /etc/crypttab with leafpad (or similar) with root rights to be able to read and write – it will be also easier to copy the UUID of your drive into leafpad (or similar) from your terminal.然后,您使用具有 root 权限的 Leafpad(或类似设备)打开/etc/crypttab以便能够读取和写入 - 从您的终端将驱动器的 UUID 复制到 Leafpad(或类似设备)也会更容易。 We will use the UUID of your storage drive as its unique id because it is possible that your system changes the position of your drive during a boot and then your automated process won't find it under the old path.我们将使用存储驱动器的 UUID 作为其唯一 ID,因为您的系统可能会在引导期间更改驱动器的 position,然后您的自动化过程将无法在旧路径下找到它。

With ~# lsblk -f you should find the UUID of /dev/sdc1 .使用~# lsblk -f你应该找到/dev/sdc1UUID It looks like 5f2add0f-d41e-250e-acc7-38512658a26d .它看起来像5f2add0f-d41e-250e-acc7-38512658a26d

Mark the UUID and copy it.标记UUID并复制它。 The first entry in /etc/crypttab is the name of your opened storage drive. /etc/crypttab中的第一个条目是您打开的存储驱动器的名称。 You can choose it like you want but without spaces or special characters.您可以根据需要选择它,但没有空格或特殊字符。 Keep it simple.把事情简单化。

After that comes the path with the UUID .之后是带有UUID的路径。 Then the keyfile and the options.然后是密钥文件和选项。 Your /etc/crypttab should be looking like this in the end:你的/etc/crypttab最后应该是这样的:

backups1-SSD   /dev/disk/by-uuid/5f2add0f-d41e-250e-acc7-38512658a26d   /root/storage.keyfile   luks

You can save the /etc/crypttab now.您现在可以保存/etc/crypttab

Then you open up /etc/fstab like you did with the crypttab and you are almost finished.然后你像打开crypttab一样打开/etc/fstab ,你就差不多完成了。

Put another line into it.将另一条线放入其中。 For the mount point of your opened storage drive I use the same of your script example assuming that folder will be in your home drive and your drive is ext4 .对于您打开的存储驱动器的挂载点,我使用与您的脚本示例相同的内容,假设该文件夹将位于您的主驱动器中并且您的驱动器是ext4 The new line in your /etc/fstab should look like this: /etc/fstab中的新行应如下所示:

/dev/mapper/backups1-SSD   /home/Emily/backups1   ext4   defaults   0 0

Save the file and you are done.保存文件,您就完成了。 You can close the terminal and leafpad.您可以关闭终端和leafpad。

You can reboot your system now to test the automated process.您现在可以重新启动系统以测试自动化过程。 After the login you should find your encrypted storage drive opened and mounted at /home/Emily/backups1 .登录后,您应该会发现您的加密存储驱动器已打开并安装在/home/Emily/backups1

For newbies: You don't need to close your opened drives when you shutdown your computer.对于新手:关闭计算机时无需关闭打开的驱动器。 Your system will do it for you.你的系统会为你做这件事。

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

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