No. No need for sudo with cp in this case.
If you use
sudo cp
to copy the file to the users home directory, the copy of the file will be owned by root, because
cp
was running as root.
So after copying the file using
sudo cp
, you'd also have to use
sudo chown alex:alex ~/.xinitrc
in order to give the user ownership of the file.
Whereas if you use
cp
on its own, without
sudo
:
Assuming the default 644 permissions are used on the file - the user has read access to the file. And as a normal user, read permissions are all you need in order to copy a file that is owned by root. And again, it will only succeed in copying the file if the destination the file is going to is a location that the user
can write to.
In other words, as long as the directory the user is writing the copied file to is either directly owned by them, or they have write permissions via group membership, or perhaps even the other/world permissions (e.g. Perhaps they're copying to a shared directory that is readable/writable by anybody).
As long as the destination the copied file is going to is a location that the user
can write to (e.g. /home/alex/.xinitrc), then the file will be copied. And the copy will be owned by the user.
If you try using
cp
to copy a file owned by root to a location that is only writable by root,
then you'd definitely
need to use
sudo
. Otherwise, it is
NOT needed.
So to sum up - without using
sudo
:
Because the
cp
command is acting as a normal user
AND the user has read permissions on the original file
AND because the user has permission to write to the destination - then the original file (owned by root)
will be copied and the copied file will be owned by the user.
So as I originally recommended, simply use:
Bash:
cp /etc/X11/xinit/xinitrc ~/.xinitrc