How does Linux copy files to other users?

  
                

In the Linux system, many people don't know how to copy files between different users. In fact, there are many ways to copy files to another user. Today, Xiaobian will introduce you to the Linux copy files to other users. Kind of method, let's learn together.

Scenario:

There are a file.txt file foo under the user's home directory, which you want to copy to bar the user's home directory. Linux has strict permission restrictions on the user's home directory. Non-owner users or group users have no permission to read or write, unless it is root (the supreme root). If there is no root permission, is there any way to copy file.txt to the home directory of the bar user?

Solution:

The first method, first log in with foo user, copy the file to the system temporary directory /tmp, then switch to the bar user, and then from the system temporary directory /tmp Copy the file to your home directory. Why use cp without mv here? Because the file owner to /tmp is still foo, by default other users have read access and no write permission (naturally no move permission). Even by modifying the file permissions, let bar be writable, move to the home directory of bar home or foo, and root must be changed to bar. This method is a bit tortuous, and the drawbacks are obvious. The file needs to be copied twice and takes twice as long.

# cp file.txt /tmp/

# su - bar

# cp /tmp/file.txt ~/

# exit

# rm /tmp/file.txt

The second method is to use the scp command. The original scp is used to copy files over the network on different hosts, just used here. Log in as the bar user

# scp foo@localhost:/home/foo/file.txt . /

Enter the foo user password to start file transfer. You can also log in as foo user,

# scp file.txt bar@localhost:/home/bar/

Enter the bar user password, the process is the same.

The above is a description of the method of copying files from Linux to another user. This article only introduces two more practical methods. You can also use other methods to copy them. This article will not introduce them one by one.

Copyright © Windows knowledge All Rights Reserved