Need help understanding sed.

General support questions
Post Reply
dbhosttexas
Posts: 12
Joined: 2014/09/23 20:59:25

Need help understanding sed.

Post by dbhosttexas » 2019/05/13 16:13:15

I am learning sed in a very hit or miss manner, sorry... I am not looking for a spoon fed just do this to fix your problem, I am looking to understand how to use sed to solve my problem so that the next time I come accross this I can solve my own issue. Thanks...

I have tasks I believe sed would be good for, although I am unsure on how to do this.

So let's say I am trying to secure a batch of servers, and our requirement is to set the /tmp filesystem to noexec,nosuid in the /etc/fstab.

So let's say most hosts look like the following...
/dev/mapper/rhel-tmp /tmp xfs defaults 0 0


But maybe a junior admin loaded one and their entry looks like...
/dev/mapper/rhel-tmp /tmp ext4 defaults 0 0

And perhaps another one just has its whitespace either wider, or narrower such as...

/dev/mapper/rhel-tmp /tmp xfs defaults 0 0

So knowing this, how would I go about making ONE script to run against all of these hosts that will attach the nodev,nosuid parameters to the fstab entry?

User avatar
TrevorH
Site Admin
Posts: 33215
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Need help understanding sed.

Post by TrevorH » 2019/05/13 16:42:43

Well, since the only thing you need to change in all of those example lines is 'defaults' to 'defaults,noexec,nosuid', it's the same sed command to do all of them. There is no filesystem specific bit of it that needs to be handled. Using sed is fine and will work but personally I would look at configuration management programs like puppet to help you do this and many more things too.

You can use augeas with puppet and augeas can make amendments to files. Using augtool at the command line you can do things like
augtool> print /files/etc/fstab/files/etc/fstab
/files/etc/fstab/#comment[1] = "/etc/fstab"
/files/etc/fstab/#comment[2] = "Created by anaconda on Fri Jan 26 00:42:00 2018"
/files/etc/fstab/#comment[3] = "Accessible filesystems, by reference, are maintained under '/dev/disk'"
/files/etc/fstab/#comment[4] = "See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info"
/files/etc/fstab/1
/files/etc/fstab/1/spec = "/dev/mapper/centos-root"
/files/etc/fstab/1/file = "/"
/files/etc/fstab/1/vfstype = "ext4"
/files/etc/fstab/1/opt = "defaults"
/files/etc/fstab/1/dump = "1"
/files/etc/fstab/1/passno = "1"
/files/etc/fstab/2
/files/etc/fstab/2/spec = "UUID=8e5a61eb-54a2-488a-81bd-754e96659eab"
/files/etc/fstab/2/file = "/boot"
/files/etc/fstab/2/vfstype = "ext4"
/files/etc/fstab/2/opt = "defaults"
/files/etc/fstab/2/dump = "1"
/files/etc/fstab/2/passno = "2"
augtool> set /files/etc/fstab/2/opt "defaults,noexec,nosuid"
augtool> print /files/etc/fstab
...
/files/etc/fstab/2/opt = "defaults,noexec,nosuid"
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

dbhosttexas
Posts: 12
Joined: 2014/09/23 20:59:25

Re: Need help understanding sed.

Post by dbhosttexas » 2019/05/13 18:58:06

Sadly, in my environment Puppet isn't necessarily an option. We are using a CM product just not puppet, that passes through local commands / scripts from the CM server to the clients.

Thraex
Posts: 51
Joined: 2019/05/14 19:50:28

Re: Need help understanding sed.

Post by Thraex » 2019/05/14 20:08:13

You can try:
sed -e '/\/tmp/s/defaults*/defaults,noexec,nosuid/' /etc/fstab

Now for a little description to help you understand:
Since you only want to change the /tmp options you'd start with defining that line which is \/tmp/

First backslash is an escape character so sed doesn't read /tmp incorrectly.
Second, you want to substitute (s) defaults with defaults,noexec,nosuid.

Also, the -e is just to show you what sed would change. Change it to -i to change it in the file (test it first though!)
Hope this helps!

Post Reply