Replace the option noexec to exec in /etc/fstab file through a shell script

1k views Asked by At

Only the /tmp option of noexec to exec should change. The /var/tmp option of noexec to exec shouldn't change.

contents of /etc/fstab

UUID=f229a689-a31e-4f1a-a823-9a69ee6ec558       /         xfs     defaults        0 0
UUID=eeb1df48-c9b0-408f-a693-38e2f7f80895       /boot         xfs     defaults        1 2
UUID=b41e6ef9-c638-4084-8a7e-26ecd2964893       swap      swap    defaults        0 0
UUID=79aa80a1-fa97-4fe1-a92d-eadf79721204       /var         xfs     defaults        1 2
UUID=644be3d0-433c-4ed5-bf12-7f61d5b99860       /tmp         xfs     defaults,nodev,nosuid,noexec        1 2
UUID=decda446-34ac-45b6-826c-ae3f090ed717       /var/log         xfs     defaults        1 2
UUID=a74170bc-0309-4b3b-862e-722fb7a6882d       /var/tmp         xfs     defaults,nodev,nosuid,noexec        1 2
1

There are 1 answers

0
fpmurphy On

Using awk:

$ cat 1.awk
$2=="/tmp"  { n=split($4,a,",");
              str=""
              for (i=1; i <= n; i++ ) {
                  if (a[i] != "noexec") {
                      if (length(str))
                          str=str","
                      str=str""a[i]
                   }
              }
              $4=str; print }
$2 != "/tmp" { print }

$ awk -f 1.awk fstab
UUID=f229a689-a31e-4f1a-a823-9a69ee6ec558       /         xfs     defaults        0 0
UUID=eeb1df48-c9b0-408f-a693-38e2f7f80895       /boot         xfs     defaults        1 2
UUID=b41e6ef9-c638-4084-8a7e-26ecd2964893       swap      swap    defaults        0 0
UUID=79aa80a1-fa97-4fe1-a92d-eadf79721204       /var         xfs     defaults        1 2
UUID=644be3d0-433c-4ed5-bf12-7f61d5b99860 /tmp xfs defaults,nodev,nosuid 1 2
UUID=decda446-34ac-45b6-826c-ae3f090ed717       /var/log         xfs     defaults        1 2
UUID=a74170bc-0309-4b3b-862e-722fb7a6882d       /var/tmp         xfs     defaults,nodev,nosuid,noexec        1 2

Note, alignment of fields in the modified line can easily be improved using printf. I cannot tell if you were using tabs or spaces between the various fields.