user = mysql new_uid = 600 old_uid = $ ( id -u $ user )
group = mysql new_gid = 600 old_gid = $ ( id -g $ user )
sudo usermod -u $ new_uid $ user
sudo groupmod -g $ new_gid $ group
ls -lah /var/lib/mysql
), we will see that the files belong to the suspicious user 112 and the suspicious group 127. We will look for such files in order to adopt them :)$old_uid
or the group $old_group
, and collect all the found files (using xargs) as arguments to the chown $user:$group
. find
is executed from root to ensure that it can get into all even the most severely protected folders and find everything it needs. xargs
collects strings from pipe and passes them to the command specified in the argument (chown). I note that xargs
can execute a command several times to avoid an argument string that is too long.sudo find / -user $ old_uid -or -group $ old_gid -print0 | xargs -0 sudo chown $ user : $ group
find -print0
and find -print0
xargs -0
: this is such a struggle with possible spaces in the file names. Such files can be perceived chown
'ohm as two different. The first flag causes find
to display every found file with zero at the end (the end-of-line character in C), and the second flag tells xargs
that it needs to separate files from each other not by line breaks, but by this very zero, which guarantees correct processing of even the most tricky file names :)sudo find / -user $ old_uid -print0 | xargs -0 sudo chown $ user
sudo find / -group $ old_gid -print0 | xargs -0 sudo chown : $ group
find
'u will have to rustle twice over the entire hard disk.find
to perform two operations for us in parallel, reducing the number of reads from the disk exactly twice. To do this, we use the grouping of conditions and find
commands with parentheses (not forgetting to screen them: otherwise the shell will be taken for them) and sending the operator “comma” between them: then both brackets will be executed for each file.-user=$old_uid
, and in the second - with -group=$old_gid
, and we will process these files separately. The search condition is now divided into two brackets for each file, and if the condition is met, the -fprint0
command writes the path to the found orphaned file to the corresponding temporary file.chownlist = $ ( tempfile ) chgrplist = $ ( tempfile )
sudo find / \
\ ( -user $ old_uid -fprint0 " $ chownlist " \ ) , \ ( -group $ old_gid -fprint0 " $ chgrplist " \ )
find
list so that it does not climb there at all. This is done using a combination of the -path "folder"
condition and the -prune
, which prevents find
climbing into folders in the condition. We exclude from the search the folders '/ proc' and '/ sys'.-or
operator. This operator will fulfill the first condition, and the second only if the first one does not work. So, find
will check if the directory excluded from the listing (in which it will not search) has got to it, and if it does not, it will make lists of files.chownlist = $ ( tempfile ) chgrplist = $ ( tempfile )
sudo find / \
\ ( \ ( -path "/ proc" -or -path "/ sys" \ ) -prune \ ) -or \ # exclude folders
\ ( \ ( -user $ old_uid -fprint0 " $ chownlist " \ ) , \ ( -group $ old_gid -fprint0 " $ chgrplist " \ ) \ )
chown
. Let me remind you that we tried to fully retain the owner of the files, taking into account the possibly different user and group.cat " $ chownlist " | xargs -0 sudo chown $ user
cat " $ chgrplist " | xargs -0 sudo chown : $ group
sudo rm " $ chownlist " " $ chgrplist " # Do not forget to clean up
find -nouser
and find -nogroup
. It is also useful to refine the search by excluding the notorious '/ proc' and '/ sys' from the search:sudo find / -nouser -or -nogroup -print
# === Settings
user = mysql new_uid = 600 old_uid = $ ( id -u $ user ) # name, new and old UID
group = mysql new_gid = 600 old_gid = $ ( id -g $ user ) # name, new and old GID
# === UID & GID change
sudo usermod -u $ new_uid $ user
sudo groupmod -g $ new_gid $ group
# === Search for files
chownlist = $ ( tempfile ) chgrplist = $ ( tempfile ) sudo find / \
\ ( \ ( -path "/ proc" -or -path "/ sys" \ ) -prune \ ) -or \
\ ( \ ( -user $ old_uid -fprint0 " $ chownlist " \ ) , \ ( -group $ old_gid -fprint0 " $ chgrplist " \ ) \ )
# === chown and brush
cat " $ chownlist " | xargs -0 sudo chown $ user
cat " $ chgrplist " | xargs -0 sudo chown : $ group
sudo rm " $ chownlist " " $ chgrplist "
Source: https://habr.com/ru/post/81715/
All Articles