Neat & Tidy /etc/group

Here’s a way to keep your /etc/group file “neat and tidy” (Gentoo wiki re: Complete Virtual Mail Server) and ensure that additions to /etc/group will make numerical sense.


Below is from my Dokuwiki page re: sort with redactions (v. 2024/03/30 17:20)

sort

sort

Parameters

From man sort:

-b, --ignore-leading-blanks
-d, --dictionary-order
-f, --ignore-case
-i, --ignore-nonprinting
-r, --reverse

-k, --key=KEYDEF  sort via a key; KEYDEF gives location and type

       KEYDEF  is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number 
       and C a character position in the field;
       both are origin 1, and the stop position defaults to the line’s end.  If neither -t nor -b 
       is in effect, characters in a  field  are
       counted  from  the  beginning  of the preceding whitespace.  OPTS is one or more 
       single-letter ordering options [bdfgiMhnRrV], which
       override global ordering options for that key.  If no key is given, use the entire line 
       as the key.  Use --debug to diagnose  incorrect key usage.

-t, --field-separator=SEP use SEP instead of non-blank to blank transition
-u, --unique with -c, check for strict ordering; without -c, output only the first of an equal run

Cookbook Recipes

/etc/group

To list by the ID. This can be helpful to restructures since the process that creates a new entry will look to the value at the end of the file and increment it by 1, so the outer high values should be tucked in before the area were user added groups occurs, e.g. 1000+

   sort -k 3 -t : -n /etc/group
   

Discussion

Here is a comparison table. What triggers the need for re-ordering is that package acct-group/postfix stuck in it's entry vmail:x:5000: at the last place for user entries which will cause any subsequent entry to be number 5001. We want subsequent entries to be just after 1000, so we moved vmail:x:5000: just above the sorted list where number 1000 starts causing the end of the list (excepting the final entries nogroup:x:65533: and nobody:x:65534:) to be BOGUS3:x:1002:

UnmodifiedSortedUltimately Desired
root:x:0:root
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
sys:x:3:root,bin,adm
adm:x:4:root,adm,daemon
tty:x:5:
disk:x:6:root,adm
lp:x:7:lp
mem:x:8:
kmem:x:9:
wheel:x:10:root,BOGUS1,BOGUS2
floppy:x:11:root
news:x:13:news
uucp:x:14:uucp
console:x:17:
audio:x:18:BOGUS1,BOGUS2
cdrom:x:19:
dialout:x:20:
tape:x:26:root
video:x:27:root
cdrw:x:80:
usb:x:85:
input:x:97:
users:x:100:BOGUS1,BOGUS2,BOGUS3
portage:x:250:
utmp:x:406:
nogroup:x:65533:
nobody:x:65534:
man:x:15:
sshd:x:22:
kvm:x:78:
render:x:28:
mail:x:12:postfix
apache:x:81:BOGUS3
postgres:x:70:
lpadmin:x:106:
BOGUS1:x:1000:
locate:x:105:
nullmail:x:88:
ldap:x:439:
cron:x:16:
crontab:x:104:
mysql:x:60:
BOGUS2:x:1001:
sgx:x:405:
BOGUS3:x:1002:
ftp:x:21:
smmsp:x:209:
messagebus:x:101:
ntp:x:123:
dovecot:x:76:
dovenull:x:74:
postfix:x:207:
postdrop:x:208:
vmail:x:5000:
root:x:0:root
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
sys:x:3:root,bin,adm
adm:x:4:root,adm,daemon
tty:x:5:
disk:x:6:root,adm
lp:x:7:lp
mem:x:8:
kmem:x:9:
wheel:x:10:root,BOGUS1,BOGUS2
floppy:x:11:root
mail:x:12:postfix
news:x:13:news
uucp:x:14:uucp
man:x:15:
cron:x:16:
console:x:17:
audio:x:18:BOGUS1,BOGUS2
cdrom:x:19:
dialout:x:20:
ftp:x:21:
sshd:x:22:
tape:x:26:root
video:x:27:root
render:x:28:
mysql:x:60:
postgres:x:70:
dovenull:x:74:
dovecot:x:76:
kvm:x:78:
cdrw:x:80:
apache:x:81:BOGUS3
usb:x:85:
nullmail:x:88:
input:x:97:
users:x:100:BOGUS1,BOGUS2,BOGUS3
messagebus:x:101:
crontab:x:104:
locate:x:105:
lpadmin:x:106:
ntp:x:123:
postfix:x:207:
postdrop:x:208:
smmsp:x:209:
portage:x:250:
sgx:x:405:
utmp:x:406:
ldap:x:439:
BOGUS1:x:1000:
BOGUS2:x:1001:
BOGUS3:x:1002:
vmail:x:5000:
nogroup:x:65533:
nobody:x:65534:
root:x:0:root
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
sys:x:3:root,bin,adm
adm:x:4:root,adm,daemon
tty:x:5:
disk:x:6:root,adm
lp:x:7:lp
mem:x:8:
kmem:x:9:
wheel:x:10:root,BOGUS1,BOGUS2
floppy:x:11:root
mail:x:12:postfix
news:x:13:news
uucp:x:14:uucp
man:x:15:
cron:x:16:
console:x:17:
audio:x:18:BOGUS1,BOGUS2
cdrom:x:19:
dialout:x:20:
ftp:x:21:
sshd:x:22:
tape:x:26:root
video:x:27:root
render:x:28:
mysql:x:60:
postgres:x:70:
dovenull:x:74:
dovecot:x:76:
kvm:x:78:
cdrw:x:80:
apache:x:81:BOGUS3
usb:x:85:
nullmail:x:88:
input:x:97:
users:x:100:BOGUS1,BOGUS2,BOGUS3
messagebus:x:101:
crontab:x:104:
locate:x:105:
lpadmin:x:106:
ntp:x:123:
postfix:x:207:
postdrop:x:208:
smmsp:x:209:
portage:x:250:
sgx:x:405:
utmp:x:406:
ldap:x:439::
vmail:x:5000:
BOGUS1:x:1000:
BOGUS2:x:1001:
BOGUS3:x:1002:

nogroup:x:65533:
nobody:x:65534:

Note: here are commands to create the first two table cells entries with multiple lines ready-for-dokuwiki-table format:

Column 1 (unsorted - current state):

    cat /etc/group | perl -p -e 's/\n/\\\\ /'

Column 2 (sorted):

   sort -k 3 -t : -n /etc/group | perl -p -e 's/\n/\\\\ /'

Further Note: tr only works on single characters, for example you cannot replace single letter “a” with triple letters “ZZZ”.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *