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)
Table of Contents
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:
Unmodified | Sorted | Ultimately Desired |
---|---|---|
root:x:0:root | root:x:0:root | root:x:0:root |
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”.
Leave a Reply