Does file user ownership change when transferring files between computers

file-permissionspermissionsunix

Scenario: A file created under computer A (userA), which takes file ownership as userA in file permissions, is then transferred to computer B with a different user (userB)….

Does the ownership of the original file change from the original user (userA) in computer A to the user (userB) of computer B?

How can I create a file that is only writable by me, the creator and owner, and only readable to anyone who might receive that file on other computers?

I created a testfile.txt with file permissions 755 on computer A (userA) to compare the permissions and ownerships of this file before and after I transferred it using scp from computer A to B. I noticed how my original file, now on computer B, has the user id as userB instead of userA [where the file was created].

Computer A, having 'userA'

rwx-r-x-r– userA testfile.txt

Computer B, having 'userB'

rwx-r-x-r– userB testfile.txt

I thought, and wanted, to make the file only readable, writeable, and executable by the owner (which I thought would be the user from the computer I created the file in).

Thanks! I am new at this!

Best Answer

Yes.

This all depends on who creates the file on the destination. Try this:

$ touch some_file
$ ls -l some_file
-rw-r--r-- 1 userA userA 0 Apr 9 17:44 some_file
$ ls -ln some_file
-rw-r--r-- 1 501 501 0 Apr 9 17:44 some_file

So in my example, userA's numeric uid is 501.

Now transfer it, logging into the remote system as userB:

$ scp some_file userB@computerB:
$ ssh userB@computerB ls -l some_file
-rw-r--r-- 1 userB users 0 Apr 9 17:50 some_file
$ ssh userB@computerB ls -l some_file
-rw-r--r-- 1 1743 20 0 Apr 9 17:50 some_file

As you see here, userB created the file, and userB has numeric uid 1743. See also how the timestamp changed?

This is the default behavior of scp. You can preserve attributes though by using scp's "-p" option. This only preserves timestamp and permissions -- and importantly, not ownership. This might be exactly what you're looking for:

$ scp -p some_file userB@computerB:
$ ssh userB@computerB ls -l some_file
-rw-r--r-- 1 userB users 0 Apr 9 17:44 some_file
$ ssh userB@computerB ls -l some_file
-rw-r--r-- 1 1743 20 0 Apr 9 17:44 some_file

Note that besides scp, there are many different ways of creating files on remote machines -- NFS, FTP, WebDAV... these will behave in different, but similarly predictable ways. Let's not get carried away though -- you asked about scp.

(OT note, you actually created the file with 754 permissions! rwx=111=7, r-x=101=5, r--=100=4 – you see, r, w and x are bits in an octal word where r=4, w=2, x=1. That's why you'll see references to octal in relation to permissions. Thanks ernie for the correction!)