Linux: changing file ownership without a copy?
If you create a separate user just to handle the chown, you can give that user the CAP_CHOWN capability, and you can have a single executable owned by that user that has the setuid bit set on it (so it executes as that user).
For security, this executable should do as little as possible, with as many checks as possible.
It should do the chown for the server user after the server user does the move. It should exist in a directory that is not writable by other users; it can do checks to insure that it is happy with all the attributes of the files it is asked to chown (current owner, location, etc.), it can have the server user hard-coded (so nobody else can use it), etc.
This will probably have to be a small C program, since most systems don't let you use setuid with scripts. You can find several small example programs on the web that do chown -- one is here
How to automatically change the ownership of every files in a directory
I am not sure what you want to achieve, so here's a couple of suggestions:
if the goal is simply for the file to have specific permission settings, and ownership change is secondary, you could set the permissions correctly on the source computer and then use a transfer method that preserves permissions (scp -p or some such)
note that a script owned by A can still be run by B if A has set the permissions correctly (group and other executable bit). When B runs A's script, it runs with B's permissions, so changing permission bits of B's file will work
if ownership change is imperative, you could transfer the file to B, make sure A has permission to read (see above), and then have A make a copy of the file to A's own directory using the cp command. The copy will be owned by A and thus A can change permissions of the copy. After that, run some regular process to clean up B's directory in order to save space if that's an issue
alternatively, you could have B on the source computer log into A's account on the receiving computer, and then the file ends up in A's ownership. If you do not want to give B the password of A, you could do this using an ssh certificate. But of course this has other security risks, so the cp method above is safer
finally, on unix it is generally not a good idea to make files writable for "other", and often not even for "group", so permission settings of 775 or 755 are much safer and preferred. Otherwise anyone on the same computer could modify the file or make it an empty file. Even on a private computer this is good practice, because sooner or later one of these files will get copied to a multi-user system and no-one remembers to check the permissions
Hopefully some of these hints are useful.
Allowing a user to edit a file without owning it in Linux
You should have a user that has associated group, named after that user. So you can do the following:
sudo chgrp -R YOUR_USER_NAME YOUR_FOLDER
this should change owinging group for the data in your folder and that owning group will be your user's group
Then change the privilige for the group using:
chmod -R g+w YOUR_FOLDER
Shell script to copy,rename and change owner permissions of file
Interesting challenge, I'll bite, but I'll have to use more tools than you said, since it cannot be done otherwise.
Problem 1 - changing the case of the file names:
Bash has variable expansions that let you change the casing in a variable.
f="my FILENAME in Mixed cap"
g=${f,,} # $g is now $f all in lower case; ${f,} would only lower the first letter
h=${g^} # $h is now $g with the first letter capitalized; ${g^^} would upper c the whole string
Problem 2 - changing ownership
This can only be done using chown
, there is no way around it.
chown new_owner file
In most cases, you won't have permission to do this, so you would need sudo
to do it, or run the whole script as root
. However, if you want the new owner to be yourself, that will happen by default when you run cp
, so you won't need to do anything special to make it happen.
Problem 3 - changing permissions
Here, you need chmod
, and again, there is no way around it.
chmod new_permission file
Problem 4 - cd source invalidates dest path
If the destination directory is not an absolute path, cd $source
will invalidate it. We can use pwd
to fix that.
dest=some/dir
if [[ $dest !~ ^/ ]]; then
dest=`pwd`/$dest
fi
This bit of script uses bash's built-in regular expression matching checks if the first character of the destination directory is a slash, in which case it is an absolute path (I'm assuming a *nix OS here). Otherwise, we make it absolute by prepending it with the current directory. I'm also assuming you're using bash 4.
Putting it together
#!/bin/bash
source=$1
dest=$2
owner=$3
perm=$4
if [[ ! $dest =~ ^/ ]]; then
dest=`pwd`/$dest
fi
cd $source
for f in *; do
g=${f,,}
h=${g^}
cp "$f" "$dest/$h"
chown "$owner" "$dest/$h"
chmod "$perm" "$dest/$h"
done
Calling this script
You could use read
to read in the parameters from the terminal, but I much prefer using command-line arguments.
Call the script what you want, say script.sh
, make it executable, and invoke it this way:
script.sh source_path dest_path new_owner new_perms
Caveats
You didn't say you wanted recursive copying, so I didn't handle directories in
$source
. That could be handled too, but would be quite a bit more complex.I've done no error checking. I've tested my script and it works, but make sure
$dest
exists before you start, and that it doesn't contain anything you don't mind overwritten!
changing the owner of folder in linux
Use chown
to change ownership and chmod
to change rights.
use the -R
option to apply the rights for all files inside of a directory too.
Note that both these commands just work for directories too. The -R
option makes them also change the permissions for all files and directories inside of the directory.
For example
sudo chown -R username:group directory
will change ownership (both user and group) of all files and directories inside of directory and directory itself.
sudo chown username:group directory
will only change the permission of the folder directory but will leave the files and folders inside the directory alone.
you need to use sudo to change the ownership from root to yourself.
Edit:
Note that if you use chown user: file
(Note the left-out group), it will use the default group for that user.
Also
You can change the group ownership of a file or directory with the command:
chgrp group_name file/directory_name
You must be a member of the group to which you are changing ownership to.
You can find group of file as follows
# ls -l file
-rw-r--r-- 1 root family 0 2012-05-22 20:03 file
# chown sujit:friends file
User 500 is just a normal user. Typically user 500 was the first user on the system, recent changes (to /etc/login.defs) has altered the minimum user id to 1000 in many distributions, so typically 1000 is now the first (non root) user.
What you may be seeing is a system which has been upgraded from the old state to the new state and still has some processes knocking about on uid 500. You can likely change it by first checking if your distro should indeed now use 1000, and if so alter the login.defs file yourself, the renumber the user account in /etc/passwd and chown/chgrp all their files, usually in /home/, then reboot.
But in answer to your question, no, you should not really be worried about this in all likelihood. It'll be showing as "500" instead of a username because o user in /etc/passwd has a uid set of 500, that's all.
Also you can show your current numbers using id i'm willing to bet it comes back as 1000 for you.
Related Topics
How to Include Cutil.H in Linux
Version Control for My Web Server
How Does The Os Know Disk Address of an Absent Page
Qwidget/X11: Prevent Window from Beeing Activated/Focussed by Mouse Clicks
How to Couple Xargs with Pdftotext Converter to Search Inside Multiple PDF Files
How to Reset Ipython/Jupyter Profile After Upgrade to 'Factory' Defaults
Sysfs Interface. I Can't Export Gpio Pins in a Xilinx's Board (Zybo and Other)
Ssl and Tkinter Not Present on Source Build of Python 3.5.2, Debian Linux
Understanding Glibc Malloc Trimming
Difference Between ./Executable and . Executable
When Compiling Programs to Run Inside a Vm, What Should March and Mtune Be Set To
Data Pointers in Inode Data Structure
Find Ip Address of My System for a Particular Interface with Shell Script (Bash)
How to Upgrade a Package in Linux That Was Built from Source