date int64 1,220B 1,719B | question_description stringlengths 28 29.9k | accepted_answer stringlengths 12 26.4k | question_title stringlengths 14 159 |
|---|---|---|---|
1,412,587,091,000 |
I'm starting X as a user and need to set my keyboard brightness in /sys/class/leds/asus\:\:kbd_backlight/brightness. The /sys/ directory gets recreated after reboot, so the permissions will reset too. How do I set it up so I don't need to make the file writable by all users after every boot?
I'm using Archlinux with SDDM as the login manager and KDE as DE.
|
No you can't, the permission of sysfs is defined in kernel space and can't be changed with userspace tools (unless with kernel side support).
But for your own problem, you could setup a sudo entry that allow everyone to write to that path, i.e ALL ALL = (ALL) NOPASSWD: /usr/bin/tee /sys/class/leds/asus\:\:kbd_backlight/brightness
And when you write to that directory, use a script like this, echo 1 | sudo /usr/bin/tee "/sys/class/leds/asus::kbd_backlight/brightness"
| How to set permissions in /sys/ permanent? |
1,412,587,091,000 |
When SELinux is installed on a system are its rules enforced before or after the standard linux permissions? For example if a non-root linux user tries to write to a file with linux permission -rw------- root root will SELinux rules be checked first or will standard filesystem permissions apply and SELinux never invoked?
|
I see the terms to search for now are MAC and DAC. DAC is the standard permission system. MAC is the system used by SELinux.
The answer to quote one source is:
It is important to remember that SELinux policy rules are checked
after DAC rules. SELinux policy rules are not used if DAC rules deny
access first.
This diagram shows:
References:
https://selinuxproject.org/page/NB_MAC
https://www.centos.org/docs/5/html/Deployment_Guide-en-US/selg-overview.html
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Security-Enhanced_Linux/chap-Security-Enhanced_Linux-Introduction.html
| Are SELinux rules enforced before or after standard linux permissions? |
1,412,587,091,000 |
Today the /tmp directory filled up on a machine at work. The problem was, it was on the root partition which wasn't very big. In order to fix this, a co-worker created a /new/tmp directory elsewhere, copied all the contents to the new directory, removed the original /tmp and made a symlink /tmp -> /new/tmp.
When he copied the files (really, this was somebody else, not me!) he didn't use -a so the owner of every file under /new/tmp was root. Furthermore, he didn't set the permissions of the /new/tmp directory so it was the default 0755. This caused no end of trouble and even tweaking mode and ownership bits failed to restore the machine to an acceptably working state. I ended up having to nuke everything in /tmp and reboot.
The /tmp directory contained various sockets and pipes and whatnot, since a bunch of people run Gnome through VNC, and I use screen which has its own pipes.
Is there a safe way to move a /tmp directory to a different volume on a running system? I'm not sure what I would have actually done to keep everything working. I'm particularly curious about what happens to pipes and sockets.
|
On “client” machines, the safe way to move /tmp is to reboot. Here, by client, I mean anything that runs programs that put sockets in /tmp, in particular X servers and screen.
The new /tmp definitely needs to have the right permissions (1777), otherwise you can't hope to have a working system.
For /tmp, you pretty much can't copy any files. That's because most of the time, programs that put stuff in /tmp open the files. If you copy the file, that copies the contents, but the programs still have the old files open. You might be able to reach into them with a debugger (ptrace), but this will be a lot more complicated than rebooting, and with many programs all you'd do is crash them anyway.
If your /tmp is full and you want to switch to a new one live, you need to restart all programs that have files open there. Since that means restarting X and screen sessions, it's not much better than rebooting.
You should be able to switch for new programs but keep existing open files in place by using a union mount. (The principle is sound, but I've never tried, so there may be by-me-unexpected issues.) Here's a way to do this on Linux.
Keep all existing files in /tmp except for a few manually-selected big ones.
Create a /tmp.new (mode 1777).
Expose /tmp on a different path: mount --bind / /.root.only. This is necessary because the next step will shadow /tmp. There may be different union mount implementations that don't require this step.
Make a union mount of /.root.only/tmp and /tmp.new, mounted on /tmp. This way new files created in /tmp will be written in /tmp.new, but files in /.root.only/tmp are also visible under /tmp. One possibility is
unionfs-fuse: unionfs-fuse /tmp.new:/.root.only/tmp /tmp.
If you don't want to go the union mount root (e.g. because it's not available on your platform, or because it's too much trouble), at least do not delete the old directory. Move it, so that running programs will keep using the old directory and new programs will use the new one. (Of course new programs won't be able to communicate with old programs through sockets or pipe in /tmp unless you set TMPDIR or otherwise tell them where to look.)
mv /tmp /tmp.old && mkdir /tmp
| How to (safely) move /tmp to a different volume? |
1,412,587,091,000 |
If the file is a plain text file created by root with:
echo 'foo' > ./file.txt
Your ls -l is:
-rw-r--r-- root root ./file.txt
But as a regular user, I can change this with vim saving with :w! or with a sed command and when this happened the user and group that owns this file is changed to:
-rw-r--r-- user user ./file.txt
After I noticed that when removed others read permission with chmod o-r ./file.txt I can't do the changes anymore, but when restored with chmod o+r ./file.txt I am able to again.
What is happening here? Why does the "others" read permission enable me to change a file owned by root and also changes the user and group ownership?
Why is this happening?
PS: I'm use Debian SID.
|
This is happening because of two things:
vim (at least in this case) and sed, when doing in place editing, actually delete the original file and then create a new one with the same name.
the ability to delete a file depends on the permissions of the directory containing the file, not on the permissions of the file itself.
So, what is happening here is that you have write permission on the directory, which means you can change the directory's contents, including deleting and creating files. So when you run your sed -i or save with :w!, you are deleting the original and then creating a new file. This is also why the ownership changes: this is actually a different file.
You can demonstrate this by checking the file's inode before and after editing:
$ ls -ld foo/
drwxr-xr-x 2 terdon terdon 266240 Nov 16 13:43 foo/
$ cd foo
$ sudo sh -c 'echo foo > file'
$ ls -l
total 4
-rw-r--r-- 1 root root 4 Nov 16 13:43 file
After those commands, I have file, owned by root, in the directory foo/ to which my regular user has write permission. Now, let's use ls -i to check the inode, and then make a changed with sed and check again:
$ ls -li file
26610890 -rw-r--r-- 1 root root 4 Nov 16 13:43 file
$ sed -i 's/foo/bar/' file
$ ls -li file
26610859 -rw-r--r-- 1 terdon terdon 4 Nov 16 15:40 file
You can also see vim doing the same thing by running
strace vim file 2> strace.out
Then editing the file and saving with :w!. In your strace.out, you will see:
unlink("file") = 0
open("file", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 4
write(4, "bar\n", 11) = 11
So, the file was first deleted (unlink("file")), then a new file of the same name was created (open("file", O_WRONLY|O_CREAT|O_TRUNC, 0644)) and the modifications I had made were written to it (write(4, "bar\n", 11)).
As you can see above, the inode changed: this is a new file with the same name. So you did not actually change a file you didn't have write access to, you changed a directory to which you did have write access by deleting a file in that directory and then creating a new file in the directory with the same name as the old one.
I have answered a similar question here: https://askubuntu.com/a/815849/85695.
| Regular user is able to modify a file owned by root |
1,412,587,091,000 |
Possible Duplicate:
How to apply recursively chmod directories without affecting files?
What is the command to apply execute permission for directories (for traversal), but leave the execute bit off for files contained in the directory?
|
If you don't want to remove the executable bit from existing files you can use the X mode. To recursively set the executable bit on all directories use:
chmod -R a+X dir
From man chmod:
execute/search only if the file is a directory or already has
execute permission for some user (X)
| execute bit on directories, but not files [duplicate] |
1,412,587,091,000 |
I was reading up on chmod and its octal modes. I saw that 1 is execute only. What is a valid use case for an execute only permission? To execute a file, one typically would want read and execute permission.
$ echo 'echo foo' > say_foo
$ chmod 100 ./say_foo
$ ./say_foo
bash: ./say_foo: Permission denied
$ chmod 500 ./say_foo
$ ./say_foo
foo
|
Shell scripts require the read permission to be executed, but binary files do not:
$ cat hello.cpp
#include<iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
$ g++ -o hello hello.cpp
$ chmod 100 hello
$ ./hello
Hello, world!
$ file hello
hello: executable, regular file, no read permission
Displaying the contents of a file and executing them are two different things. With shell scripts, these things are related because they are "executed" by "reading" them into a new shell (or the current one), if you'll forgive the simplification. This is why you need to be able to read them. Binaries don't use that mechanism.
For directories, the execute permission is a little different; it means you can do things to files within that directory (e. g. read or execute them). So let's say you have a set of tools in /tools that you want people to be able to use, but only if they know about them. chmod 711 /tools. Then executable things in /tools can be run explicitly (e. g. /tools/mytool), but ls /tools/ will be denied. Similarly, documents could be stored in /private-docs which could be read if and only if the file names are known.
| What is a valid use case for an "execute only" file permission? |
1,412,587,091,000 |
I have a personal folder /a/b on the server with permission 700. I don't want others to list the contents in /a/b. The owner of /a is root.
Now I need to open the full authorities of directory /a/b/c to all users.
I changed the permission of /a/b/c to 777 but it is still inaccessible for others.
|
You can. You just have to set the executable bit on the /a/b directory. That will prevent being able to see anything in b, but you can still do everything if you go directly to a/b/c.
% mkdir -p a/b/c
% chmod 711 a/b
% sudo chown root a/b
% ll a/b
ls: cannot open directory a/b: Permission denied
% touch a/b/c/this.txt
% ls a/b/c
this.txt
Beware that while others cannot list the contents of /a/b, they can access files in that directory if they guess the name of the file.
% echo hello | sudo tee a/b/f
% cat a/b/f
hello
% cat a/b/doesntexist
cat: a/b/doesntexist: No such file or directory
So be sure to maintain proper permissions (no group/world) on all other files/directories within the b directory, as this will avoid this caveat.
| Can I make a public directory under a private directory? |
1,412,587,091,000 |
I think I rather understand how file permissions work in Linux. However, I don't really understand why they are split into three levels and not into two.
I'd like the following issues answered:
Is this deliberate design or a patch? That is - was the owner/group permissions designed and created together with some rationale or did they come one after another to answer a need?
Is there a scenario where the user/group/other scheme is useful but a group/other scheme will not suffice?
Answers to the first should quote either textbooks or official discussion boards.
Use cases I have considered are:
private files - very easily obtainable by making a group per-user, something that is often done as is in many systems.
allowing only the owner (e.g. system service) to write to a file, allowing only a certain group to read, and deny all other access - the problem with this example is that once the requirement is for a group to have write access, the user/group/other fails with that. The answer for both is using ACLs, and doesn't justify, IMHO, the existence of owner permissions.
NB I have refined this question after having the question closed in superuser.com.
EDIT corrected "but a group/owner scheme will not suffice" to "...group/other...".
|
History
Originally, Unix only had permissions for the owning user, and for other users: there were no groups. See the documentation of Unix version 1, in particular chmod(1). So backward compatibility, if nothing else, requires permissions for the owning user.
Groups came later. ACLs allowing involving more than one group in the permissions of a file came much later.
Expressive power
Having three permissions for a file allows finer-grained permissions than having just two, at a very low cost (a lot lower than ACLs). For example, a file can have mode rw-r-----: writable only by the owning user, readable by a group.
Another use case is setuid executables that are only executable by one group. For example, a program with mode rwsr-x--- owned by root:admin allows only users in the admin group to run that program as root.
“There are permissions that this scheme cannot express” is a terrible argument against it. The applicable criterion is, are there enough common expressible cases that justify the cost? In this instance, the cost is minimal, especially given the other reasons for the user/group/other triptych.
Simplicity
Having one group per user has a small but not insignificant management overhead. It is good that the extremely common case of a private file does not depend on this. An application that creates a private file (e.g. an email delivery program) knows that all it needs to do is give the file the mode 600. It doesn't need to traverse the group database looking for the group that only contains the user — and what to do if there is no such group or more than one?
Coming from another direction, suppose you see a file and you want to audit its permissions (i.e. check that they are what they should be). It's a lot easier when you can go “only accessible to the user, fine, next” than when you need to trace through group definitions. (Such complexity is the bane of systems that make heavy use of advanced features such as ACLs or capabilities.)
Orthogonality
Each process performs filesystem accesses as a particular user and a particular group (with more complicated rules on modern unices, which support supplementary groups). The user is used for a lot of things, including testing for root (uid 0) and signal delivery permission (user-based). There is a natural symmetry between distinguishing users and groups in process permissions and distinguishing users and groups in filesystem permissions.
| Is there a reason why 'owner' permissions exist? Aren't group permissions enough? |
1,348,306,435,000 |
If I run:
sudo chown -R user:user /
Can I revert it to what it was before I ran it?
|
In short: no.
You'll need to restore from a backup. (Some backup tools might have options to only restore permission, others can list backed-up files with their permissions and you can use that to fix your system.)
If you don't have a backup, you'll need to fix all that manually.
| How to revert chown command? |
1,348,306,435,000 |
I have a card reader attached on /dev/sdb.
What I do is giving all permissions to owner, group and the rest of the world, using:
sudo chmod 777 /dev/sdb
Can I just use another combination, allowing only the owner (me) to use the card reader?
There is only one user account.
|
There are multiple ways of accomplishing this.
1. Add your user to the group that owns the device
Generally in most distros, block devices are owned by a specific group. All you need to do is add your user to that group.
For example, on my system:
# ls -l /dev/sdb
brw-rw---- 1 root disk 8, 16 2014/07/07-21:32:25 /dev/sdb
Thus I need to add my user to the disk group.
# usermod -a -G disk patrick
2. Change the permissions of the device
The idea is to create a udev rule to run a command when the device is detected.
First you need to find a way to identify the device. You use udevadm for this. For example:
# udevadm info -a -n /dev/sdb
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
looking at device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3/1-1.3:1.0/host6/target6:0:0/6:0:0:0/block/sdb':
KERNEL=="sdb"
SUBSYSTEM=="block"
DRIVER==""
ATTR{ro}=="0"
ATTR{size}=="31116288"
ATTR{stat}==" 279 219 3984 1182 0 0 0 0 0 391 1182"
ATTR{range}=="16"
ATTR{discard_alignment}=="0"
ATTR{events}=="media_change"
ATTR{ext_range}=="256"
ATTR{events_poll_msecs}=="-1"
ATTR{alignment_offset}=="0"
ATTR{inflight}==" 0 0"
ATTR{removable}=="1"
ATTR{capability}=="51"
ATTR{events_async}==""
looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3/1-1.3:1.0/host6/target6:0:0/6:0:0:0':
KERNELS=="6:0:0:0"
SUBSYSTEMS=="scsi"
DRIVERS=="sd"
ATTRS{rev}=="0207"
ATTRS{type}=="0"
ATTRS{scsi_level}=="0"
ATTRS{model}=="STORAGE DEVICE "
ATTRS{state}=="running"
ATTRS{queue_type}=="none"
ATTRS{iodone_cnt}=="0x184"
ATTRS{iorequest_cnt}=="0x184"
ATTRS{device_busy}=="0"
ATTRS{evt_capacity_change_reported}=="0"
ATTRS{timeout}=="30"
ATTRS{evt_media_change}=="0"
ATTRS{max_sectors}=="240"
ATTRS{ioerr_cnt}=="0x2"
ATTRS{queue_depth}=="1"
ATTRS{vendor}=="Generic "
ATTRS{evt_soft_threshold_reached}=="0"
ATTRS{device_blocked}=="0"
ATTRS{evt_mode_parameter_change_reported}=="0"
ATTRS{evt_lun_change_reported}=="0"
ATTRS{evt_inquiry_change_reported}=="0"
ATTRS{iocounterbits}=="32"
ATTRS{eh_timeout}=="10"
looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3/1-1.3:1.0/host6/target6:0:0':
KERNELS=="target6:0:0"
SUBSYSTEMS=="scsi"
DRIVERS==""
looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3/1-1.3:1.0/host6':
KERNELS=="host6"
SUBSYSTEMS=="scsi"
DRIVERS==""
looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3/1-1.3:1.0':
KERNELS=="1-1.3:1.0"
SUBSYSTEMS=="usb"
DRIVERS=="usb-storage"
ATTRS{bInterfaceClass}=="08"
ATTRS{bInterfaceSubClass}=="06"
ATTRS{bInterfaceProtocol}=="50"
ATTRS{bNumEndpoints}=="02"
ATTRS{supports_autosuspend}=="1"
ATTRS{bAlternateSetting}==" 0"
ATTRS{bInterfaceNumber}=="00"
looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3':
KERNELS=="1-1.3"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="00"
ATTRS{devpath}=="1.3"
ATTRS{idVendor}=="05e3"
ATTRS{speed}=="480"
ATTRS{bNumInterfaces}==" 1"
ATTRS{bConfigurationValue}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{busnum}=="1"
ATTRS{devnum}=="5"
ATTRS{configuration}==""
ATTRS{bMaxPower}=="500mA"
ATTRS{authorized}=="1"
ATTRS{bmAttributes}=="80"
ATTRS{bNumConfigurations}=="1"
ATTRS{maxchild}=="0"
ATTRS{bcdDevice}=="0207"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{quirks}=="0x0"
ATTRS{serial}=="000000000207"
ATTRS{version}==" 2.00"
ATTRS{urbnum}=="1115"
ATTRS{ltm_capable}=="no"
ATTRS{manufacturer}=="Generic"
ATTRS{removable}=="unknown"
ATTRS{idProduct}=="0727"
ATTRS{bDeviceClass}=="00"
ATTRS{product}=="USB Storage"
looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1/1-1':
KERNELS=="1-1"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="01"
ATTRS{devpath}=="1"
ATTRS{idVendor}=="8087"
ATTRS{speed}=="480"
ATTRS{bNumInterfaces}==" 1"
ATTRS{bConfigurationValue}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{busnum}=="1"
ATTRS{devnum}=="2"
ATTRS{configuration}==""
ATTRS{bMaxPower}=="0mA"
ATTRS{authorized}=="1"
ATTRS{bmAttributes}=="e0"
ATTRS{bNumConfigurations}=="1"
ATTRS{maxchild}=="6"
ATTRS{bcdDevice}=="0000"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{quirks}=="0x0"
ATTRS{version}==" 2.00"
ATTRS{urbnum}=="61"
ATTRS{ltm_capable}=="no"
ATTRS{removable}=="unknown"
ATTRS{idProduct}=="0024"
ATTRS{bDeviceClass}=="09"
looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb1':
KERNELS=="usb1"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="00"
ATTRS{devpath}=="0"
ATTRS{idVendor}=="1d6b"
ATTRS{speed}=="480"
ATTRS{bNumInterfaces}==" 1"
ATTRS{bConfigurationValue}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{authorized_default}=="1"
ATTRS{busnum}=="1"
ATTRS{devnum}=="1"
ATTRS{configuration}==""
ATTRS{bMaxPower}=="0mA"
ATTRS{authorized}=="1"
ATTRS{bmAttributes}=="e0"
ATTRS{bNumConfigurations}=="1"
ATTRS{maxchild}=="3"
ATTRS{bcdDevice}=="0313"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{quirks}=="0x0"
ATTRS{serial}=="0000:00:1d.0"
ATTRS{version}==" 2.00"
ATTRS{urbnum}=="26"
ATTRS{ltm_capable}=="no"
ATTRS{manufacturer}=="Linux 3.13.6-gentoo ehci_hcd"
ATTRS{removable}=="unknown"
ATTRS{idProduct}=="0002"
ATTRS{bDeviceClass}=="09"
ATTRS{product}=="EHCI Host Controller"
looking at parent device '/devices/pci0000:00/0000:00:1d.0':
KERNELS=="0000:00:1d.0"
SUBSYSTEMS=="pci"
DRIVERS=="ehci-pci"
ATTRS{irq}=="23"
ATTRS{subsystem_vendor}=="0x144d"
ATTRS{broken_parity_status}=="0"
ATTRS{class}=="0x0c0320"
ATTRS{companion}==""
ATTRS{enabled}=="1"
ATTRS{consistent_dma_mask_bits}=="32"
ATTRS{dma_mask_bits}=="32"
ATTRS{local_cpus}=="0f"
ATTRS{device}=="0x1e26"
ATTRS{uframe_periodic_max}=="100"
ATTRS{msi_bus}==""
ATTRS{local_cpulist}=="0-3"
ATTRS{vendor}=="0x8086"
ATTRS{subsystem_device}=="0xc0d3"
ATTRS{numa_node}=="-1"
ATTRS{d3cold_allowed}=="1"
looking at parent device '/devices/pci0000:00':
KERNELS=="pci0000:00"
SUBSYSTEMS==""
DRIVERS==""
Then create a new file in /etc/udev/rules.d, such as 99-cardreader.rules:
SUBSYSTEM=="block", ATTRS{idProduct}=="0727", ATTRS{serial}=="000000000207", ACTION=="add", RUN+="/bin/chmod 777 /dev/$name"
Here I used the output from the udevadm info command to find some identifying information for the device. I used the SUBSYSTEM="block" entry for the very first entry, and then the ATTRS values from the 6th entry. This will basically find the USB device with that product & serial number, and then find the block device that results from that USB device.
The RUN command will change the permissions on the device to 777. However I don't consider this a very good solution as this opens the device up to the world. Instead a better solution might be:
SUBSYSTEM=="block", ATTRS{idProduct}=="0727", ATTRS{serial}=="000000000207", ACTION=="add", RUN+="/bin/setfacl -m u:patrick:rw- /dev/$name"
This will grant the user patrick read/write access to the device.
Note: It is important to remember that when writing udev rules, you can only use parameters from the top device, and one other device in the chain. Thus I can use the SUBSYSTEM="block" parameter, and the ATTRS parameters. But I could not use any parameters from any other device in the chain, or the rule would fail to match.
| Give a specific user permissions to a device without giving access to other users |
1,348,306,435,000 |
I need to create a service for a web server called daphne I would like to know what are the correct linux permissions for this. or if exists a general rule for whatever systemd service?
|
Any local user can read the definition of any systemd system unit through the DBus interface (for example using systemctl show someUnitName), unless you have a custom DBus policy in place to prevent this.
Making the unit file not world-readable thus makes no sense and systemd will print a warning if applicable. Similarly, it will also warn if the unit file is marked executable.
Unless you want the unit file to be editable for a particular (non-root) user or group, stick to the same convention used for most other system files: 0644 root:root.
| What are the correct permissions for a systemd .service? |
1,348,306,435,000 |
I've edited my root cron tab to periodically execute a script located in a particular user's folder using this command:
sudo crontab -e
When cron runs the script, this is the output:
sh: 1: /home/user/Location/Of/Script: Permission denied
I thought that the root cron had permission to do anything. I have no issue when I manually run this script as root.
I've read in the documentation that further error info can be found here:
sudo cat /var/log/syslog
Here's what I found:
Jan 30 12:30:01 backup CRON[17702]: (CRON) info (No MTA installed, discarding output)
However, I think this is probably unrelated to the permission denied issue.
So what do I really need to do?
|
I think that your script is not executable. So, use the following command to make it:
chmod +x /home/user/Location/Of/Script
Or, if you are not the owner of that script:
sudo chmod +x /home/user/Location/Of/Script
| Root Cron Won't Run Script (permission denied) |
1,348,306,435,000 |
I am wondering why by default my directory /home/<user>/ has permissions set to 755. This allows other users to enter into directories and read files in my home. Is there any legitimate reason for this ?
Can I set the permissions to 700 for my home and all sub directories , for example:
chmod -R o-xw /home/<user>/
chmod -R g-xw /home/<user>/
without breaking anything ?
Also, is it possible to set the permissions on my home, so that all new files created will have 600 and directories 700 ?
|
If your home directory is private, then no one else can access any of your files. In order to access a file, a process needs to have execute permission to all the directories on the path down the tree from the root directory. For example, to allow other users to read /home/martin/public/readme, the directories /, /home, /home/martin and /home/martin/public all need to have the permissions d??x??x??x (it can be drwxr-xr-x, or drwx--x--x or some other combination), and additionally the file readme must be publicly readable (-r??r??r??).
It is common to have home directories with mode drwxr-xr-x (755) or at least drwx--x--x (711). Mode 711 (only execute permission) on a directory allows others to access a file in that directory if they know its name, but not to list the content of the directory. Under that home directory, create public and private subdirectories as desired.
If you never, ever want other people to read any of your files, you can make your home directory drwx------ (700). If you do that, you don't need to protect your files individually. This won't break anything other than the ability of other people to read your file.
One common thing that may break, because it's an instance of other people reading your files, is if you have a directory such as ~/public_html or ~/www which contains your web page. Depending on the web server's configuration, this directory may need to be world-readable.
You can change the default permissions for the files you create by setting the umask value in your .profile. The umask is the complement of the maximal permissions of a file. Common values include 022 (writable only by the owner, readable and executable by everyone), 077 (access only by the owner), and 002 (like 022, but also group-writable). These are maximal permissions: applications can set more restrictive permissions, for example most files end up non-executable because the application that created them didn't set the execute permission bits when creating the file.
| permissions 755 on /home/<user>/ |
1,348,306,435,000 |
When I list al groups I see one called 'nogroup'. What is this for? Is it supposed to be least privileged one or something? I'm using ubuntu 11.04.
|
nogroup is the group analog to the nobody user. It is used for unprivileged processes so that even if something goes wrong the process does not have the permissions to cause any serious damage to an important user or group.
| what is 'nogroup' group's purpose |
1,348,306,435,000 |
I would like to allow users to chmod a file that is owned by root or some user that is not themselves. I have chmod'ed the file to 777 and I get "operation not permitted". I have added the user to the group of the file and get the same. Why can't a user chmod a file they have write access to?
|
Why can't a user chmod a file they have write access to?
For the normal access rights this is a design decision. You need richacls: WRITE_ACL and maybe WRITE_OWNER.
| Can I allow users to chmod a file not owned by them? |
1,348,306,435,000 |
To set the sticky bit on a directory, why do the commands chmod 1777 and chmod 3777 both work?
|
1 1 1 1 1 1 1 1 1 1 1 1
___________ __________ __________ ___ ___ ___ ___ ___ ___ ___ ___ ___
setUID bit setGID bit sticky bit user group others
Each number (also referred to as an octal because it is base8) in that grouping represents 3 bits. If you turn it into binary it makes it a lot easier.
1 = 0 0 1
3 = 0 1 1
5 = 1 0 1
7 = 1 1 1
So if you did 1777, 3777, 5777, or 7777 you would set the sticky bit because the third column would be a 1. However, with 3777, 5777, and 7777 you are additionally setting other bits (SUID for the first column, and SGID for the second column).
Conversely, any other number in that spot (up to the maximum of 7) would not set the sticky bit because the last column wouldn't be a 1 or "on."
2 = 0 1 0
4 = 1 0 0
6 = 1 1 0
| Why does "chmod 1777" and "chmod 3777" both set the sticky bit? |
1,348,306,435,000 |
Is it possible for a user to have a write access to a file and not be able to read it? How is it possible?
I tried the following commands:
debianbox@debian:~/posix/io$ touch filetest
debianbox@debian:~/posix/io$ ls -l filetest
-rw-r--r-- 1 debianbox debianbox 0 14 oct. 03:10 filetest
debianbox@debian:~/posix/io$ echo "Hello World" > filetest
debianbox@debian:~/posix/io$ cat filetest
Hello World
debianbox@debian:~/posix/io$ chmod u-r filetest
debianbox@debian:~/posix/io$ cat filetest
cat: filetest: Permission forbidden
debianbox@debian:~/posix/io$
As you can see here, I have write but not read access on this file. How can this be possible? Is this considered as a bug? If not, in what situation would this be useful?
|
It is not a bug, it's a featureTM (Also, just a consequence of universal unix approach to permisions).
Apart from dropbox-like behavior in case of directories (as described by BillThor), write-only access is necessary for some special (pseudo-) files under /proc and /sys. Such files are used to set some driver or kernel properties or trigger a system action. You cannot read them, because they are used only for one-way signaling - you can only echo some text/data to them. To find such files, you can use
find /proc/[^0-9]* /sys -perm /222 ! -perm /444
Notice that, since these files are used for advanced system configuration (potentially dangerous), only root has write access to them (in most cases).
| Write access without read access |
1,348,306,435,000 |
How can I use, preferably a single chmod command, which will allow any user to create a file in a directory but only the owner of their file (the user who created it) can delete their own file but no one else's in that directory.
I was thinking to use:
chmod 755 directory
As the user can create a file and delete it, but won't that allow the user to delete other people's files?
I only want the person who created the file to be able to delete their own file. So, anyone can make a file but only the person who created a file can delete that file (in the directory).
|
The sticky bit can do more or less what you want. From man 1 chmod:
The restricted deletion flag or sticky bit is a single bit, whose interpretation depends on the file type. For directories, it prevents unprivileged users from removing or renaming a file in the directory unless they own the file or the directory; this is called the restricted deletion flag for the directory, and is commonly found on world-writable directories like /tmp.
That is, the sticky bit's presence on a directory only allows contained files to be renamed or deleted if the user is either the file's owner or the containing directory's owner (or the user is root).
You can apply the sticky bit (which is represented by octal 1000, or t) like so:
# instead of your chmod 755
chmod 1777 directory
# or, to add the bit to an existing directory
chmod o+t directory
| Allow all users to create files in a directory, but only the owner can delete |
1,348,306,435,000 |
On Unix, a long time back, I learned about chmod:
the traditional way to set permissions on Unix
(and to allow programs to gain privileges, using setuid and setgid).
I have recently discovered some newer commands on GNU/Linux:
setfacl extends the traditional ugo:rwx bits and the t bit of chmod.
setcap gives more fine-grained control over privileges
than the ug:s bits of chmod.
chattr allows some other controls (a bit of a mix) over the file.
Are there any others?
|
chmod: change file mode bits
Usage (octal mode):
chmod octal-mode files...
Usage (symbolic mode):
chmod [references][[operator][modes]] files...
references is a combination of the letters ugoa,
which specify which user's access to the files will be modified:
u the user who owns it
g other users in the file's group
o other users not in the file's group
a all users
If omitted, it defaults to all users,
but only permissions allowed by the umask are modified.
operator is one of the characters +-=:
+ add the specified file mode bits
to the existing file mode bits of each file
- removes the specified file mode bits
from the existing file mode bits of each file
= adds the specified bits and removes unspecified bits, except the setuid and setgid bits set for directories, unless explicitly specified.
mode consists of a combination of the letters rwxXst, which specify which permission bits are to be modified:
r read
w write
x (lower case X) execute (or search for directories)
X (capital) execute/traverse only if the file is a directory
or already has an execute bit set for some user category
s setuid or setgid (depending on the specified references)
t restricted deletion flag or sticky bit
Alternatively, the mode can consist of one of the letters ugo,
in which case case the mode corresponds to the permissions
currently granted to the owner (u), members of the file's group (g)
or users in neither of the preceding categories (o).
The various bits of chmod explained:
Access control (see also setfacl)
rwx — read (r), write (w), and execute/traverse (x) permissions
Read (r) affects if a file can be read, or if a directory can be listed.
Write (w) affects if a file can be written to,
or if a directory can be modified (files added, deleted, renamed).
Execute (x) affects if a file can be run,
use for scripts and other executable files.
Traverse (x), also known as "search",
affects whether a directory can be traversed;
i.e., whether a process can access (or try to access) file system objects
through entries in this directory.
s and t — sticky bit (t), and setgid (s) on directories
The sticky bit only affects directories. Will prevent anyone except file owner, and root, from deleting files in the directory.
The setgid bit on directories will cause new files and directories
to have the group set to the same group,
and new directories to have their setgid bit set
(see also defaults in setfacl).
s — setuid, setgid, on executable files
This can affect security in a bad way, if you don't know what you are doing.
When an executable is run, if one of these bits is set,
then the user/group of the executable
will become the effective user/group of the process.
Thus the program runs as that user.
See setcap for a more modern way to do this.
chown chgrp:
chattr: change file attributes
Usage:
chattr operator[attribute] files...
operator is one of the characters +-=:
+ adds the selected attributes to be to the existing attributes of the files
- removes the selected attributes
= overwrites the current set of attributes the files have with the specified attributes.
attribute is a combination of the letters acdeijmstuxACDFPST,
which correspond to the attributes:
a append only
c compressed
d no dump
e extent format
i immutable
j data journaling
m don't compress
s secure deletion
t no tail-merging
u undeletable
x direct access for files
A no atime updates
C no copy on write
D synchronous directory updates
F case-insensitive directory lookups
P project hierarchy
S synchronous updates
T top of directory hierarchy
There are restrictions on the use of many of these attributes.
For example, many of them can be set or cleared only
by the superuser (i.e., root) or an otherwise privileged process.
setfattr: change extended file attributes
Usage (set attribute):
setfattr -n name -v value files...
Usage (remove):
setfattr -x name files...
name is the name of the extended attribute to set or remove
value is the new value of the extended attribute
setfacl: change file access control lists
Usage:
setfacl option [default:][target:][param][:perms] files...
option must include one of the following:
--set set the ACL of a file or a directory, replacing the previous ACL
-m|--modify modify the ACL of a file or directory
-x|--remove remove ACL entries of a file or directory
target is one of the letters ugmo (or the longer forms shown below):
u, users permission of a named user identified by param,
defaults to file owner UID if omitted
g, group permission of a named group identified by param,
default to owning group GID if omitted
m, mask effective rights mask
o, other permissions of others
perms is a combination of the letters rwxX, which correspond to the permissions:
r read
w write
x execute
X execute only if the file is a directory or already has execute permission for some user
Alternatively, perms may be an octal digit (0-7) indicating the set of permissions.
setcap: change file capabilities
Usage:
setcap capability-clause file
A capability-clause consists of a comma-separated list of capability names followed by a list of operator-flag pairs.
The available operators are =, + and -. The available flags are e, i and p which correspond to the Effective, Inheritable and Permitted capability sets.
The = operator will raise the specified capability sets and reset the others. If no flags are given in conjunction with the = operator all the capability sets will be reset. The + and - operators will raise or lower the one or more specified capability sets respectively.
chcon: change file SELinux security context
Usage:
chcon [-u user] [-r role] [-t type] files...
user is the SELinux user, such as user_u, system_u or root.
role is the SELinux role (always object_r for files)
type is the SELinux subject type
chsmack: change SMACK extended attributes
SMACK is Simplified Mandatory Access Control Kernel.
Usage:
chsmack -a value file
value is the SMACK label to be set for the SMACK64 extended file attribute
setrichacl: change rich access control list
richacls are a feature that will add more advanced ACLs.
Currently a work in progress, so I can not tell you much about them. I have not used them.
See also this question Are there more advanced filesystem ACLs beyond traditional 'rwx' and POSIX ACL?
and man page
| What are the different ways to set file permissions, etc., on GNU/Linux? |
1,348,306,435,000 |
A hacker has dropped a file in my tmp dir that is causing issues. Nothing malicious except creating GB's of error_log entries because their script is failing. However, the file they are using to execute has no permissions and even as ROOT I can't delete or rename this file.
---------- 1 wwwusr wwwusr 1561 Jan 19 02:31 zzzzx.php
root@servername [/home/wwwusr/public_html/tmp]# rm zzzzx.php
rm: remove write-protected regular file './zzzzx.php'? y
rm: cannot remove './zzzzx.php': Operation not permitted
I have also tried removing by inode
root@servername [/home/wwwusr/public_html/tmp]# ls -il
...
1969900 ---------- 1 wwwusr wwwusr 1561 Jan 19 02:31 zzzzx.php
root@servername [/home/wwwusr/public_html/tmp]# find . -inum 1969900 -exec rm -i {} \;
rm: remove write-protected regular file './zzzzx.php'? y
rm: cannot remove './zzzzx.php': Operation not permitted
How do I delete this file?
|
The file has probably been locked using file attributes.
As root, do
lsattr zzzzx.php
Attributes a (append mode) or i (immutable) present would prevent your rm. If they're there, then
chattr -ai zzzzx.php
rm zzzzx.php
should delete your file.
| How do I remove a file with no permissions? |
1,348,306,435,000 |
The logged in user is a member of a group that has a write permission on a folder. But when this user is trying to write something, "permission is denied".
The log below summarizes the question:
subv:/www/tracer/ whoami
frank
subv:/www/tracer/
subv:/www/tracer/ ls -ltr
total 4
drwxrwxr-x 2 root tracer 4096 Jan 20 12:25 convert.tracer.com
subv:/www/tracer/ groups frank
frank : frank tracer
subv:/www/tracer/ > convert.tracer.com/test
-bash: convert.tracer.com/test: Permission denied
subv:/www/tracer/
Output of "ls -bail /www/tracer/convert.tracer.com/":
subv:~/ ls -bail /www/tracer/convert.tracer.com/
total 8
38010883 drwxrwxr-x 2 root tracer 4096 Jan 20 12:25 .
38010882 drwxr-xr-x 3 root root 4096 Jan 20 12:25 ..
subv:~/
|
Group membership is re-read on login. groups seem to report the groups you are in according to /etc/group and does not reflect membership of groups in the current session.
Use the command id -Gn to show the groups that you are currently an active member of. Solution: relogin to apply the group changes.
| Group member cannot write even with write permission |
1,348,306,435,000 |
Well, to be specific, it was chmod -R 755. Now every file is executable, which I don't want.
I am thinking that I should look at the first two bytes of each file for the #!, but will this cover everything? Should I instead use file to look at everything and base my decision on that? Or, more likely, is there an even better way to do this?
What is the preferred way to recursively go through a directory and set -x on files that are not 'supposed to be' executable?
|
There's no magic bullet here. The permissions carry information which is not always redundant.
If you'd done this in a system directory, your system would be in a very bad state, because you'd have to worry about setuid and setgid bits, and about files that are not supposed to be world-readable, and about files that are supposed to be group- or world-writable.
In a per-user directory, you have to worry about files that aren't supposed to be world-readable. No one can help you there.
As for executability, a good rule of thumb would be to make everything that doesn't look like it could be executed, be nonexecutable. The kernel can execute scripts whose first two bytes are #!, ELF binaries whose first four bytes are \x7fELF where \x7f is the byte with the value 12, and a few rarer file types (a.out, anything registered with binfmt_misc). Hence the following command should restore your permissions to a reasonable state (assumes bash 4 or zsh, otherwise use find to traverse the directory tree; warning, typed directly into the browser):
for x in **/*; do
if ! [ -f "$x" ]; then continue; fi # skip all but regular files
case $(head -c 4 "$x") in
"#!"??) :;; # skip script
"\x7fELF") :;; # skip ELF executable
*) chmod a-x "$x";;
esac
done
Note that there is a simple way to back up and restore permissions of a directory tree, on Linux and possibly other unices with ACL support:
getfacl -R >saved-permissions
setfacl --restore=saved-permissions
| I accidentally chmod -R +x on a directory. How do I restore the correct permissions? |
1,348,306,435,000 |
I've got a Docker image which generates log-like files when errors occur. I've mounted the directory it writes to to my host machine with a bind mount. However, the created files are owned by root. Though my user account has root privileges, it is tedious to run chown and chgrp after every run of the container in order to inspect the files.
Is there a way to have the container set the owner and group of the files to that of the user who ran the container?
For some context, here's a toy example I created:
Dockerfile
FROM debian
WORKDIR /root
VOLUME /root/output
COPY run.sh /root/
ENTRYPOINT ["./run.sh"]
run.sh
#!/bin/bash
echo hello > output/dump
My execution command is
docker run -v $PWD/output:/root/output test
|
The files are created by the user that runs within the container.
If your containerized command runs as root, then all files will be created as root.
If you want your files to be created as another user, run the container as this other user.
e.g.
docker run -v "$(pwd)/output":/root/output -u $(whoami) test
Note: Depending on your container, this might not work out of the box (e.g., because, within the container, you need to open a privileged port or your script is only accessible by a given (super)user).
| Files created by Docker container are owned by root |
1,348,306,435,000 |
I am root. I would like to know whether a non-root user has write access to some files - thousands of them. How to do it efficiently while avoiding process creation?
|
Perhaps like this:
#! /bin/bash
writable()
{
local uid="$1"
local gids="$2"
local ids
local perms
ids=($( stat -L -c '%u %g %a' -- "$3" ))
perms="0${ids[2]}"
if [[ ${ids[0]} -eq $uid ]]; then
return $(( ( perms & 0200 ) == 0 ))
elif [[ $gids =~ (^|[[:space:]])"${ids[1]}"($|[[:space:]]) ]]; then
return $(( ( perms & 020 ) == 0 ))
else
return $(( ( perms & 2 ) == 0 ))
fi
}
user=foo
uid="$( id -u "$user" )"
gids="$( id -G "$user" )"
while IFS= read -r f; do
writable "$uid" "$gids" "$f" && printf '%s writable\n' "$f"
done
The above runs a single external program for each file, namely stat(1).
Note: This assumes bash(1), and the Linux incarnation of stat(1).
Note 2: Please read comments from Stéphane Chazelas below for past, present, future, and potential dangers and limitations of this approach.
| Find files that a user can write, efficiently with minimal process creation |
1,348,306,435,000 |
Out of the two options to change permissions:
chmod 777 file.txt
chmod a+rwx file.txt
I am writing a document that details that users need to change the file permissions of a certain file. I want to detail it as the most common way of changing file permissions.
Currently is says:
- Set permissions on file.txt as per the example below:
- chmod 777 /tmp/file.txt
This is just an example, and won't change files to have full permissions for everyone.
|
Google gives:
1,030,000 results for 'chmod 777'
371,000 results for 'chmod a+rwx'
chmod 777 is about 3 times more popular.
That said, I prefer using long options in documentation and scripts, because they are self-documenting. If you are following up your instructions with "Run ls -l | grep file.txt and verify permissions", you may want to use chmod a+rwx because that's how ls will display the permissions.
| Which is more widely used: chmod 777 or chmod a+rwx [closed] |
1,348,306,435,000 |
What is the purpose of Linux permissions such as 111 or 333 (i.e. the user can execute, but cannot read the file), if the ability to execute does not automatically imply the ability to read?
|
I played with it and apparently, exec permissions do not imply read permissions. Binaries can be executable without being readable:
$ echo 'int main(){ puts("hello world"); }' > hw.c
$ make hw
$ ./hw
hello world
$ chmod 111 hw
$ ./hw
hello world
$ cat hw
/bin/cat: hw: Permission denied
I can't execute scripts though, unless they have both read and exec permission bits on:
$ cat > hw.sh
#!/bin/bash
echo hello world from bash
^D
$ chmod +x ./hw.sh
$ ./hw.sh
hello world from bash
$ chmod 111 ./hw.sh
$ ./hw.sh
/bin/bash: ./hw.sh: Permission denied
| Purpose of permissions such as 0111 or 0333 |
1,348,306,435,000 |
It appears I still miss some things about the way permissions work. I am on a debian 7 system btw.
just now I have this file of which I downloaded and it belongs to myuser:myuser, that is both user and group are set to me. It also resides in my $HOME directory since that is where I downloaded it to.
So far so good.
Now I want to share this file with some other users of the pc and for that I want to switch the group ownership of the file to group "users".
however that fails:
nass@quarx:~/xmas_carol$ chgrp -R users *
chgrp: changing group of movie.mov': Operation not permitted
And the contents of the folder are:
-rwxr-xr-x 1 nass nass 2482411461 Feb 6 03:57 movie.mov
I am fuzzy about what is going on with the permissions. Can someone explain
|
Your user is probably not a member of the users group, so you don't have the right to give a file to that group. To illustrate:
$ groups
terdon sudo netdev fuse vboxsf vboxusers
$ ls -l file
-rw-r--r-- 1 terdon terdon 604 Feb 6 03:04 file
$ chgrp users file
chgrp: changing group of ‘file’: Operation not permitted
$ chgrp vboxusers file
$ ls -l file
-rw-r--r-- 1 terdon vboxusers 604 Feb 6 03:04 file
This behavior is mentioned in the POSIX specs:
Only the owner of a file or the user with appropriate privileges may change the owner or group of a file.
Some implementations restrict the use of chgrp to a user with appropriate privileges when the group specified is not the effective group ID or one of the supplementary group IDs of the calling process.
The main reason for this is that if you aren't a member of a group, you should not be able to modify what that group has access to. This answer on chown permissions is also relevant.
Traditionally, on shared systems, you have a users group to which all regular users belong and that is the primary group of each user. That way, files are created owned by the users group and all users can read them.
Anyway, since that is not the way that Debian-based distros are set up these days, the way to give a specific user access to your file would be to either
Change the group ownership of the file/directory to a group that both you and the other user are members of;
Just change the permissions of the file/directory accordingly:
$ chmod 755 /home/terdon
$ ls -ld /home/terdon
drwxr-xr-x 170 terdon terdon 491520 Apr 20 13:43 /home/terdon/
That will make the directory accessible to everybody.
| Permission denied to change gid(group) of a file I own |
1,348,306,435,000 |
$ touch testfile
$ chmod g+w testfile
$ sudo adduser user2 user1
$ stat -c'%a %A' testfile
664 -rw-rw-r--
$ su user2
Password:
$ groups
user2 user1
$ rm testfile
rm: cannot remove `testfile': Permission denied
What is missing?
|
Deleting a file means you are making changes to the directory it resides in, not the file itself. Your group needs rw on the directory to be able to remove a file. The permissions on a file are only for making changes to the file itself.
This might come off as confusing at first until you think about how the filesystem works. A file is just an inode, and the directory refers to the inode. By removing it, you're just removing a reference to that file's inode in the directory. So you're changing the directory, not the file. You could have a hard link to that file in another directory, and you'd still be able to remove it from the first directory without actually changing the file itself, it would still exist in the other directory.
| I can't delete a file that I have write permissions for as a group member |
1,348,306,435,000 |
I have read this topic, which gave me some info but I feel I am not fully getting it. What does the execute permission actually mean on a folder? I fully understand what it means on a file, but on a folder.
|
With the execute bit set you have the permission
to cd into the directory
Also for long listing ls -l i.e. to view the meta data of the files inside the directory (Provided that read permission is there for the directory.
| What does "execute" permission mean on a folder? [duplicate] |
1,348,306,435,000 |
What are the cons, for having a restrictive umask of 077? A lot of distros (I believe all, except Red Hat? ) have a default umask of 022, configured in /etc/profile. This seems way too insecure for a non-desktop system, which multiple users are accessing, and security is of concern.
On a related note, on Ubuntu, the users' home directories are also created with 755 permissions, and the installer states that this is for making it easier for users to share files. Assuming that users' are comfortable setting permissions by hand to make files shared, this is not a problem.
What other downsides are there?
|
022 makes things convenient. 077 makes things less convenient, but depending on the circumstances and usage profile, it might not be any less convenient than having to use sudo.
I would argue that, like sudo, the actual, measurable security benefit you gain from this is negligible compared to the level of pain you inflict on yourself and your users. As a consultant, I have been scorned for my views on sudo and challenged to break numerous sudo setups, and I have yet to take more than 15 seconds to do so. Your call.
Knowing about umask is good, but it's just a single Corn Flake in the "complete breakfast". Maybe you should be asking yourself "Before I go mucking with default configs, the consistency of which will need to be maintained across installs, and which will need to be documented and justified to people who aren't dim-witted, what's this gonna buy me?"
Umask is also a bash built-in that is settable by individual users in their shell initialization files (~/.bash*), so you're not really able to easily enforce the umask. It's just a default. In other words, it's not buying you much.
| Downsides of umask 077? |
1,348,306,435,000 |
I tried finding this on here, but couldn't so sorry if it's a duplicate.
Say I have 2 groups and a user: group1, group2, user1
with the following structure: group1 is a member of group 2, user1 is a member of group1
Now say I have the following files with relevant permissions
file1 root:group1 660
file2 root:group2 660
Now when I log into user1, I'm able to edit file1, but not edit file2. Short of adding user1 to group2, is there any way of doing this? or is there no way?
I'm using Ubuntu btw.
|
There is no such thing as a group being a member of a group. A group, by definition, has a set of user members. I've never heard of a feature that would let you specify “subgroups” where members of subgroups are automatically granted membership into the supergroup on login. If /etc/group lists group1 as a member of group2, it designates the user called group1 (if such a user exists, which is possible: user names and group names live in different name spaces).
If you want user1 to have access to file2, you have several solutions:
Make file2 world-accessible (you probably don't want this)
Make user1 the owner of file2: chown user1 file2
Add user1 to group2: adduser user1 group2
Add an ACL to file2 that grants access to either user1 or group`:
setfacl -m user:user1:rw file2
setfacl -m group:group1:rw file2
See Make all new files in a directory accessible to a group on enabling ACLs.
| Group within group file permissions |
1,348,306,435,000 |
As discussed in Understanding UNIX permissions and file types, each file has permission settings ("file mode") for:
the owner / user ("u"),
the owner's group ("g"), and
everyone else ("o").
As far as I understand, the owner of a file can always change the file's permissions using chmod. So can any application running under the owner.
What is the reason for restricting the owner's own permissions if they can always change them?
The only use I can see is the protection from accidental deletion or execution, which can be easily overcome if intended.
A related question has been asked here: Is there a reason why 'owner' permissions exist? Aren't group permissions enough? It discusses why the owner's permissions cannot be replaced by a dummy group consisting of a single user (the owner). In contrast, here I am asking about the purpose of having permissions for the owner in principle, no matter if they are implemented through a separate "u" octal or a separate group + ACLs.
|
There are various reasons to reduce the owner's permissions (though rarely to less than that of the group).
The most common is not having execute permission on files not intended to be executed. Quite often, shell scripts are fragments intended to be sourced from other scripts (e.g. your .profile) and don't make sense as top-level processes. Command completion will only offer executable files, so correct permissions helps in interactive shells.
Accidentally overwriting a file is a substantial risk - it can happen through mistyping a command, or even more easily in GUI programs. One of the first things I do when copying files from my camera is to make them (and their containing directory) non-writeable, so that any edits I make must be copies, rather than overwriting the original.
Sometimes it's important that files are not even readable. If I upgrade my Emacs and have problems with local packages in my ~/lisp directory, I selectively disable them (with chmod -r) until it can start up successfully; then I can make them readable one at a time as I fix compatibility problems.
A correct set of permissions for user indicates intentionality. Although the user can change permissions, well-behaved programs won't do that (at least, not without asking first). Instead of thinking of the permissions as restricting the user, think of them as restricting what the user's processes can do at a given point in time.
| What is the reason for having or restricting file owner's permissions? |
1,348,306,435,000 |
This is a more general question about 'chmoding' recursively.
I have this script which at some point needs to change the permissions recursively in a folder which has a few hundred thousand files.
There are new files added in that folder every day, but the ones that are already there have the permissions already set and they don't change.
My question is... when I call
chmod 775 . -R
does it try to set the permission for the files that already have the right permissions set, or only for the new files that don't have the right permissions?
It seems to always take ages to get past this command in the script, even though the 'new' files are only a few thousand and it should do their permissions fairly quickly.
I've looked at the man page for chmod, but it doesn't seem to mention anything on this case.
If chmod doesn't check for permissions beforehand, should I start looking at combining 'find' with 'chmod'?
|
chmod might or might not change the permissions of files that are already set to what you want, but if not, it would still need to check them to see what their current permissions are[0]. With hundreds of thousands of files, I don't think it would matter either way; the time is most likely being spent by the tools stating every file.
You can try using find to either check for files newer than the last run or files that need chmod to be run, but I don't think you'll get much speed improvement.
If possible for your script, you might be able to get the new files put into a separate directory first, as a "holding" area. Then you can chmod THAT directory (which only has new files), and mv them in with the rest. That should be substantially faster, but unfortunately won't work for every application.
[0] Even if it does try to set the permission of files that don't need any changes, the underlying filesystem probably won't do anything with the request, because it's unnecessary.
| chmod recursive permission on thousands of files |
1,348,306,435,000 |
I am new to system administration and I have a permission related query. I have a group called administration. Inside the administration group, I have the users user1, user2, user3, superuser. All the users are in the administration group. Now, I need to give permissions to the user superuser to be able to view the /home directory of the other users. However, I do not want user1, user2, user3 to see the home of any other user other than himself. (That is, user1 should be able to see only user1's home and so on).
I have created the users and groups and assigned all the users to the group. How should I specify the permissions for the superuser now?
In other words, I'm thinking of having two groups (say NormalUsers and Superuser). The NormalUsers group will have the users user1, user2 and user3. The Superuser group will only have the user Superuser. Now, I need the Superuser to have full access on the files of users in the group NormalUsers. Is this possible in Linux?
|
If the users are cooperative, you can use access control lists (ACL). Set an ACL on the home directory of user1 (and friends) that grants read access to superuser. Set the default ACL as well, for newly created files, and also the ACL on existing files.
setfacl -R -m user:superuser:rx ~user1
setfacl -d -R -m user:superuser:rx ~user1
user1 can change the ACL on his files if he wishes. Even if user1 is cooperating, new files may accidentally be unreadable, e.g. when untarring an archive with restrictive permissions, or because some applications deliberately create files that are only readable by the user (e.g. mail clients tend to do this).
If you want to always give superuser read access to user1's files, you can create another view of the users' home directories with different permissions, with bindfs.
mkdir -p ~superuser/spyglass/user1
chown superuser ~superuser/spyglass
chmod 700 ~superuser/spyglass
bindfs -p a+rD-w ~user1 ~superuser/spyglass/user1
Files accessed through ~superuser/spyglass/user1 are world-readable. Other than the permissions, ~superuser/spyglass/user1 is a view of user1's home directory. Since superuser is the only user who can access ~superuser/spyglass, only superuser can benefit from this. This method is automatic and user1 cannot opt out.
| Allow a user to read some other users' home directories |
1,348,306,435,000 |
Can someone explain to me the following?
$ ls -ld /temp/sit/build/
dr-xr-s--- 3 asdf qwer 4096 Jan 31 2012 /temp/sit/build/
$ ls -ld /temp/sit/build/*
ls: /temp/sit/build/*: Permission denied
So apprently, I can't use the asterisk here. I've tried it with a sudo command and I get a "no such file" error rather than "permission denied"...
sudo ls -l /temp/sit/build/*
ls: /temp/sit/build/batch*: No such file or directory
but it finally works if I don't use the *
sudo ls -l /temp/sit/build/
total 4
dr-xr-s--- 11 asdf qwer 4096 Oct 3 23:31 file
|
The shell that's doing the expansion of the * wildcard is the shell where you type it. If the shell has the permission to read the list of files in the directory, then it expands /temp/sit/build/* to /temp/sit/build/file, and runs sudo with the arguments ls, -l and /temp/sit/build/file. If the shell is unable to find any match for /temp/sit/build/* (whether it's because there are no matches, or because the shell has no permission to see the matches), then it leaves the pattern alone, and sudo is called with the arguments ls, -l and /temp/sit/build/*.
Since there is no file called /temp/sit/build/*, the ls command complains if you pass it that name. Recall that ls doesn't expand wildcards, that's the shell's job.
If you want wildcard expansion to happen in a directory where you don't have read permission, then the expansion must happen in a shell that's started by sudo instead of in the shell that calls sudo. sudo doesn't automatically start a shell, you need to do that explicitly.
sudo sh -c 'ls -l /temp/sit/build/*'
Here, of course, you can do sudo ls -l /temp/sit/build/ instead, but that doesn't generalize to other patterns.
| Cannot expand asterisk without proper permission |
1,348,306,435,000 |
I have device file that appears in /dev when a specific board is plugged in. The read and write operations to it work just fine, but in order to open the device file the program needs to be executed with root priveledges. Is there any way I can all a non-root user to open this one specific device file without having to use sudo?
|
Yes, you may write an udev rule.
In /etc/udev/rules.d make a file 30-mydevice.rules (number has to be from 0 to 99 and decides only about the script running order; name doesn't really matter, it has just to be descriptive; .rules extension is required, though)
In this example I'm assuming your device is USB based and you know it's vendor and product id (can be checked using lsusb -v), and you're using mydevice group your user has to be in to use the device. This should be file contents in that case:
SUBSYSTEM=="usb", SYSFS{idVendor}=="0123", SYSFS{idProduct}=="4567", ACTION=="add", GROUP="mydevice", MODE="0664"
MODE equal to 0664 allows device to be written to by it's owner (probably root) and the defined group.
| How to grant non-root user access to device files |
1,348,306,435,000 |
By accident I ran chmod -u filename and it removed all of the permissions I had on filename.
The man page does not reference a -u option. Experimenting I was able to conclude that it removes not all permissions, but just read and execute access, leaving write access intact.
So what does this do exactly?
My conclusion above is wrong, I now think that what it does is remove the permissions that the owner has, from all categories.
I think the behavior is analogous to a=u, only it is - instead of = and a can be dropped just as it can with, for instance, a+x.
|
This is not an option, but a standard (but uncommon) way of specifying the permissions. It means to remove (-) the permissions associated with the file owner (u), for all users (no preceding u, g, or o). This is documented in the man page.
GNU chmod's man page documents this as:
The format of a symbolic mode is [ugoa...][[-+=][perms...]...], where perms is either zero or more letters from the set rwxXst, or a single letter from the set ugo
and later
Instead of one or more of these letters, you can specify exactly one of the letters ugo: the permissions granted to the user who owns the file (u), the permissions granted to other users who are members of the file's group (g), and the permissions granted to users that are in neither of the two preceding categories (o)
So -u means to remove (-) whatever permissions are currently enabled for the owner (u) for everybody (equivalently to a-u, except honouring the current umask). While that's not often going to be very useful, the analogous chmod +u will sometimes be, to copy the permissions from the owner to others when operating recursively, for example.
It's also documented in POSIX, but more obscurely defined: the permission specification is broadly who[+-=]perms (or a number), and the effect of those are further specified:
The permcopy symbols u, g, and o shall represent the current permissions associated with the user, group, and other parts of the file mode bits, respectively. For the remainder of this section, perm refers to the non-terminals perm and permcopy in the grammar.
and then
-
...
If who is not specified, the file mode bits represented by perm for the owner, group, and other permissions, except for those with corresponding bits in the file mode creation mask of the invoking process, shall be cleared.
| What does chmod -u do? |
1,348,306,435,000 |
Is there a way to check the permissions of the root folder, /? I mean the folder's permissions, not its content's (/var, /usr, etc.) permissions? Running ls /.. shows the content's permissions.
|
You can also use the -d switch of ls:
$ ls -ld /
drwxr-xr-x 28 root root 126976 Mar 20 17:11 /
From man ls:
-l use a long listing format
-d, --directory
list directory entries instead of contents, and do not derefer‐
ence symbolic links
| How Do I check Permissions of Root Folder (/ Folder, not /root)? |
1,348,306,435,000 |
When you run an executable, sometimes the OS will deny your
permission to. For example running make install with the prefix
being a system path will need sudo, while with the prefix being a
non-system path will not be asked for sudo. How does the OS decide
that running an executable would require more privilege than a user
has, even before the program does something?
Sometimes, running a program will not be denied permission, but the
program will be able to do more things if it is run with sudo. For
example, when running du on some system directory, only with
sudo it will be able to access some directory. Why does the OS not
deny permission of running such a program, or friendly notify more privilege is preferred, before the program can run?
Is it true that whenever sudo works, su will also work, and
whenever su works, sudo will also work? or with su, a user can do
more than with sudo? How does the OS decide when sudo works, and
when su is needed?
|
Sometimes the "Permission denied" message is due to filesystem permissions denying you write access, for example. The executable/tool simply checks if it the filesystem grants you enough permissions to do what you're about to do and throws an error if it's denied by the filesystem. Other times, the tool itself will check your user ID before allowing you to continue using it.
When you run a program with sudo you are running it under some other user's name. If that user is "able to do more things" than your user and the sudo configuration allows you to do these things on the other user's behalf then yes, sudo will allow you to do more things. This is not necessary, though. If you just tack sudo on at the beginning of the command line, you're actually sudoing as root, so typically you're able to do more things than a mere mortal.
Most definitely not. To use sudo you need to supply your own user password and then you're allowed to do some things on the target user's behalf. To use su, you need the target user's password and if you have it, you become that target user as far as the system is concerned and can do anything that user can do.
See also
Why is the 'sudo' password different than the 'su root' password
| How does the OS know that a command needs sudo? |
1,348,306,435,000 |
I've seen cases like that with faulty storage devices, with faults in remote storage (SAN, NAS), I think I've even seen something similar caused by mount permissions. But it's the first time I see this happening on the same filesystem as my home directory.
What kind of permissions are kicking in here? Definitely not mounts (I'm on the same ext4 filesystem), not SELinux, not ACLs. Then what?
I do not recall how this directory was created. It's likely it got created by some kind of software.
For me the weirdest part is that the directory is not even allowed to see its or its parent's info (last command).
I'm using Linux Mint Sarah.
user01@MyPC ~/somedirectory $ ls -l ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D\:
ls: negaliu pasiekti './deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D:/workspace': Permission denied
viso 0
d????????? ? ? ? ? ? workspace
user01@MyPC ~/somedirectory $ ls -ld ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D\:
drw-r--r-- 3 user01 user01 4096 Rgs 27 2016 ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D:
user01@MyPC ~/somedirectory $ sudo file ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D\:
./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D:: directory
user01@MyPC ~/somedirectory $ sudo ls -l ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D\:
viso 4
drwxr-xr-x 3 user01 user01 4096 Rgs 27 2016 workspace
user01@MyPC ~/somedirectory $ sudo stat ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D\:
File: './deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D:'
Size: 4096 Blocks: 8 IO Block: 4096 aplankas
Device: 807h/2055d Inode: 3937216 Links: 3
Access: (0644/drw-r--r--) Uid: ( 1000/ user01) Gid: ( 1000/ user01)
Access: 2017-09-21 12:57:33.990819052 +0300
Modify: 2016-09-27 11:18:38.309775066 +0300
Change: 2017-03-13 14:56:40.960468954 +0200
Birth: -
user01@MyPC ~/somedirectory $ sudo getfacl ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D\:
# file: deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D:
# owner: user01
# group: user01
user::rw-
group::r--
other::r--
user01@MyPC ~/somedirectory $ stat ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D\:
File: './deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D:'
Size: 4096 Blocks: 8 IO Block: 4096 aplankas
Device: 807h/2055d Inode: 3937216 Links: 3
Access: (0644/drw-r--r--) Uid: ( 1000/ user01) Gid: ( 1000/ user01)
Access: 2017-09-21 12:57:33.990819052 +0300
Modify: 2016-09-27 11:18:38.309775066 +0300
Change: 2017-03-13 14:56:40.960468954 +0200
Birth: -
user01@MyPC ~/somedirectory $ stat ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D\:/workspace
stat: nepavyksta patikrinti './deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D:/workspace': Permission denied
user01@MyPC ~/somedirectory $ sudo stat ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D\:/workspace
File: './deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D:/workspace'
Size: 4096 Blocks: 8 IO Block: 4096 aplankas
Device: 807h/2055d Inode: 3937217 Links: 3
Access: (0755/drwxr-xr-x) Uid: ( 1000/ user01) Gid: ( 1000/ user01)
Access: 2017-09-21 12:58:46.845727190 +0300
Modify: 2016-09-27 11:18:38.309775066 +0300
Change: 2016-12-02 13:56:08.298109826 +0200
Birth: -
user01@MyPC ~/somedirectory $ stat .
File: '.'
Size: 4096 Blocks: 8 IO Block: 4096 aplankas
Device: 807h/2055d Inode: 3278479 Links: 23
Access: (0755/drwxr-xr-x) Uid: ( 1000/ user01) Gid: ( 1000/ user01)
Access: 2017-09-21 09:46:22.102269130 +0300
Modify: 2017-09-20 17:33:04.564009275 +0300
Change: 2017-09-20 17:33:04.564009275 +0300
Birth: -
user01@MyPC ~/somedirectory $ ll ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D\:/
ls: negaliu pasiekti './deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D:/workspace': Permission denied
ls: negaliu pasiekti './deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D:/.': Permission denied
ls: negaliu pasiekti './deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D:/..': Permission denied
viso 0
d????????? ? ? ? ? ? ./
d????????? ? ? ? ? ? ../
d????????? ? ? ? ? ? workspace/
Attributes:
user01@MyPC ~/somedirectory $ sudo lsattr ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D\:/
-------------e-- ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D:/workspace
user01@MyPC ~/somedirectory $ sudo lsattr ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D\:/workspace
-------------e-- ./deploy_dir/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/D:/workspace/directory2
|
On files read suffices to check the permissions. You need read AND execute on folders to ls them.
chmod -R a+X ./deploy_dir
Capital X to only set execute on folders (and files that already have execute bit set).
| Linux local directory permissions as question-marks for non-root |
1,348,306,435,000 |
How can I set file to be executable only to other users but not readable/writable, the reason for this I'm executing something with my username but I don't want to give out the password. I tried :
chmod 777 testfile
chmod a=x
chmod ugo+x
I still get permission denied when executing as another user.
|
You need both read and execute permissions on a script to be able to execute it. If you can't read the contents of the script, you aren't able to execute it either.
tony@matrix:~$ ./hello.world
hello world
tony@matrix:~$ ls -l hello.world
-rwxr-xr-x 1 tony tony 17 Jul 13 22:22 hello.world
tony@matrix:~$ chmod 100 hello.world
tony@matrix:~$ ls -l hello.world
---x------ 1 tony tony 17 Jul 13 22:22 hello.world
tony@matrix:~$ ./hello.world
bash: ./hello.world: Permission denied
| File permission execute only |
1,348,306,435,000 |
When I turned on my Ubuntu 18.04 yesterday and wanted to start GitKraken, it did not work. After I click its icon I see how the process tries to start in the upper left corner (next to "Activities") but after a few seconds the process seems to die and nothing happens.
Trying to launch GitKraken from the console fails too with the following two messages:
/snap/gitkraken/58/bin/desktop-launch: line 23: $HOME/.config/user-dirs.dirs: Permission denied
ln: failed to create symbolic link '$HOME/snap/gitkraken/58/.config/gtk-2.0/gtkfilechooser.ini': File exists
Unfortunately, my Linux skills are too limited to solve this. The only thing I've tried is chmod 777 $HOME/.config/user-dirs.dirs because of the Permossion denied but that did not help.
EDIT: as terdon suggested in his comment I've made ls -ld ~/.config/user-dirs.dirs and this is its output:
-rwxrwxrwx 1 myusername myusername 633 Mai 6 10:30 /home/mayusername/.config/user-dirs.dirs
Then, I made the mv ~/snap/gitkraken/58/.config/gtk-2.0/gtkfilechooser.ini gtkfilechooser.ini.bak command and tried to start GitKraken afterwards. I did not start showing again:
/snap/gitkraken/58/bin/desktop-launch: line 23: /home/myusername/.config/user-dirs.dirs: Permission denied
The ln: failed to create symbolic link ... error from my initial post did not appear. Exe cuting ll in the directory ~/snap/gitkraken/58/.config/gtk-2.0 gives me the following output:
drwxrwxr-x 2 myusername myusername 4096 Jun 3 16:44 ./
drwxrwxr-x 8 myusername myusername 4096 Mai 21 12:28 ../
lrwxrwxrwx 1 myusername myusername 47 Jun 3 15:45 gtkfilechooser.ini -> /home/myusername/.config/gtk-2.0/gtkfilechooser.ini
-rw-r--r-- 1 myusername myusername 198 Jun 3 16:44 gtkfilechooser.ini.bak
gtkfilechooser.ini -> /home/myusername/.config/gtk-2.0/gtkfilechooser.ini is red since the file does not exist anymore. Executing the chmod command afterwards did not change anything. GitKraken does not start and outputs the same errors.
|
SOLVED:
Had to install libgnome-keyring:
sudo apt install libgnome-keyring0
The UI now comes up and works for me.
Still get the following warnings, but it's working:
Gtk-Message: 11:19:31.343: Failed to load module "overlay-scrollbar"
Gtk-Message: 11:19:31.349: Failed to load module "canberra-gtk-module"
Node started time: 1528391971495
state: update-not-available
EVENT: Main process loaded at 441 ms
state: checking-for-update
state: update-not-available
state: checking-for-update
state: update-not-available
EVENT: Starting initial render of foreground window at 5331 ms
EVENT: Startup triggers started at 5446 ms
| GitKraken does not start anymore on Ubuntu 18.04 |
1,348,306,435,000 |
In my new Gentoo installation, su doesn't work as my non-root user: After entering the correct password I get the message "su: Permission denied". What could be causing this? I have already tried reinstalling the package containng /bin/su.
EDIT: sudo works.
|
You have to add your user to the wheel group:
gpasswd -a youruser wheel
Alternatively, you can disable the group membership check for su in pam by editing
/etc/pam.d/su
and commenting out this line:
auth required pam_wheel.so use_uid
It requires users to be in the wheel group to be able to switch user.
User switching as non-root works again when this pam module is disabled for su.
| su: Permission denied despite correct password |
1,348,306,435,000 |
In a standard Linux filesystem, which of these common directories are world-writable by default?
/tmp
/etc
/var
/proc
/bin
/boot
/....
....
Why are they world-writable? Does that pose a security risk?
|
The only FHS-mandated directories that are commonly world-writable are /tmp and /var/tmp. In both cases, that's because they are intended for storing temporary files that may be made by anyone.
Also common is /dev/shm, as a tmpfs (filesystem backed by RAM), for fast access to mid-sized data shared between processes, or just creating files that are guaranteed to be destroyed on reboot.
There may also be a /var/mail or /var/spool/mail, and sometimes other spooler directories. Those are used to hold mail temporarily before it's processed. They aren't always world-writable, depending on the tools in use. When they are, it's because files can be created there by user tools for processing by daemons.
All of these directories usually have the sticky bit (t) set, meaning that only the owner of a file or of the directory can move or delete the files in it.
Any program running as any user can make files in these directories, and it's up to the creating program to do the right thing as far as security for its particular data goes. There's no particular general security problem other than someone potentially filling up the filesystem, but plenty of scope for a program to get it wrong.
There have been some moves towards service-specific /tmp directories. These avoid some of the potential bugs that can come up, so it's not as vital for the program to be bug-free in how it uses the directory.
You can find the world-writable directories on your system with:
find / -maxdepth 3 -type d -perm -777
| What are the world writable directories by default? |
1,348,306,435,000 |
Would this save one bit per file, or is there necessary padding that has to be used anyways? And even if there is padding, why not still combine them and utilize the extra bit for a new feature?
|
For historical reasons. The sticky bit was originally used for a completely different purpose: if it was set on an executable file, it told the operating system to retain the text segment in swap. Thus the name "Sticky Bit".
| If the suid bit has no effect on directories and the sticky bit has no effect on files, why aren't they combined into one bit? |
1,348,306,435,000 |
I would like to give a user permissions to create and read files in a particular directory, but not to modify or delete files. If the user can append to files that is ok, but I'd rather not. This is on Ubuntu Linux.
I think this is impossible with standard Unix file permissions, but perhaps this is possible using ACLs? The user will always be connecting using SFTP, so if there was some way to control this within SFTP (as opposed to OS permissions) that would be fine.
To be absolutely clear, I want the following:
echo hello > test # succeeds, because test doesn't exist, and creation is allowed
echo hello >> test # can succeed or fail, depending on whether appending is allowed
echo hello2 > test # fails, because test already exists, and modification is not allowed
cat test # succeeds, because reads are allowed
rm test # fails, because delete is not allowed
If you're wondering why I want to do this, it's to make a Duplicati backup system resistant to Ransomware.
|
You could use bindfs like:
$ ls -ld dir
drwxr-xr-t 2 stephane stephane 4096 Aug 12 12:28 dir/
That directory is owned by stephane, with group stephane (stephane being its only member). Also note the t that prevents users from renaming or removing entries that they don't own.
$ sudo bindfs -u root -p u=rwD,g=r,dg=rwx,o=rD dir dir
We bindfs dir over itself with fixed ownership and permissions for files and directories. All files appear owned by root (though underneath in the real directory they're still owned by stephane).
Directories get drwxrwxr-x root stephane permissions while other types of files get -rw-r--r-- root stephane ones.
$ ls -ld dir
drwxrwxr-t 2 root stephane 4096 Aug 12 12:28 dir
Now creating a file works because the directory is writeable:
$ echo test > dir/file
$ ls -ld dir/file
-rw-r--r-- 1 root stephane 5 Aug 12 12:29 dir/file
However it's not possible to do a second write open() on that file as we don't have permission on it:
$ echo test > dir/file
zsh: permission denied: dir/file
(note that appending is not allowed there (as not part of your initial requirements)).
A limitation: while you can't remove or rename entries in dir because of the t bit, new directories that you create in there won't have that t bit, so you'll be able to rename or delete entries there.
| Allow owner to create & read files, but not modify or delete |
1,348,306,435,000 |
How to add an ssh user who only has permissions to access specific folder?
useradd -d /var/www/xyz.com.tr/musteri -s /bin/bash -g sshd musteri
I created a user called musteri. I set its home folder and group.
So, I want to integrate musteri users into "/var/www/xyz.com.tr/musteri". I don't want it to access another folder.
|
It sounds like you want your müşteriler to have file transfer access to a folder without actually giving them shells. This is a good thing because as binfalse pointed out, giving people shells with limited access is tricky because shells need to access all kinds of things scattered on the system just to run.
In order to give SFTP access to a specific folder, you can do something like this.
Add a new group to the system, say 'sftponly'.
Add any users on your system that should have restricted rights to this group. You could also give them restricted shells like /bin/true, but it's not required.
Change your ssh config file (Usually /etc/ssh/sshd_config) with these lines
Subsystem sftp internal-sftp
Match Group sftponly
ChrootDirectory %h
AllowTCPForwarding no
X11Forwarding no
ForceCommand internal-sftp
This would activate the sftp subsystem inside of SSH and force members of that system group to use only that system when logging in. It would also chroot them to their home directories. You could change that to be a sub-folder of their home-directores as well with something like ChrootDirectory %h/musteri_sftp so that they couldn't se the rest of their system files but would login directly to a special subfolder of their home folder.
Kolay gelsin.
| How to add a ssh user who only has permissions to access specific folder? |
1,348,306,435,000 |
I'm confused about execute file permissions not behaving as I expect. Probably because my expectations are wrong. Anyway:
I have a script file, for simplicity is just called s, located in ~/bin. For the sake of this example, the file contains just the following lines:
#!/bin/zsh
echo "Test";
Very simple.
I navigate to the ~/bin directory, and chmod the file permissions of s to 400 - i.e., read-only for me only. No execute permission. So then I try executing the script by entering its path, giving this:
% ./s
zsh: permission denied: ./s
So far so good. The file can't be executed due to the wrong permissions. Bumping permissions up to 500 (execute permission granted) works fine too - with these permissions, the file executes fine:
% ./s
Test
This is all as expected. But then I chmod permissions back down to 400 (execute permission off again), try sourceing the file, and this happens:
% source s
Test
Although permissions are 400, the script executes.
So here's my question: why does ./s fail (like it should) but source s executes normally? Doesn't this defeat the whole purpose of the execute permission?
At 400 permissions, sh s and zsh s also work.
I'm sure I'm either doing or understanding something horribly wrong somewhere. Can someone point out where to me, and explain the difference between ./s, source s, sh s and zsh s?
|
When you run ./s, you tell the kernel to execute the program s. If you have execution permission, then the kernel reads the first few bytes of the file, sees the #! line so it knows that this is a script, and runs the interpreter, passing it the script name as its first argument. If you don't have execute permission, the kernel aborts the execution at the first step.
When you run zsh s, you execute zsh, and tell it to read the file called s and interpret it as commands. You aren't executing s, you're executing zsh. Same thing with sh s or cat s.
When you run source s, again, you tell zsh to read a file, so what matters is that you have read permission on it.
| Executing a script in zsh - file permissions |
1,348,306,435,000 |
I mounted a samba share using the smbmount command:
$ sudo smbmount \\\\foo\\bar /mnt/bar -o user=tom
When I create new files, they get created with the executable bit set for owner, group and world. For e.g.
$ touch hello.txt
$ ls -la hello.txt
-rwxr-xr-x 1 root root 0 Dec 2 12:28 hello.txt
The same file when created on a NFS mounted share sets up correct permissions without any executable bit set.
Why is this happening? How can it be fixed?
|
NFS was invented in the Unix world and so understands traditional Unix permissions out of the box. (The ACL of modern unix systems are another matter, but recent implementations of NFS should cope with them.)
Samba was invented in the IBM/Microsoft PC world, to exchange files with systems that had no permissions beyond read-only/read-write. It is now native to Windows. By default, Samba does not transmit Unix permissions. Depending on the configuration, either all files are marked executable (which is annoying) or all files (except directories) are marked non-executable (which is annoying).
There are various extensions to the Samba/CIFS protocol that make it more suited for Unix use. Try enabling Unix extensions in the server configuration:
[global]
unix extensions = yes
| Why are files in a smbfs mounted share created with executable bit set? |
1,383,747,898,000 |
I believe (not sure) that the owner of a file/directory and the root user are the only users that are allowed to change the permissions of a file/directory. Am I correct or are there other users that are also allowed to change the permissions?
|
Only the owner and root (super user) are allowed to the change the permission of a file or directory. This means that the owner and the super user can set the read (r), write (w) and execute (x) permissions. But changing the user ownership of files and directories with the command chown is only allowed to root. Changing the group ownership with chgrp is allowed to root and the owner of the file, if the user is a member of the new group.
| Who can change the permissions of a file/directory? |
1,383,747,898,000 |
Is there a way to see a file's flags, such as hidden, schg, etc? I've got an rwxrwxrwx file that I can't edit, and I think a flag might be responsible.
|
I assume you are talking about FreeBSD - from man ls:
-o Include the file flags in a long (-l) output.
So ls -lo should list all the file flags on FreeBSD.
| List all flags of a file? |
1,383,747,898,000 |
I have a script that works with /etc/NetworkManager:
drwxr-xr-x 6 root root 4096 Apr 3 2017 NetworkManager/
I want to give the user programX write permission for this folder without changing the ownership.
Is that possible or would I have to change the ownership?
|
This is what access control lists are for.setfacl -m 'u:programX:rwx' /etc/NetworkManager The user account programX now has read, write, and traverse access to the directory, but does not have ownership access.
Bonus way of doing this on FreeBSD with its NFS ACLs:setfacl -m 'u:programX:rwxD::allow' /etc/NetworkManager
Further reading
Operation not supported with setfacl?
| Give user access to folder without changing ownership? |
1,383,747,898,000 |
A volume intended for use by my user was created at OS installation with root ownership and my user lacks write permissions.
Some solutions I've read about include:
changing ownership of the mount point with chown
adding group write permissions with chmod
adding user or users mount option in /etc/fstab.
What is the best practice for this situation, and what are the implications of each approach?
|
If it's in /etc/fstab, then it will mount at boot.
As only root has write permissions,
you'll need to modify it so that the user has those permissions.
The best way is:
chown -R user /mnt/point
where user represents your user name (or user ID),
and, obviously, /mnt/point represents the mount point of your file system.
If the root group has write permission as well and you want another group to have it then you can use:
chown -R user:group /mnt/point
If the root group doesn't have write access, then you can use chmod next:
chmod -R 775 /mnt/point
That will give write permission to the group if it's not there and read and execute to everyone else. You can modify the 775 to give whatever permissions you want to everyone else as that will be specified by the third number.
To better cover what you asked in your comment below:
You can add the user option to /etc/fstab, but that only allows the file system to be mounted by any user. It won't change the permissions on the file system,
which is why you need chown and/or chmod. You can go ahead and add the user option so that a regular user without sudo can mount it should it be unmounted.
For practicality, the best option here is chown as it gives the user the needed permissions instantly. The chmod command can be used afterwards if the permissions need to be modified for others.
| Mounting volume/partition with permissions for user |
1,383,747,898,000 |
I have done
chmod -R 644 .
inside the directory dir
My user's permissions are drw-r--r-- and i'm the owner of the directory
When trying chmod 755 dir, error is popped
chmod: changing permissions of dir Operation not permitted
The same error is popped when doing ls even as root
How to change permission back to 755 and allow its deletion and modification?
|
from the level above dir:
chmod -R a+x *dir*
to give all users (a) execute permission to all subdirectories and files (+x) or:
chmod -R a+X *dir*
to give all users execute permission to all subdirectories only (+X)
| chmod: changing permissions of directory Operation not permitted |
1,383,747,898,000 |
What does GID actually mean?
I have Googled it and this is what linux.about.com said:
Group identification number for the process. Valid group numbers are given in /etc/group, and in the GID field of /etc/passwd file. When a process is started, its GID is set to the GID of its parent process.
But what does that mean?
The permissions I have for my folder is currently at 0755
I understand if I set the UID for the Owner it will be 4755
And if I set the GID of the Group it will be 2755
If I set the Sticky Bit for Others it will be 1755
Is it even important to set those permissions?
|
Every process in a UNIX-like system, just like every file, has an owner (the user, either real or a system "pseudo-user", such as daemon, bin, man, etc) and a group owner. The group owner for a user's files is typically that user's primary group, and in a similar fashion, any processes you start are typically owned by your user ID and by your primary group ID.
Sometimes, though, it is necessary to have elevated privileges to run certain commands, but it is not desirable to give full administrative rights. For example, the passwd command needs access to the system's shadow password file, so that it can update your password. Obviously, you don't want to give every user root privileges, just so they can reset their password - that would undoubtedly lead to chaos! Instead, there needs to be another way to temporarily grant elevated privileges to users to perform certain tasks. That is what the SETUID and SETGID bits are for. It is a way to tell the kernel to temporarily raise the user's privileges, for the duration of the marked command's execution. A SETUID binary will be executed with the privileges of the owner of the executable file (usually root), and a SETGID binary will be executed with the group privileges of the group owner of the executable file. In the case of the passwd command, which belongs to root and is SETUID, it allows normal users to directly affect the contents of the password file, in a controlled and predictable manner, by executing with root privileges. There are numerous other SETUID commands on UNIX-like systems (chsh, screen, ping, su, etc), all of which require elevated privileges to operate correctly. There are also a few SETGID programs, where the kernel temporarily changes the GID of the process, to allow access to logfiles, etc. sendmail is such a utility.
The sticky bit serves a slightly different purpose. Its most common use is to ensure that only the user account that created a file may delete it. Think about the /tmp directory. It has very liberal permissions, which allow anyone to create files there. This is good, and allows users' processes to create temporary files (screen, ssh, etc, keep state information in /tmp). To protect a user's temp files, /tmp has the sticky bit set, so that only I can delete my files, and only you can delete yours. Of course, root can do anything, but we have to hope that the sysadmin isn't deranged!
For normal files (that is, for non-executable files), there is little point in setting the SETUID/SETGID bits. SETGID on directories on some systems controls the default group owner for new files created in that directory.
| What does GID mean? |
1,383,747,898,000 |
I am curious as to what this difference is between programs that are; started with systemd when enabled through systemctl, vs those started by means of /etc/rc.local or through the CLI.
For example, I was recently using shairport-sync for the raspberry pi. Initially, I set shairport-sync to start by means of sudo systemctl enabled shairport-sync.
Later down the road I used a functionality within shairport-sync to run scripts prior and post to devices connecting.
To my surprise, the scripts when executed by shairport-sync did not kill arecord or aplay
However, when I would run the script via terminal the script executed and killed arecord and aplay.
To further confuse myself, I killed shairport-sync and started it via terminal to see the output of what was happening. When I did so the scripts functioned as I expected when the device connected and killed arecord and aplay. So, as a fix I disabled shairport-sync in sysmtectl and set it to run in /etc/rc.local as a quick fix. After a reboot it functioned as I expected.
This leads me to believe that there is some difference between a program run as apart of systemd and a program that runs when started via /etc/rc.local or the CLI.
Why does this happen? Is this because of different run levels? Some dark magic?
The script that is run when a device connects to shairport-sync is as follows: shairportstart.sh
#!/bin/sh
/usr/bin/sudo /bin/pkill arecord
if [ $(date +%H) -ge "18" -o $(date +%H) -le "7" ]; then
/usr/bin/amixer set Speaker 40%
else
/usr/bin/amixer set Speaker 100%
fi
/home/pi/shScripts/shairportfade.sh&
exit 0
Here is the fade script: shairportfade.sh
#!/bin/sh
/usr/bin/amixer set Speaker 30-
for (( i=0; i<30; i++))
do
/usr/bin/amixer set Speaker 1+
done
exit 0
The script that is run when a device disconnects to shairport-sync is as follows: shairportend.sh
#!/bin/sh
/usr/bin/amixer set Speaker 70%
/usr/bin/arecord -D plughw:1 -f dat | /usr/bin/aplay -D plughw:1 -f dat&
exit 0
I found the following error in the /var/log/syslog only when shairport-sync was initially run as apart of systemd. When shairport-sync was run from the CLI or /etc/rc.local there were no errors present.
Jan 24 00:38:45 raspberrypi shairport-sync[617]: sudo: no tty present and no askpass program specified
Please note, that the only difference is how shairport-sync is initially started, when devices connect or disconnect shairport-sync continues to run.
|
Variations of "Why do things behave differently under systemd?" are a frequently asked question.
Any time something runs from the CLI and not from systemd, there are a some broad categories of possibilities to account for differences.
Different environment variables. systemd documents the environment variables it passes in man systemd.exec in the section Environment variables in spawned processes. If you want to inspect the difference yourself, you can use systemd-run /path/to/binary, it will run your app in a transient scope, as it would be run by a systemd service. You'll get output like: Running as unit: run-u160.service. You can then journalctl -u run-u160.service to review the output. Modify your app to dump out the environment variables it receives and compare the CLI run to the systemd run. If the app isn't conveniently modified, you can just use systemd-run env to see the environment variables that would be passed and review the resulting journal logging for it. If you are trying to start an X11 GUI app, the DISPLAY environment variable needs to be set. In that case, consider using your desktop environment's "autostart" feature instead of systemd.
Resource restrictions. See man systemd.resource-control for configuration values which could restrict resource consumption. Use systemctl show your-unit-unit.service to check the full configuration values affecting the service you attempting to start.
Non-interactive shell. Your bash CLI environment is an interactive login shell. It has sourced files like .bashrc that systemd has not. Besides setting environment variables, these scripts can do any number of other things, such as connecting an SSH agent so that SSH actions don't require a login. See also Difference between Login Shell and Non-Login Shell?
No TTY. Your interactive session is connected to a TTY that some programs like sudo and ssh expect when prompting for passwords. See also sudo: no tty present and no askpass program specified
Relative vs. Absolute Paths. Relative binary work in the shell, but as documented in man systemd.service, the first argument to ExecStart= must be an absolute path to a binary.
Restricted command line syntax. Shell CLIs support many metacharacters, while systemd has a very restricted command line syntax. Depending on your needs, you may be able to replicate Shell syntax with systemd by explicitly running your command through a shell: ExecStart=/bin/bash -c '/my/bash $(syntax) >/goes-here.txt'
It's a feature that system runs your code in a consistent environment with resource controls. This helps with reproducible, stable results in the long run without overwhelming the hardware.
| Difference between systemd and terminal starting program |
1,383,747,898,000 |
On my NFS server, I have the following export defined:
#NFS exports Database
/shared -alldirs -network=192.168.1 -mask=255.255.255.0
On my NFS client:
192.168.1.7:/shared /shared nfs rw 0 0
Obviously, as root on the server, I can do whatever I want. On the client however, my regular user 'gabe' can make changes to the nfs mount (assuming I have permissions to), but root cannot.
As my regular user:
gabe@client$ cd /shared
gabe@client$ ls -l
total 8
drwxrwxrwx 4 gabe wheel 512 Mar 20 19:20 tmp
gabe@client$ cd tmp
gabe@client$ touch test.txt
gabe@client$ rm test.txt
As root:
# cd /shared/tmp
# touch test.txt
touch: test.txt: Permission denied
Again, this is all on the NFS client side of things, and I suspect perhaps it has something to do with the -maproot option. This is the first time I'm setting up NFS and I just noticed this peculiarity. I'm going to do some reading now, to see if I can figure this out, but if anyone has any insight, I would appreciate it.
|
NFS was designed with the idea that user and group ids would be the same on all machines across the network. For ordinary users, that works ok. But root's UID is always 0, and just because you have root on one box, it doesn't mean that you should have root access to every machine on the network.
Therefore, NFS treats root specially. By default, root is mapped to the nobody user, which normally has no write access. The -maproot option allows you to change how root is handled. BSD's -maproot=root corresponds to Linux's no_root_squash option.
| Why can't root on one machine change nfs mounted content from another machine? |
1,383,747,898,000 |
I'm trying to make Wordpress work. I currently have this error message:
Could not create directory. /var/www/html/wp-content/upgrade/theme_name
when trying to upload a theme. This is the permissions set to /var/www/html/wp-content/upgrade/
drwxrwxr-x 3 ec2-user apache 4096 Jun 21 00:30 upgrade
chmod 777 upgrade makes the error go away. But that is not considered best practice. However, I think this should work too... why not?
I guess the web server may not be included by the above permissions. What group should I use to allow the web server to write?
(My setup is Amazon EC2, Amazon Linux AMI with httpd)
|
I don't know anything about Amazon EC2, but you should be able to:
Retrieve the name of the user running Apache with a command similar to this:
ps aux | grep apache # The username should be in the first column.
Retrieve the groups this user is part of with the groups(1) command:
groups [USERNAME]
| How to check which Apache group I can use for the web server to write? |
1,383,747,898,000 |
I created a directory d and a file f inside it. I then gave myself only read permissions on that directory. I understand this should mean I can list the files (e.g. here), but I can't.
will@wrmpb /p/t/permissions> ls -al
total 0
drwxr-xr-x 3 will wheel 102 4 Oct 08:30 .
drwxrwxrwt 16 root wheel 544 4 Oct 08:30 ..
dr-------- 3 will wheel 102 4 Oct 08:42 d
will@wrmpb /p/t/permissions> ls d
will@wrmpb /p/t/permissions>
If I change the permissions to write and execute, I can see the file.
will@wrmpb /p/t/permissions> chmod 500 d
will@wrmpb /p/t/permissions> ls d
f
will@wrmpb /p/t/permissions>
Why is this? I am using MacOS.
Edit: with reference to @ccorn's answer, it's relevant that I'm using fish and type ls gives the following:
will@wrmpb /p/t/permissions> type ls
ls is a function with definition
function ls --description 'List contents of directory'
command ls -G $argv
end
|
Some preparations, just to make sure that ls does not try more things
than it should:
$ unalias ls 2>/dev/null
$ unset -f ls
$ unset CLICOLOR
Demonstration of the r directory permission:
$ ls -ld d
dr-------- 3 ccorn ccorn 102 4 Okt 14:35 d
$ ls d
f
$ ls -l d
ls: f: Permission denied
$ ls -F d
ls: f: Permission denied
In traditional Unix filesystems, a directory was simply a list of (name, inode
number) pairs. An inode number is an integer used as index into the filesystem's
inode table where the rest of the file metadata is stored.
The r permission on a directory allows to list the names in it,
but not to access the information stored in the inode table, that is,
getting file type, file length, file permissions etc, or opening the file.
For that you need the x permission on the directory.
This is why ls -l, ls -F, ls with color-coded output etc fail
without x permission, whereas a mere ls succeeds.
The x permission alone allows inode access, that is, given an explicit
name within that directory, x allows to look up its inode and access that directory entry's metadata:
$ chmod 100 d
$ ls -l d/f
-rw-r--r-- 1 ccorn ccorn 0 4 Okt 14:35 d/f
$ ls d
ls: d: Permission denied
Therefore, to open a file /a/b/c/f or list its metadata,
the directories /, /a, /a/b, and /a/b/c must be granted x permission.
Unsurprisingly, creating directory entries needs both w and x permissions:
$ chmod 100 d
$ touch d/g
touch: d/g: Permission denied
$ chmod 200 d
$ touch d/g
touch: d/g: Permission denied
$ chmod 300 d
$ touch d/g
$
Wikipedia has a brief overview in an article on file system permissions.
| Why can't I list a directory with read permissions? |
1,383,747,898,000 |
This question Unix & Linux: permissions 755 on /home/ covers part of my question but:
Default permissions on a home directory are 755 in many instances. However that lets other users wander into your home folder and look at stuff.
Changing the permissions to 711 (rwx--x--x) means they can traverse folders but not see anything. This is required if you have authorized_keys for SSH - without it the SSH gives errors when trying to access the system using a public key.
Is there some way to set up the folders / directories so SSH can access authorized_keys, postfix / mail can access files it requires, the system can access config files but without all and sundry walking the system?
I can manually make the folder 711, set ~/.ssh/authorized_keys to 644 but remembering to do that every time for every config is prone to (my) mistakes.
I would have thought by default all files were private unless specifically shared but with two Ubuntu boxes (admittedly server boxes) everyone can read all newly created files. That seems a little off as a default setting.
|
As noted in the manual by default home folders made with useradd copy the /etc/skel folder so if you change it's subfolder rights all users created after in with default useradd will have the desired rights. Same for adduser. Editing "UMASK" in /etc/login.defs will change the rights when creating home folders.
If you want more user security you can encrypt home folders and put ssh keys in /etc/ssh/%u instead of /home/%u/.ssh/authorized_keys .
| Default permissions on Linux home directories |
1,383,747,898,000 |
I have a directory with log files and I'm putting
logs from script launched by users into them. Logging with syslog doesn't seem
possible in this case. (non-daemon rsync)
I want the users to have only write permissions on log files.
The problem is, that write permissions must be further restricted, so that
users (script) can only append to that files.
The underlying filesystem is XFS.
The following doesn't work:
# chattr +a test.log
chattr: Inappropriate ioctl for device while reading flags on test.log
Is there any other solution for this? Thank you for your hints.
|
The chattr utility is written for ext2/ext3/ext4 filesystems. It emits ioctls on the files, so it's up to the underlying filesystem to decide what to do with them. The XFS driver in newer Linux kernels supports the same FS_IOC_SETFLAGS ioctl as ext[234] to control flags such as append-only, but you may be running an older kernel where it doesn't (CentOS?). Try using the xfs_io utility instead:
echo chattr +a | xfs_io test.log
Note that, for XFS like for ext[234], only root can change the append-only flag (more precisely, you need the CAP_LINUX_IMMUTABLE capability).
| Restrict file access to append only |
1,383,747,898,000 |
I created a Bash script which echoes "Hello World". I also created a test user, bob, using adduser.
Nobody has permission to execute that file as denoted by ls:
$ ls -l hello.sh
-rw-r--r-- 1 george george 19 Mai 29 13:06 hello.sh
As we can see from the above the file's owner is george where he has only read and write access but no execute access. But logged in as george I am able to execute the script directly:
$ . hello.sh
Hello World
To make matters worse, I log in as bob, where I have only read permission, but I am still able to execute the file:
$ su bob
Password:
$ . /home/george/testdir/hello.sh
Hello World
What's going on?
|
In your examples, you are not executing the files, but sourcing them.
Executing would be via
$ ./hello.sh
and for that, execution permission is necessary. In this case a sub-shell is opened in which the commands of the script file are executed.
Sourcing, i.e.
$ . hello.sh
(with a space in between) only reads the file, and the shell from which you have called the . hello.sh command then executes the commands directly as read, i.e. without opening a sub-shell. As the file is only read, the read permission is sufficient for the operation. (Also note that stating the script filename like that invokes a PATH search, so if there is another hello.sh in your PATH that will be sourced! Use explicit paths, as in . ./hello.sh to ensure you source "the right one".)
If you want to prevent that from happening, you have to remove the read permission, too, for any user who is not supposed to be using the script. This is reasonable anyway if you are really concerned about unauthorized use of the script, since
non-authorizeded users could easily bypass the missing execution permission by simply copy-and-pasting the script content into a new file to which they could give themselves execute permissions, and
as noted by Kusalananda, otherwise an unauthorized user could still comfortably use the script by calling it via
sh ./hello.sh
instead of
./hello.sh
because this also only requires read permissions on the script file (see this answer e.g.).
As a general note, keep in mind that there are subtle differences between sourcing and executing a script (see this question e.g.).
| How are users able to execute a file without permission? |
1,383,747,898,000 |
I had a question on a job interview:
How can you execute (run) the program with the user user1 without sudo privileges and without access to the root account:
$ whoami
user1
$ ls -l ~/binary_program
-rw-r--r-- 1 root root 126160 Jan 17 18:57 /home/user1/binary_program
|
Since you have read permission:
$ cp ~/binary_program my_binary
$ chmod +x my_binary
$ ./my_binary
Of course this will not auto-magically grant you escalated privileges. You would still be executing that binary as a regular user.
| Run a binary owned by root without sudo |
1,383,747,898,000 |
When I type in cd .ssh in terminal, it returns with -bash: cd: .ssh/: Permission denied. Now I cannot add my ssh keys to ssh.
When I type ssh-add ~/.ssh/idname it says /Users/Dan/.ssh/idname: Permission denied.
I think it has to do with me typing ls -d because it worked before I typed this into terminal?
|
Since you have "Permission denied" on a directory, it is likely that the directory does not have execute permissions. Similarly, to traverse a directory tree to get at a file, you would need execute permissions on each directory in between the root and the file (hence the same error for the other command).
Try setting the execute permissions on the directory
chmod u+xr,go-rwx ~/.ssh
Then see if you can run those statements again.
| -bash: cd: .ssh/: Permission denied |
1,383,747,898,000 |
Steps to reproduce:
germar@host:~$ cd /tmp/
germar@host:/tmp$ touch test && chmod u+s test && ls -la test
-rwSr--r-- 1 germar germar 0 Nov 2 20:11 test
germar@host:/tmp$ chown germar:germar test && ls -la test
-rw-r--r-- 1 germar germar 0 Nov 2 20:11 test
Tested with Debian squeeze and Ubuntu 12.04
|
Not a bug according to chown documentation:
$ info coreutils 'chown invocation'
The `chown' command sometimes clears the set-user-ID or set-group-ID
permission bits. This behavior depends on the policy and functionality
of the underlying `chown' system call, which may make system-dependent
file mode modifications outside the control of the `chown' command.
For example, the `chown' command might not affect those bits when
invoked by a user with appropriate privileges, or when the bits signify
some function other than executable permission (e.g., mandatory
locking). When in doubt, check the underlying system behavior.
| chown removes setuid bit: bug or feature? |
1,383,747,898,000 |
EDIT: The problem was that Apple uses permissions to mark backups and prevents you from modifying them (probably a security feature). By using chmod -RN <dir> I removed ACL data from all the folders with important data and that allowed me to make myself the owner and apply the appropriate permissions.
Original question
I have an extremely large backup (>700GB) that now has the wrong permissions (my UID changed during clean install, long story) and I need to change them. The time-consuming option is to manually go through each folder and change the permissions but that will take ages.
I want to use chown to make myself the owner of all my important data and then use chmod 700 on all those folders to give rwx permissions to only me.
The ideal solution is some method of using find to recursively look for folders matching a regex (my current one is .*/[DCV].*|Pictures|M[ou].*) and then make my UID the owner and set the permissions to 700.
The important bit that I can't grasp:
However, when I try to run chown Me DirectoryName I get chown: DirectoryName: Operation not permitted.
Everything I find is related to changing the permissions of a file and not a directory. Maybe I'm looking at this the wrong way?
Something tells me there isn't a way of giving my UID rwx and --- to everyone else.
How can I achieve this? I'm running Mac OS X 10.10.3.
I know that this is a UNIX/Linux forum (and I'm running Mac) but this question is a lot more about using the shell, chown, chmod, and permissions and any solutions posted here will be applicable to any UNIX-based OS. It would be preferable if the posted solutions will make my older backups reappear in Time Machine.
Thanks to all who have promptly replied, but chown just doesn't seem to work on directories for some reason. Is the fact that this is a .sparsebundle disk image on a network drive relevant? I assumed it would be the same as on any external drive.
|
I may have misunderstood. But you can recursively use chmod and chown eg.
chown -R username:username /path/directory
To recursively apply permission 700 you can use:
chmod -r 700 /path/directory
Of course the above is for Linux so not sure if mac osx is the same.
EDIT: Yea sorry forgot to mention you need to be root to chown something, I just assumed u knew this...my bad.
| How to Change Owner of a Directory |
1,383,747,898,000 |
I just installed Arch. Works great. I created a new user, logged in with it and now I'm trying to install several things with pacman. But I keep getting the error: error: you cannot perform this operation unless you are root. I can use pacman with root just fine, but is there a way to allow my new user to perform these actions?
I tried logging into root and using gpasswd:
gpasswd -a jack root
but that didn't do anything.
|
You should read the Arch Wiki page on sudo.
sudo ("substitute user do") allows a system administrator to delegate authority to give certain users (or groups of users) the ability to run some (or all) commands as root or another user while providing an audit trail of the commands and their arguments.
You can install sudo from the repositories and then configure it to allow your user, jack, access to privileged commands by editing /etc/sudoers. Make sure you do this using the visudo command.
To give the user jack full root privileges, you would add this line:
jack ALL=(ALL) ALL
| How to give user root permissions? |
1,383,747,898,000 |
I am working as user, and I would like to create a tar archive, which when unpacked (by root) will extract its files with root ownership (otherwise root would have to change the ownership manually for each file, after the files have been extracted to its destination).
I have found fakeroot which seems to do exactly that. But i am not able to find the syntax which I need to use to create my archive.
How can I create a tar.xz archive, so that the files have root ownership when unpacked by root?
do something with fakeroot ...
tar cfpJ foo.tar.xz foo/
|
How can I create a tar.xz archive, so that the files have root ownership when unpacked by root?
That's up to the root who unpacks:
tar --no-same-owner -xf ...
If you want to make them all root to start with, you can use
tar --owner=root --group=root -cf ...
| modify file ownership for files inside tar archive |
1,383,747,898,000 |
My question is what settings do I need to change and/or commands to run to allow me to log into my vsftpd system?
I am getting this error, when I login using ftp instead of sftp:
Name (localhost:dbadmin): dbadmin
331 Please specify the password.
Password:
500 OOPS: cannot change directory:/home/dbadmin
Login failed.
ftp>
This works when logging in using sftp@, but my server is behind a firewall, and I need to be able to login using ftp as well as sftp.
I have been looking at quite a few posts about the "OOPS" error but so far have had no luck logging in.
Here is some information about my system and settings:
I am running CentOS 6.4.
iptables and ip6tables are stopped and disabled.
My home directory is protected 700, and I have tried 750, just to see if that made a difference. It did not.
Here are the active lines in /etc/vsftpd/vsftpd.conf
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
My login name is not in user_list.
|
Run this one command, no need to restart any service & server:
# setenforce 0
To check SELinux status :
# getenforce
or
edit the file /etc/sysconfig/selinux to include
SELINUX=disabled
Doing so will require a reboot.
| What are the settings to correct vsftpd "500 OOPS: cannot change directory" error? |
1,383,747,898,000 |
I ran the following command to give the wheel group rwx permissions on new files and subdirectories created:
[belmin@server1]$ ls -la
total 24
drwxr-sr-x+ 2 guards guards 4096 Aug 27 15:30 .
drwxr-xr-x 104 root root 12288 Aug 27 15:19 ..
[belmin@server1]$ sudo setfacl -m d:g:wheel:rwX .
[belmin@server1]$ getfacl .
# file: .
# owner: guards
# group: guards
# flags: -s-
user::rwx
group::r-x
group:wheel:rwx
other::r-x
default:user::rwx
default:group::r-x
default:group:wheel:rwx
default:mask::rwx
default:other::r-x
However, when I create a file as root, I am not completely clear how the effective permissions are calculated:
[belmin@server1]$ sudo touch foo
[belmin@server1]$ getfacl foo
# file: foo
# owner: root
# group: guards
user::rw-
group::r-x #effective:r--
group:wheel:rwx #effective:rw-
group:guards:rwx #effective:rw-
mask::rw-
other::r--
Can someone elaborate on what this means?
|
effective permissions are formed by ANDing the actual (real?) permissions with the mask. Since the mask of your file is rw-, all the effective permissions have the x bit turned off.
| How does ACL calculate the effective permissions on a file? |
1,383,747,898,000 |
I've noticed that after uninstalling the postgresql package in ArchLinux the postgres user and group are not removed automatically. The same is true for some other packages. Investigating this further, I've come across this page, which states:
The packages listed here use userdel/groupdel to remove the user they
created. These should never be removed automatically as it poses a
security risk if any files are left behind with this ownership.
I wonder why leaving files with this ownership poses a security risk?
|
This is a security risk because file ownership in the FS is stored not by symbolic name, but by UID and GID. If a user is removed and files remain owned by that user, they become inaccessible under owner permission. However, if a different user is later created that is allocated the same UID, that user will gain ownership of the files. This is potentially a security risk because of the various ways in which file ownership is used as a security mechanism; the simplest form is that where confidential information (e.g. SSH keys in id_rsa and so forth, wi-fi authentication information in wpa_supplicant.conf) could be leaked to the new user.
| Why does ArchLinux keep some users/groups after package uninstallation? |
1,383,747,898,000 |
I have a directory that has the following permissions set:
drwxr-s---. user group folder
On the desktop, I access this folder and right click to create a new file call foo.txt. Then using the terminal, I created another file using the command $ touch bar.txt.
When I check the permissions for these files, I have:
-rw-r--r--. user group foo.txt
-rw-rw-r--. user group bar.txt
I was expecting -rw-r-----. user group. How did the extra write permission for group and read permission for others come about?
|
setguid
There are 2 forces here at work. The first is the setgid bit that's enabled on the folder, folder.
drwxr-s---. user group folder
That's the s in the pack of characters at the beginning of this line. They're grouped thusly:
d - directory
rwx - read/write/execute bits for user
r-s - read/execute/setuid bits for group
--- - nothing for other users
The r-s means that any files or directories created inside this folder will have the group automatically set to the group group.
That's what caused the files foo.txt and bar.txt to be created like so:
-rw-r--r--. user group foo.txt
-rw-rw-r--. user group bar.txt
permissions & umask
The permissions you're seeing are another matter. These are governed by the settings for your umask. You can see what your umask is set to with the command umask:
$ umask
0002
NOTE: these bits are also called "mode" bits.
It's a mask so it will disable any of the bits related to permissions which are enabled. In this example the only bit I want off is the write permissions for other.
0 - skipping for this conversation
0 - value of user bits
0 - value of group bits
2 - value of other bits
The representation of the "bits" in this command are in decimal form. So a 2 equates to 010 in binary form, which is the write bit. A 4 (100) would mean you want read disabled. A 7 (111) means you want read/write/execute all disabled. Building it up from here:
$ umask 007
Would disable the read/write/execute bits for other users.
So then what about your files?
Well the umask governs the permissions that will get set when a new file is created. So if we had the following umask set:
$ umask 007
And started touching new files, we'd see them created like so:
$ touch newfile1.txt newfile2.txt
$ ls -l |grep newfile
-rw-rw---- 1 saml saml 0 Nov 3 22:34 newfile1.txt
-rw-rw---- 1 saml saml 0 Nov 3 22:34 newfile2.txt
If we changed it to something else, say this:
$ umask 037
$ ls -l |grep newfile
-rw-rw---- 1 saml saml 0 Nov 3 22:36 newfile1.txt
-rw-rw---- 1 saml saml 0 Nov 3 22:36 newfile2.txt
-rw-r----- 1 saml saml 0 Nov 3 22:35 newfile3.txt
-rw-r----- 1 saml saml 0 Nov 3 22:35 newfile4.txt
It won't have any impact on files that we've already created though. See here:
$ umask
0037
$ touch newfile1.txt newfile2.txt
$ ls -l | grep newfile
-rw-rw---- 1 saml saml 0 Nov 3 22:37 newfile1.txt
-rw-rw---- 1 saml saml 0 Nov 3 22:37 newfile2.txt
-rw-r----- 1 saml saml 0 Nov 3 22:35 newfile3.txt
-rw-r----- 1 saml saml 0 Nov 3 22:35 newfile4.txt
So then what's going on with the file browser?
The umask is what I'd called a "soft" setting. It is by no means absolute and can be by-passed fairly easily in Unix in a number of ways. Many of the tools take switches which allow you to specify the permissions as part of their operation.
Take mkdir for example:
$ umask
0037
$ mkdir -m 777 somedir1
$
$ ls -ld somedir1
drwxrwxrwx 2 saml saml 4096 Nov 3 22:44 somedir1
With the -m switch we can override umask. The touch command doesn't have this facility so you have to get creative. See this U&L Q&A titled: Can files be created with permissions set on the command line? for just such methods.
Other ways? Just override umask. The file browser is most likely either doing this or just completely ignoring the umask and laying down the file using whatever permissions it's configured to do as.
| How are file permissions applied to newly created files? |
1,383,747,898,000 |
I am running a dual boot of Windows and Debian on my Laptop. I use Linux mostly but from time to time I need to access my files in my Windows partition. My Windows partition is mounted as follows at startup.
>cat /etc/fstab |grep Win7
LABEL=Windows7_OS /mnt/Win7 auto nosuid,nodev,nofail,x-gvfs-show 0 0
Basically every file in the Windows partition is owned by root:root and has a 777 permission. Then whenever I mv a file to my working (Linux) partition, I have a 777 file under my partition, owned by me (while cp in terminal will give a 755 file but if done via gnome will save the file with a 777 permission).
Is this the best practice to mount a partition? Or should I mount it such that instead of root, I am the owner of all files/dirs and somehow be able to set all dirs to 755 and files to 644 when the mount happens at boot? If so, how can it be done?
|
You can use fmask and dmask mount options* to change the permission mapping on an ntfs filesystem.
To make files appear rw-r--r-- (644) and directories rwxr-xr-x (755) use fmask=0133,dmask=0022. You can combine this with uid= and gid= options to select the file owner and group if you need write access for your user.
* fmask and dmask seem to work for the kernel (read-only) driver as well, even that they are not documented in mount man page. They are documented options for ntfs-3g.
| Best practice for mounting a Windows partition |
1,383,747,898,000 |
I have a command that accepts a file as an argument, modifies the file, then writes it to the file name specified in the second argument. I'll call that program modifyfile.
I wanted it to work "in place" so I wrote a shell script (bash) that modifies it to a temporary file then moves it back:
TMP=`mktemp`
modifyfile "$original" "$TMP"
mv -v "$TMP" "$original"
This has the unfortunate side effect of destroying the permissions on this file. The file gets re-created with default permissions.
Is there a way to tell the mv command to overwrite the destination without altering its permissions? Or alternately is there a way to save the user, group, and permisssions from the original and restore them?
|
Instead of using mv, just redirect cat. For example:
TMP=$(mktemp)
modifyfile "$original" "$TMP"
cat "$TMP" > "$original"
This overwrites $original with the contents of $TMP, without touching anything at the file level.
| Maintain (or restore) file permissions when replacing file |
1,383,747,898,000 |
So, I've been using Linux for a few years now, and I really should know this answer, but I'm having trouble finding it. Specifically I've been using Debian based distro's....mostly Ubuntu.
If I have a server, that has more than three users, how do I set a different set of permissions to a file for each user.
For example:
If I have a file with these permissions and ownership:
rwx rw_ r__ user1:group1 file1.txt
and I have 3 users with these desired permissions....
user1 rwx
user2 rw_
user3 r__
All I have to do is have user1 own the file, user2 be in group1, and user3 can be neither -- correct?
But, what if I have a user4 and user5.
user4 _wx
user5 __x
How would I set that up?
I haven't had to do this before, but I was asked that question by a Windows admin, and I honestly couldn't answer.
|
Traditional unix permissions only allow user, group, other permissions as you've found. These can result in some awkward combination of groups needing to be created...
So a new form of ACL (Access Control Lists) were tacked on. This allows you to specify multiple users and multiple groups with different permissions. These are set with the setfacl command and read with getfacl
$ setfacl -m u:root:r-- file.txt
$ setfacl -m u:bin:-wx file.txt
$ setfacl -m u:lp:--x file.txt
$ getfacl file.txt
# file: file.txt
# owner: sweh
# group: sweh
user::rw-
user:root:r--
user:bin:-wx
user:lp:--x
group::r--
mask::rwx
other::r--
You can easily tell if a file has an ACL by looking at the ls output:
$ ls -l file.txt
-rw-rwxr--+ 1 sweh sweh 0 Jul 26 10:33 file.txt
The + at the end of the permissions indicates an ACL.
| How do I grant different permissions for each user? |
1,383,747,898,000 |
I use this rsync invocation to backup my home directory:
rsync -aARrx --info= --force --delete --info=progress2 -F "$USER_HOME" "$BACKUP_MNTPOINT"
rsync man page says that -a implies -g and -o (among other switches), which should preserve ownership. However I've noticed that if a directory does not exist under $BACKUP_MNTPOINT/$USER_HOME, it is created with root:root ownership instead of the correct one. (This only happens with directories right under $BACKUP_MNTPOINT/$USER_HOME). Why is that?
$BACKUP_MNTPOINT is a localy mounted drive. $BACKUP_MNTPOINT/$USER_HOME does have the right ownership and permissions. Neither $USER_HOME nor $BACKUP_MNTPOINT end with a slash.
Both the source and the target filesystems are XFS and running mkdir $BACKUP_MNTPOINT/$USER_HOME creates a directory with the expected ownership.
|
I had a similar problem when using rsync to backup my system to my server. I used:
rsync -aAXSHPr \
-e ssh \
--rsync-path="sudo /usr/bin/rsync/" \
--numeric-ids \
--delete \
--progress \
--exclude-from="/path/to/file/that/lists/excluded/folders.txt" \
--include-from="/path/to/file/that/lists/included/folders.txt" \
/ USER@SERVER:/path/to/folder/where/backup/should/go/
The solution is that there is not really a problem. I suspect that you aborted the rsync process once you saw that it creates folders with wrong permissions set. The crux is that rsync only sets the permissions of a parent-folder once it is done syncing all subfolders and files of it.
| rsync doesn't preserve directory ownership even with -a |
1,383,747,898,000 |
This is something I haven't been able to find much info on so any help would be appreciated.
My understanding is thus. Take the following file:
-rw-r----- 1 root adm 69524 May 21 17:31 debug.1
The user phil cannot access this file:
phil@server:/var/log$ head -n 1 debug.1
cat: debug.1: Permission denied
If phil is added to the adm group, it can:
root@server:~# adduser phil adm
Adding user `phil' to group `adm' ...
Adding user phil to group adm
Done.
phil@server:/var/log$ head -n 1 debug.1
May 21 11:23:15 server kernel: [ 0.000000] DMI: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.7.5.1-0-g8936dbb-20141113_115728-nilsson.home.kraxel.org 04/01/2014
If, however, a process is started whilst explicitly setting the user:group to phil:phil it cannot read the file. Process started like this:
nice -n 19 chroot --userspec phil:phil / sh -c "process"
If the process is started as phil:adm, it can read the file:
nice -n 19 chroot --userspec phil:adm / sh -c "process"
So the question really is:
What is special about running a process with a specific user/group combo that prevents the process being able to access files owned by supplementary groups of that user and is there any way around this?
|
A process is run with a uid ang a gid. Both have permissions assigned to them. You could call chroot with a userspec of a user and group, where actually the user is not in that group. The process would then executed with the users uid and the given groups gid.
See an example. I have a user called user, and he is in the group student:
root@host:~$ id user
uid=10298(user) gid=20002(student) groups=20002(student)
I have a file as follows:
root@host:~$ ls -l file
-rw-r----- 1 root root 9 Mai 29 13:39 file
He cannot read it:
user@host:~$ cat file
cat: file: Permission denied
Now, I can execte the cat process in the context of the user user AND the group root. Now, the cat process has the necessary permissions:
root@host:~$ chroot --userspec user:root / sh -c "cat file"
file contents
Its interesting to see what id says:
root@host:~$ chroot --userspec user:root / sh -c "id"
uid=10298(user) gid=0(root) groups=20002(student),0(root)
Hm, but the user user is not in that group (root). Where does id get its informations from? If called without argument, id uses the system calls, getuid(), getgid() and getgroups(). So the process context of id itself is printed. That context we have altered with --userspec.
When called with an argument, id just determines the group assignments of the user:
root@host:~$ chroot --userspec user:root / sh -c "id user"
uid=10298(user) gid=20002(student) groups=20002(student)
To your question:
What is special about running a process with a specific user/group
combo that prevents the process being able to access files owned by
supplementary groups of that user and is there any way around this?
You can set the security process context that is needed to solve whatever task the process needs to do. Every process has a uid and gid set under which he runs. Normally the process "takes" the calling users uid and gid as his context. With "takes" I means the kernel does, otherwise it would be a security problem.
So, it's actually not the user, that has no permissions to read the file, its the process' permissions (cat). But the process runs with the uid/gid of the calling user.
So you don't have to be in a specific group for a process to run with your uid and the gid of that group.
| How do Linux permissions work when a process is running as a specific group? |
1,383,747,898,000 |
I accidentally changed /var owner/group to my username and then changed it back to root, but not all the /var folders' owners are root, so is there anyway to change back owner/group of files/folders to default state? Or at least those files/folders that are created by packages?
|
Similar to one of the answers above, if you have a copy of the directory with the correct permissions named "var" in your local directory, you can use the following two commands to restore permissions to the /var directory.
sudo find var -exec chown --reference="{}" "/{}" \;
sudo find var -exec chmod --reference="{}" "/{}" \;
| How to restore default group/user ownership of all files under /var? |
1,383,747,898,000 |
Why cannot a user change the group permissions of files which he owns. Let's suppose I have user martin:
$ ls -lAd /home/martin/
drwx------ 8 martin martin 4096 Apr 20 01:06 /home/martin/
For some reason, I want user paul to be able to access my home.
$ chgrp paul /home/martin/
chgrp: changing group of `/home/martin/': Operation not permitted
Is there a security reason why a user cannot change the group ownership of his own files?
I am using Debian 7 (wheezy).
|
You can only chgroup to a group you are a member of:
$ groups
terdon sudo netdev fuse vboxsf vboxusers
$ chgrp fuse file
$ ls -l file
-rw-r--r-- 1 terdon fuse 531 Apr 15 19:17 file
$ chgrp mysql file
chgrp: changing group of ‘file’: Operation not permitted
This behavior is mentioned in the POSIX specs:
Only the owner of a file or the user with appropriate privileges may change the owner or group of a file.
Some implementations restrict the use of chgrp to a user with appropriate privileges when the group specified is not the effective group ID or one of the supplementary group IDs of the calling process.
The main reason for this is that if you aren't a member of a group, you should not be able to modify what that group has access to. This answer on chown permissions is also relevant.
Traditionally, on shared systems, you have a users group to which all regular users belong and that is the primary group of each user. That way, files are created owned by the users group and all users can read them.
Anyway, since that is not the way that Debian-based distros are set up these days, the way to give a specific user access to your directory would be to either
Change the group ownership of the directory to a group that both you and the other user are members of;
Just change the permissions of the directory accordingly:
$ chmod 755 /home/martin
$ ls -ld /home/martin/
drwxr-xr-x 170 martin martin 491520 Apr 20 13:43 /home/martin/
That will make the directory accessible to everybody. Paul included.
| Why cannot a user change group ownership of his own files? [duplicate] |
1,383,747,898,000 |
The command setfacl -dm g::rwx mydir sets permissions for groups to read-write-execute. I'd like to run a similar command such that other users (i.e. not the owner) have no access whatsoever, but setfacl -dm o:: mydir complains that option -m is incomplete. What is the proper way of expressing this?
|
An empty permission set can be represented with -:
setfacl -dm o::- mydir
This doesn't appear to be documented, so I don't know how portable it is. However, the documentation does mention that they can be specified as an octal digit (4 r, 2 w, 1 x, as in chmod), so:
setfacl -dm o::0 mydir
| How can I use setfacl to give no access to "other" users? |
1,383,747,898,000 |
As I've found out, when using umask, the highest permissions you can give to files are 666.
Which is done by umask 0000. That's because of the default file creation permissions, which appear to be 666 on every system that I know.
I know for files we need executable rights to show their contents.
But why do we limit the default file creation permissions on 666?
|
As far as I can tell, this is hard-coded into standard utilities. I straced both a touch creating a new file and a mkdir creating a new directory.
The touch trace produced this:
open("newfile", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3
while the mkdir trace produced this:
mkdir("newdir", 0777) = 0
Short of coding the file/directory creation process in C, I don't see a way of modifying the default permissions. It seems to me, though, that not making files executable by default makes sense: you don't want any random text to be accidentally misconstrued as shell commands.
Update
To give you an example of how the permission bits are hard-coded into the standard utilities. Here are some relevant lines from two files in the coreutils package that contains the source code for both touch(1) and mkdir(1), among others:
mkdir.c:
if (specified_mode)
{
struct mode_change *change = mode_compile (specified_mode);
if (!change)
error (EXIT_FAILURE, 0, _("invalid mode %s"),
quote (specified_mode));
options.mode = mode_adjust (S_IRWXUGO, true, umask_value, change,
&options.mode_bits);
free (change);
}
else
options.mode = S_IRWXUGO & ~umask_value;
}
In other words, if the mode is not specified, set it to S_IRWXUGO (read: 0777) modified by the umask_value.
touch.c is even clearer:
int default_permissions =
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
That is, give read and write permissions to everyone (read: 0666), which will be modified by the process umask on file creation, of course.
You may be able to get around this programmatically only: i.e. while creating files from within either a C program, where you make the system calls directly or from within a language that allows you to make a low-level syscall (see for example Perl's sysopen under perldoc -f sysopen).
| Why are 666 the default file creation permissions? |
1,383,747,898,000 |
How can I determine all files and folders that given user can write to? The user is nobody. Some script in bash or python, perhaps? I'm using Ubuntu 11.04
|
find / '(' -type f -o -type d ')' \
'(' '(' -user nobody -perm -u=w ')' -o \
'(' -group nobody -perm -g=w ')' -o \
'(' -perm -o=w ')' ')' -print
This will find all files and directories belonging to nobody that are writable by their owner, or that belong to the group nobody that are group writable, as well as all files or directories that are writable by anyone.
This would only take the primary group into account though.
Since generalizing this to take secondary groups (and ACLs etc.) into account would result in an even more unwieldy find expression, GNU find user could use
find / -type d -writable -print
This would report all directories writable by the current user. To use the same short syntax for finding directories writable by some other user, use sudo -u username find ..., i.e. change into that other user before running find (but see an answer to a related question for caveats and better alternatives).
| Find a user's writable files and directories [duplicate] |
1,383,747,898,000 |
I tried adding cap_sys_admin permissions to user myroot.
For this, I added these lines to /etc/security/capabilities:
cap_sys_admin myroot
none *
and this line to /etc/pam.d/su:
auth required pam_cap.so
But user myroot doesn't have these permissions.
What can I do to add these permissions to my user?
|
I believe the file is called /etc/security/capability.conf not /etc/security/capabilities. I was able to get this working like so:
$ cat /etc/security/capability.conf
cap_sys_admin user1
And then adding pam_cap.so to PAM. NOTE: It's imperative that pam_cap.so come before the pam_rootok.so line.
$ cat /etc/pam.d/su
#%PAM-1.0
auth optional pam_cap.so
auth sufficient pam_rootok.so
...
...
Example
Here with the above in place if I run the following su command:
$ su - user1
I can verify this user's capabilities:
$ capsh --print
Current: = cap_sys_admin+i
Bounding set =cap_chown,cap_dac_override,cap_dac_read_search,cap_fowner,cap_fsetid,cap_kill,cap_setgid,cap_setuid,cap_setpcap,cap_linux_immutable,cap_net_bind_service,cap_net_broadcast,cap_net_admin,cap_net_raw,cap_ipc_lock,cap_ipc_owner,cap_sys_module,cap_sys_rawio,cap_sys_chroot,cap_sys_ptrace,cap_sys_pacct,cap_sys_admin,cap_sys_boot,cap_sys_nice,cap_sys_resource,cap_sys_time,cap_sys_tty_config,cap_mknod,cap_lease,cap_audit_write,cap_audit_control,cap_setfcap,cap_mac_override,cap_mac_admin,cap_syslog,35,36
Securebits: 00/0x0/1'b0
secure-noroot: no (unlocked)
secure-no-suid-fixup: no (unlocked)
secure-keep-caps: no (unlocked)
uid=1001(user1)
gid=1001(user1)
groups=1001(user1)
The key line in that output:
Current: = cap_sys_admin+i
Packages
This was done on a CentOS 7.x box. I had these packages installed pertaining to capabilities:
$ rpm -qa | grep libcap
libcap-ng-utils-0.7.5-4.el7.x86_64
libcap-2.22-9.el7.x86_64
libcap-ng-0.7.5-4.el7.x86_64
They provide the following useful tools when dealing with capabilities:
$ rpm -ql libcap-ng-utils | grep /bin/
/usr/bin/captest
/usr/bin/filecap
/usr/bin/netcap
/usr/bin/pscap
$ rpm -ql libcap | grep /sbin/
/usr/sbin/capsh
/usr/sbin/getcap
/usr/sbin/getpcaps
/usr/sbin/setcap
NOTE: See the respective man pages for these tools if you need more info on their usage.
References
Is it possible to configure Linux capabilities per user? [closed]
Linux Kernel Capabilities Explained
How can I get capabilities to work with su?
Restricting and granting capabilities
| How do you add `cap_sys_admin` permissions to user in CentOS 7? |
1,456,433,541,000 |
Let's say user wants to execute a script test.sh but ls -l test.sh gives
-rwxrwxr-- 1 root root 96 Feb 25 21:44 test.sh
Now if user doesn't want to make a copy of test.sh (on which he does chmod +x), he can simply do
sh test.sh
to execute test.sh.
Is there an analogue way to execute binary programs which one doesn't have execute permissions?
|
Basically this is the same thing as one of the very famous UNIX technical interview questions, known for ages:
Assume someone with root access ran a command chmod -R 444 / and made the chmod binary non-executable. How do you recover from it ?
There is a perl answer and there is this one, which basically is running a non-executable program, chmod in this case:
/lib/ld-linux.so /bin/chmod +x /bin/chmod
I think you can apply it to any other program that you know is executable. Otherwise be ready to embrace the disaster, which may ensue
PS> /lib/ld-linux.so might differ in name. So if the direct match is not available, look around for similarly named so's. For instance on my CentOS 6 server, it is /lib/ld-linux.so.2 which is a symlink pointing to /lib/ld-2.12.so. So, your mileage may vary.
| How to execute a file without execute permissions [duplicate] |
1,456,433,541,000 |
I ran ls and found a directory that has the + bit set:
$ ls -ld /var/log/journal
drwxr-sr-x+ 3 root systemd-journal 4096 Oct 1 01:23 /var/log/journal
I understand drwxr-sr-x, but what is +? What is it called? What does it do?
|
It means there's an ACL associated with the entry.
eg
$ ls -ld X
drwxr-xr-x 2 sweh sweh 4096 Feb 16 20:06 X/
$ setfacl -m u:root:rwx X
$ ls -ld X
drwxrwxr-x+ 2 sweh sweh 4096 Feb 16 20:06 X/
$
You can read the ACL with the getfacl command:
$ getfacl X
# file: X
# owner: sweh
# group: sweh
user::rwx
user:root:rwx
group::r-x
mask::rwx
other::r-x
| What does a plus (+) after the file permission bits mean? [duplicate] |
1,456,433,541,000 |
Can someone explain to me how umask affects the default mask of newly created files if ACLs are activated? Is there some documentation about this?
Example:
$ mkdir test_dir && cd test_dir
$ setfacl -m d:someuser:rwx -m u:someuser:rwx . # give access to some user
$ getfacl .
# file: .
# owner: myUsername
# group: myGroup
user::rwx
user:someuser:rwx
group::---
mask::rwx
other::---
default:user::rwx
default:user:someuser:rwx
default:group::---
default:mask::rwx
default:other::---
$ umask # show my umask
077
$ echo "main(){}" > x.c # minimal C program
$ make x # build it
cc x.c -o x
$ getfacl x
# file: x
# owner: myUsername
# group: myGroup
user::rwx
user:someuser:rwx #effective:rw-
group::---
mask::rw-
other::---
I would expect mask:rwx. Actually after setting umask to e.g. 027 I get the expected behavior.
|
I found this example, titled: ACL and MASK in linux. In this article the following examples are demonstrated which I think help to understand how ACL's and umask interact with each other.
Background
When a file is created on a Linux system the default permissions 0666 are applied whereas when a directory is created the default permissions 0777 are applied.
example 1 - file
Suppose we set our umask to 077 and touch a file. We can use strace to see what's actually happening when we do this:
$ umask 077; strace -eopen touch testfile 2>&1 | tail -1; ls -l testfile
open("testfile", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3
-rw-------. 1 root root 0 Sep 4 15:25 testfile
In this example we can see that the system call open() is made with the permissions 0666, however when the umask 077 is then applied by the kernel the following permissions are removed (---rwxrwx) and we're left with rw------- aka 0600.
example - 2 directory
The same concept can be applied to directories, except that instead of the default permissions being 0666, they're 0777.
$ umask 022; strace -emkdir mkdir testdir; ls -ld testdir
mkdir("testdir", 0777) = 0
drwxr-xr-x 2 saml saml 4096 Jul 9 10:55 testdir
This time we're using the mkdir command. The mkdir command then called the system call mkdir(). In the above example we can see that the mkdir command called the mkdir() system call with the defaul permissions 0777 (rwxrwxrwx). This time with a umask of 022 the following permissions are removed (----w--w-), so we're left with 0755 (rwxr-xr-x) when the directories created.
example 3 (Applying default ACL)
Now let's create a directory and demonstrate what happens when the default ACL is applied to it along with a file inside it.
$ mkdir acldir
$ sudo strace -s 128 -fvTttto luv setfacl -m d:u:nginx:rwx,u:nginx:rwx acldir
$ getfacl --all-effective acldir
# file: acldir
# owner: saml
# group: saml
user::rwx
user:nginx:rwx #effective:rwx
group::r-x #effective:r-x
mask::rwx
other::r-x
default:user::rwx
default:user:nginx:rwx #effective:rwx
default:group::r-x #effective:r-x
default:mask::rwx
default:other::r-x
Now let's create the file, aclfile:
$ strace -s 128 -fvTttto luvly touch acldir/aclfile
# view the results of this command in the log file "luvly"
$ less luvly
Now get permissions of newly created file:
$ getfacl --all-effective acldir/aclfile
# file: acldir/aclfile
# owner: saml
# group: saml
user::rw-
user:nginx:rwx #effective:rw-
group::r-x #effective:r--
mask::rw-
other::r--
Notice the mask, mask::rw-. Why isn't it mask::rwx just like when the directory was created?
Check the luvly log file to see what default permissions were used for the file's creation:
$ less luvly |grep open |tail -1
10006 1373382808.176797 open("acldir/aclfile", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3 <0.000060>
This is where it get's a little confusing. With the mask set to rwx when the directory was created, you'd expect the same behavior for the creation of the file, but it doesn't work that way. It's because the kernel is calling the open() function with the default permissions of 0666.
To summarize
Files won't get execute permission (masking or effective). Doesn't matter which method we use: ACL, umask, or mask & ACL.
Directories can get execute permissions, but it depends on how the masking field is set.
The only way to set execute permissions for a file which is under ACL permissions is to manually set them using chmod.
References
acl man page
| How does umask affect ACLs? |
1,456,433,541,000 |
[db2inst1][testing ~/sqllib/db2dump] rm db2diag.log
rm: cannot remove `db2diag.log': Permission denied
[db2inst1][testing ~/sqllib/db2dump] id
uid=1002(db2inst1) gid=107(db2iadm1) groups=16(dialout),33(video),107(db2iadm1),108(db2fadm1),1001(eduserver)
[db2inst1][testing ~/sqllib/db2dump] ll
total 1381172
-rw-rw-rw- 1 root db2iadm1 1412931954 Oct 2 10:24 db2diag.log
Currently logged user (db2inst1) is part of db2iadm1 group that has w permission to the file I want to delete. But I am not able. Any idea why?
update - permission for parent directory
drwxr-sr-x 2 root db2iadm1 4096 Aug 22 14:39 db2dump
|
In order to delete a file, you must have write permissions on the directory that the file resides in. When you rm a file it makes the unlink system call which removes the name from the directory. This only deletes the file if it is the last remaining link to the inode.
You can find more information in unlink(2).
| Cannot delete a file - permission denied - why? |
1,456,433,541,000 |
I would like to inspect group permissions of a file from a bash script. Specifically, I need to check whether a file has the group writeable bit on.
That's it. Simple as that. However:
I also need this to be portable.
test -w <file won't tell me whether it's group writeable.
The output of ls -ld is nice for humans, but not so sure about scripts. Technically I could parse output such as drwxrwxr-x to extract the group bits, but this seems brittle.
The interface for stat is completely incompatible between OS X and other systems.
find <file> -perm ... can't possibly be the answer?
|
I ended up ls-parsing:
is_group_writable() {
local ls="$(ls -ld "$1")"
[ "${ls:5:1}" = "w" ]
}
I'm grateful for @slm's extensive answer, but none of the other methods are as straightforward. The stat method would require me to write at least two different invocations to cover OS X, and even after that it returns the octal representation which I still have to apply arithmetic to. The Perl method is OK but requires me to start an interpreter for such a simple task, and I would like to keep it simple and using only posix utils.
| How to inspect group permissions of a file |
1,456,433,541,000 |
Having worked with Linux for years, and finding myself with some free time, I decided to revisit some basics. So I re-read the stuff about permissions (without checking source code), and its special cases for folders, and came up with a new (to me at least...) way of thinking about folder permissions (for a specific user/group/others): I imagine a folder as a table with two columns, like so:
filename | inode
foo | 111
bar | 222
The read permission means you can read (and list) the left column of the table, the write permission corresponds to adding and removing entries to the table, and the execute permission corresponds to being able to translate from file name to inode; i.e. you can access the contents of the folder.
I did some experiments, and the results are all consistent with this "worldview" of mine, but one conclusion seems inescapable: that a folder with permissions d-w-------, is totally useless. Elaborating: you can't list its contents, you can't read any files you know exist inside (because you can't translate names into inodes), you can't remove or rename or add files, because again that would imply translation, and you can't even add hardlinks (because, I surmise, that would mean adding a name as well as an inode number, which means you would know both, which in turn, again surmising, violates the purpose of unsetting execution permission). And of course, if there are files inside one such folder, then you can't delete that folder either, because you can't delete its contents.
So... I would like to ask two questions:
Is this analogy of mine correct, or is it a big blunder?
Irrespective of the previous answer, is there any situation where having a folder with permissions as described is appropriate?
|
Your understanding is pretty much correct. A better way to think of the execute permission is that it allows you to do things with a file or directory name in the directory (other than just reading the name itself). Most of those things involve translating the name to an inode, but it also includes creating new names and removing existing names.
Write permission to the directory without execute is therefore pretty useless, since there's nothing you can actually write if you can't access the files within it.
| Folder with ONLY write permission is useless... right? |
1,456,433,541,000 |
How to create a file even root user can't delete it ?
|
Simple answer: You can't, root can do everything.
You can set the "i" attribute with chattr (at least if you are on ext{2,3,4}) which makes a file unchangeable but root can just unset the attribute and delete the file anyways.
More complex (and ugly hackish workaround):
Put the directory you want unchangeable for root on remote server and mount it via NFS or SMB. If the server does not offer write permissions that locks out the local root account. Of course the local root account could just copy the files over locally, unmount the remote stuff, put the copy in place and change that.
You cannot lock out root from deleting your files. If you cannot trust your root to keep files intact, you are having a social problem, not a technical one.
| How to create a file even root user can't delete it |
1,456,433,541,000 |
If I create a file as an unprivileged user, and change the permissions mode to 400, it's seen by that user as read-only, correctly:
$ touch somefile
$ chmod 400 somefile
$ [ -w somefile ] && echo rw || echo ro
ro
All is well.
But then root comes along:
# [ -w somefile ] && echo rw || echo ro
rw
What the heck? Sure, root can write to read-only files, but it shouldn't make a habit of it: Best Practice would tend to dictate that I should be able to test for the write permission bit, and if it's not, then it was set that way for a reason.
I guess I want to understand both why this is happening, and how can I get a false return code when testing a file that doesn't have the write bit set?
|
test -w aka [ -w doesn't check the file mode. It checks if it's writable. For root, it is.
$ help test | grep '\-w'
-w FILE True if the file is writable by you.
The way I would test would be to do a bitwise comparison against the output of stat(1) ("%a Access rights in octal").
(( 0$(stat -c %a somefile) & 0200 )) && echo rw || echo ro
Note the subshell $(...) needs a 0 prefixed so that the output of stat is interpreted as octal by (( ... )).
| Why is a file with 400 permissions seen writable by root but read-only by user? |
1,456,433,541,000 |
I need this for a unit test. There's a function that does lstat on the file path passed as its parameter. I have to trigger the code path where the lstat fails (because the code coverage has to reach 90%)
The test can run only under a single user, therefore I was wondering if there's a file in Ubuntu that always exists, but normal users have no read access to it, or to its folder. (So lstat would fail on it unless executed as root.)
A non-existent file is not a solution, because there's a separate code path for that, which I'm already triggering.
EDIT: Lack of read access to the file only is not enough. With that lstat can still be executed. I was able to trigger it (on my local machine, where I have root access), by creating a folder in /root, and a file in it. And setting permission 700 on the folder. So I'm searching for a file that is in a folder that is only accessible by root.
|
On modern Linux systems, you should be able to use /proc/1/fdinfo/0 (information for the file descriptor 1 (stdout) of the process of id 1 (init in the root pid namespace which should be running as root)).
You can find a list with (as a normal user):
sudo find /etc /dev /sys /proc -type f -print0 |
perl -l -0ne 'print unless lstat'
(remove -type f if you don't want to restrict to regular files).
/var/cache/ldconfig/aux-cache is another potential candidate if you only need to consider Ubuntu systems. It should work on most GNU systems as /var/cache/ldconfig is created read+write+searchable to root only by the ldconfig command that comes with the GNU libc.
| Is there a file that always exists and a 'normal' user can't lstat it? |
1,456,433,541,000 |
This question is associated with Where is core file with abrt-hook-cpp installed? .
While I was trying to generate a core file for an intentionally-crashing program, at first core file generation seemed to be stymied by abrt-ccpp. So I tried to manually editing /proc/sys/kernel/core_pattern with vim:
> sudo vim /proc/sys/kernel/core_pattern
When I tried to save the file, vim reported this error:
"/proc/sys/kernel/core_pattern" E667: Fsync failed
I thought this was a permission problem, so I tried to change permissions:
> sudo chmod 666 /proc/sys/kernel/core_pattern
chmod: changing permissions of '/proc/sys/kernel/core_pattern\': Operation not permitted
Finally, based on this post, I tried this:
>sudo bash -c 'echo /home/user/foo/core.%e.%p > /proc/sys/kernel/core_pattern'
This worked.
Based on the working solution, I also tried these, which failed:
> echo "/home/user/foo/core.%e.%p" > /proc/sys/kernel/core_pattern
-bash: /proc/sys/kernel/core_pattern: Permission denied
>
> sudo echo "/home/user/foo/core.%e.%p" > /proc/sys/kernel/core_pattern
-bash: /proc/sys/kernel/core_pattern: Permission denied
Question:
Why is it that editing, chmoding, and redirecting echo output to the file /proc/sys/kernel/core_pattern all failed, and only the noted invocation of sudo bash... was able to overwrite/edit the file?
Question:
Specifically, wrt the attempts to invoke sudo in the failed attempts above: why did they fail? I thought sudo executed the subsequent command with root privilege, which I thought let you do anything in Linux.
|
Entries in procfs are managed by ad hoc code. The code that would set permissions and ownership on the files under /proc/sys (proc_sys_setattr) rejects changes of permissions and ownership with EPERM. So it isn't possible to change the permissions or ownership of these files, full stop. Such changes are not implemented, so being root doesn't help.
When you try to write as a non-root user, you get a permission error. Even with sudo echo "/home/user/foo/core.%e.%p" > /proc/sys/kernel/core_pattern, you're trying to write as a non-root user: sudo runs echo as root, but the redirection happens in the shell from which sudo is executed, and that shell has no elevated privileges. With sudo bash -c '… >…', the redirection is performed in the bash instance which is launched by sudo and which runs as root, so the write succeeds.
The reason only root must be allowed to set the kernel.core_pattern sysctl is that it allows a command to be specified and, since this is a global setting, this command could be executed by any user. This is in fact the case for all sysctl settings to various degrees: they're all global settings, so only root can change them. kernel.core_pattern is just a particularly dangerous case.
| Why is editing core_pattern restricted? |
1,456,433,541,000 |
I have a directory with numerous files. Part of the files have the 755 permissions and the other part have 644 permissions. I'd like to convert the files with 755 permissions to 644. I have tried the following line by running it from the directory itself:
find . -perm 755 -exec chmod 644 {}\;
However as a result, the permission changed only for the directory itself and after changing it back I found out that the files permissions remained unchanged. Do I miss something?
|
Ok, it seems that I've found the problem. It seems that there must be a mandatory space between the {} and \;, so the command will look like this:
find . -perm 755 -exec chmod 644 {} \;
Rather than:
find . -perm 755 -exec chmod 644 {}\;
Also the issue with changing the directory permissions can be solved by adding a -type f flag, so it'll look as follows:
find . -type f -perm 755 -exec chmod 644 {} \;
| How to change permissions of multiple files found with find command? |
1,456,433,541,000 |
Possible Duplicate:
How to add write permissions for a group?
When I run Wireshark, It shows the following message:
Couldn't run /usr/sbin/dumpcap in child process: Permission denied
Are you a member of the 'wireshark' group? Try running
'usermod -a -G wireshark _your_username_' as root.
I ran this command: usermod -a -G wireshark myusername and checked my groups using groups myusername and I was added to the wireshark group. But I still get that error message.
[nima@nma ~]$ groups nima
nima : nima wheel dialout wireshark
[nima@nma ~]$ ls -l /usr/sbin/dumpcap
-rwxr-x--- 1 root wireshark 67884 Aug 16 12:04 /usr/sbin/dumpcap
[nima@nma ~]$ /usr/sbin/dumpcap
-bash: /usr/sbin/dumpcap: Permission denied
What's the problem?
|
You need to update your group IDs by using newgrp.
| wireshark: Couldn't run /usr/sbin/dumpcap in child process [duplicate] |
1,456,433,541,000 |
Say I have a folder called folder in the following path:
my_path = /a/b/c/d/e/folder
and a file called file in that folder.
Then, say I run this command to remove group permissions under /a/
> chmod g-rwx -R /a/
Now, say I give +rx permissions to folder:
> chmod g+rx /a/b/c/d/e/folder
Then, if a second user in my group runs:
> ls /a/b/c/d/e/folder
or
> cat /a/b/c/d/e/folder/file
she gets permission errors, and as far as I understand this is because I need to provide g+x access to to all the parents of folder. My question then is, when or why would it ever be useful to give +x permission to a directory whose parent does not have it?
Thanks
|
Most of the time, if you want to block access and usage of an entire directory (including its subdirectory), you can do it by removing it (non-recursively) -x. Therefore, you may have left subdirectories with +x, without doing any harm.
Keeping the permissions on the subdirectories can be useful for a number of reasons (especially when -x doesn't apply to everyone but at least one user can still do something).
For example, you could block usage of the container directory temporarily, while doing other changes to the permissions within that directory structure, and then re-enable access to the whole tree in one operation (giving +x to the top level directory).
You could also have a situation where a script (not necessarily run by the owner) backs up the directory tree in a temporary location (which shouldn't be readable by others) and puts everything in a tar file, preserving the permission settings of the content of the directory.
| Directory with +x permission, parents without it. When would this be useful? |
1,456,433,541,000 |
I've stumbled upon surprising (for me) permission behavior on FreeBSD. Let's say I'm operating as non-root user. I create a file, set its permission on read-only and then try to write into it:
$ touch f
$ chmod 400 f
$ ls -l f
-r-------- 1 user wheel f
$ echo a >> t
t: Permission denied.
So far so good. Now I do the same as root and it writes into the file:
# ls -l f2
-r-------- 1 root wheel f2
# echo a >> f2
# echo $?
0
Is it a bug or intended behavior? Can I safely assume that this would work so on any Unix & Linux?
|
It's normal for root to be able to override permissions in this manner.
Another example is root being able to read a file with no read access:
$ echo hello > tst
$ chmod 0 tst
$ ls -l tst
---------- 1 sweh sweh 6 Aug 16 15:46 tst
$ cat tst
cat: tst: Permission denied
$ sudo cat tst
hello
Some systems have the concept of immutable files. eg on FreeBSD:
# ls -l tst
-rw-r--r-- 1 sweh sweh 6 Aug 16 15:50 tst
# chflags simmutable tst
# echo there >> tst
tst: Operation not permitted.
Now even root can't write to the file. But, of course, root can remove the flag:
# chflags nosimmutable tst
# echo there >> tst
# cat tst
hello
there
With FreeBSD you can go a step further and set a kernel flag to prevent root from removing the flag:
# chflags simmutable tst
# sysctl kern.securelevel=1
kern.securelevel: -1 -> 1
# chflags nosimmutable tst
chflags: tst: Operation not permitted
Now no one, not even root can change this file.
(The system needs rebooting to reduce the securelevel).
| Can super user write into read-only files? |
1,456,433,541,000 |
Processes which de-escalate privileges via setuid() and setgid() do not seem to inherit the group memberships of the uid/gid they set.
I have a server process that must be executed as root in order to open a privileged port; after that it de-escalates to a specific non-privilleged uid/gid,1 -- e.g., that of user foo (UID 73). User foo is a member of group bar:
> cat /etc/group | grep bar
bar:x:54:foo
Hence if I login as foo, I can read a file /test.txt with these characteristics:
> ls -l /test.txt
-rw-r----- 1 root bar 10 Mar 8 16:22 /test.txt
However, the following C program (compile std=gnu99), when run root:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main (void) {
setgid(73);
setuid(73);
int fd = open("/test.txt", O_RDONLY);
fprintf(stderr,"%d\n", fd);
return 0;
}
Always reports Permission denied. I imagine this has to do with it being a non-login process, but it kind of hamstrings the way permissions are supposed to work.
1. Which is often SOP for servers, and I think there must be a way around this as I found a report of someone doing it with apache -- apache has been added to the audio group and can apparently then use the sound system. Of course, this likely happens in a fork and not the original process, but in fact the case is the same in my context (it's a child process forked subsequent to the setuid call).
|
The problem is that setuid and setgid are not sufficient to give your process all the credentials it needs. The authorizations of a process depend on
its UID
its GID
its supplementary groups
its capabilities.
See man 7 credentials to get a more detailed overview. So, in your case, the problem is that you correctly set the UID and GID, but you don't set the supplementary groups of the process. And group bar has GID 54, not 73,
so it is not recognized as a group your process is in.
You should do:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <grp.h>
int main (void) {
gid_t supplementary_groups[] = {54};
setgroups(1, supplementary_groups);
setgid(73);
setuid(73);
int fd = open("/test.txt", O_RDONLY);
fprintf(stderr,"%d\n", fd);
return 0;
}
| Group memberships and setuid/setgid processes |
1,456,433,541,000 |
I am attempting to mount my Amazon Kindle, 3rd Edition. It automounts correctly and when I run mount -l, I get the following output:
/dev/sdg1 on /media/usb0 type vfat (rw,noexec,nodev,sync,noatime,nodiratime) [Kindle]
From this output, it appears to me that I should be able to read and write to the file system.
Unfortunately, when I try to copy any files to the Kindle, I cannot do it as a regular user. I do have pmount setup on my machine, so I'm not sure if that is causing the problem...haven't been able to find any additional information about it in regards to this kind of issue.
Anybody have any suggestions of what I may be missing here? Thank you.
Updated per Question in Comments
jascav@home:~$ id
uid=1000(jascav) gid=1000(jascav) groups=1000(jascav),4(adm),7(lp),24(cdrom),27(sudo),29(audio),30(dip),46(plugdev),104(fuse),108(lpadmin),109(sambashare),1001(power)
jascav@home:~$ ls -ld /media/usb0/
drwxr-xr-x 7 root root 8192 Dec 31 1969 /media/usb0/
Updated per Additional Discussion
I am using usbmount for my automounting solution. I use pmount so a normal user can mount the device. It appears (after further investigation) that these applications aren't working together. usbmount is doing the automounting, but it is not doing it from the user's perspective. If I pumount the device and then mount it again manually, I can write to the device.
Getting closer, but I'm still not sure how to get usbmount to honor the user. (Maybe I can't?)
|
Figured it out (thanks to everybody who helped jog the brain a little bit).
Because usbmount is doing the automounting, this is where the problem lay. And, conveniently enough, usbmount provides a configuration file for managing how a drive gets mounted. In order to manage this, open /etc/usbmount/usbmount.conf.
There is a line in the file that looks like this:
FS_MOUNTOPTIONS=""
Add the uid and/or the gid that you would like the device to mount as.
FS_MOUNTOPTIONS="uid=1000,gid=1000"
Now, my drives automount correctly every single time.
| Device is Mounting as Read Only (Can Copy Files as Root) |
1,456,433,541,000 |
I was doing a mass recursive change of permissions of some files that I had migrated to a unix system. I changed them to ug+rw, but then I found that I could not traverse subdirectories. I looked at the man page for chmod and didn't see any explanation for excluding directories, so I googled a little and found that people used find to recursively change the permissions on directories to 'execute' for user and group. I did that and then I could look into them.
But it seemed to me that I should be able to be able to do this find chmod -- to recursively change the files to read/write but not make the directories untraversable.
Have I done this the 'right' way or is there a simpler way to do it?
|
The better solution should be
chmod -R ug=rwX,o=rX /path
where the capital X means: set execute bit if
the file is a directory or already has execute permission for some user
(quoted from chmod man page).
Or also, if you want to use find
find /path \( -type f -exec chmod ug=rw,o=r {} + \) -o \
\( -type d -exec chmod ug=rwx,o=rx {} + \)
| recursively change file permission but not directories? |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.