[英]add options to /tmp partition in fstab by using a bash script
在基本的 linux 服务器安装之后,我正在编写一个长脚本来添加安全选项。 其中一项安全措施是向 /temp 分区添加选项。 为此,我写道:
awk '{
if (!/^#/ && $2 == "/tmp")
awk '{ if(!match(/rw,nosuid,nodev,noexec,realtime/, $4)) $4=$4",rw,nosuid,nodev,noexec,realtime" } 1' /etc/fstab > /tmp/$$
cat /tmp/$$ > /root/fstab
rm /tmp/$$
echo "$(tput setaf 2)Mounting point /tmp has been updated and is now secured !$(tput sgr0)" >&3
}' /etc/fstab
fstab 文件是:
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/debiantest--vg-root / ext4 errors=remount-ro 0 1
# /boot was on /dev/sda1 during installation
UUID=495503ab-78ed-4af5-8587-5d1c30ff233d /boot ext2 defaults 0 2
/dev/mapper/debiantest--vg-home /home ext4 defaults 0 2
/dev/mapper/debiantest--vg-tmp /tmp ext4 defaults 0 2
/dev/mapper/debiantest--vg-var /var ext4 defaults 0 2
/dev/mapper/debiantest--vg-swap_1 none swap sw 0 0
/dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0
我想改:
/dev/mapper/debiantest--vg-tmp /tmp ext4 默认 0 2
经过:
/dev/mapper/debiantest--vg-tmp /tmp ext4 默认值,rw,nosuid,nodev,noexec,realtime 0 2
但这不适用于此错误消息:
无人值守符号« ( » 附近的语法错误
我发现这是因为我在第二个 awk 中有简单的引号,它停止了之前的第一个简单引号(第一个 awk)。
那么如何先检查 /tmp 是否存在,然后在这一行添加选项呢? 也许“简单”的 grep 可以工作,但是如何使用 grep 来确保只修改 /tmp 的行? 例如,我们可以使用 /var/tmp...
非常感谢您的帮助。
托托
好的解决方案是删除第一个 awk 并将 egrep 处理为:
if egrep -qs '\s/tmp\s' /etc/fstab; then
awk -v OFS='\t' '!/^#/ && ($2 == "/tmp") && ($4 == "defaults") { $4=$4",rw,nosuid,nodev,noexec,realtime" } 1' /etc/fstab > /tmp/$$
cat /tmp/$$ > /etc/fstab
rm /tmp/$$
echo "$(tput setaf 2)Mounting point /tmp has been updated and is now secured !$(tput sgr0)" >&3
else
echo "$(tput setaf 1)Bad news, you don't have a separate /tmp partition... This is unsecured !$(tput sgr0)" >&3
fi
感谢我指出双重 awk 问题。
如果您认为 grep 可以改进,请告诉我:-)
问候
托托
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.