The adduser command can run a site-specific script to do any setup like removing files. As long as it is acceptable to start with a full copy and then delete some files afterwards, then this approach could work for you.
From the adduser(8) man page:
If the file /usr/local/sbin/adduser.local
exists, it will be executed after the user account has been set
up in order to do any local setup. The arguments passed to
adduser.local are:
username uid gid home-directory
So all you need to do is write a script that takes four parameters and use it remove any files you need. Save it as /usr/local/sbin/adduser.local and make sure it's marked executable (chmod a+x).
Here's something to get you started:
#!/bin/bash
## Site-specific setup for newly-created users.
## adduser(8) will call this script after setting up a new user.
set -euo pipefail
if [[ "$#" != 4 ]]; then
echo "usage: $0 username uid gid home" > /dev/stderr
fi
NEW_USERNAME="${1:?}"
NEW_UID="${2:?}"
NEW_GID="${3:?}"
NEW_HOME="${4:?}"
# The groups command outputs a space-separated list of group names
IFS=' '
for group in $(groups "${NEW_USERNAME}"); do
case "${group}" in
a)
[[ "${VERBOSE}" > 0 ]] && echo Removing file for a
rm "${NEW_HOME}/not_for_a.txt"
;;
b)
[[ "${VERBOSE}" > 0 ]] && echo Removing dir for b
rm -r "${NEW_HOME}/not_for_b/"
;;
*)
[[ "${VERBOSE}" > 1 ]] && echo No special setup required for $group
;;
esac
done
The interesting part, which you'll want to edit, are the lines that look like this one:
a)
[[ "${VERBOSE}" > 0 ]] && echo Removing file for a
rm "${NEW_HOME}/not_for_a.txt"
;;
You can fill in the actual group name and behaviour you'd like to see instead of a) and rm not_for_a.txt.
useraddis discouraged for debian based systems in the man page on my 16.04 system. If the reason for this caution has changed could you add it to your answer. Also on 16.04useraddandadduserare different programs with different options, perhaps you could edit your answer for clarity. – J. Starnes Dec 06 '17 at 06:10addusercommand, so we useuseradd. It is not entirely discouraged: "useraddis a low level utility for adding users. On Debian, administrators should usually useadduserinstead." It is OK to use this command under unusual circumstances like this. – Mukesh Sai Kumar Dec 06 '17 at 06:31useraddis definitely not "discouraged" but just is for different usage like for anything non-interactive as automation, including Docker/Podman, Kubernetes sometimes, and such systems like Ansible. Related: https://github.com/ansible/ansible/issues/75063 (/dev/null must be allowed for skeleton in user module...); https://github.com/ansible/ansible/blob/v2.16.0/lib/ansible/modules/user.py#L1389 . – Serious Angel Nov 17 '23 at 21:28