Red Hat Bugzilla – Bug 3246
useradd overwrites existing dot-files by default (without truncating)
Last modified: 2015-01-07 18:37:36 EST
When the useradd command is run to add a new user, existing
"dot" files in the users home directory are overwritten by
default, even though the man page states that these files
are not copied by default. So when you do a fresh install,
mount an existing /home partition, and add the users with
"useradd", a number of important dot-files are blown away. I
would at least expect that a backup copy would be made.
In addition to this, the copy action itself contains a bug.
The destination file is opened without the O_TRUNC flag, so
the file is not truncated on a close. This is easily fixed
by adding the O_TRUNC flag to the open() call in
if ((ofd = open (dst_name, O_WRONLY|O_CREAT|O_TRUNC, 0)) < 0
Useradd must be invoked with the -M option in order to not overwrite
the existing config files in the home directory. The default behavior
for useradd is indeed to overwrite everything in the home directory.