Why copied file has different permissions in Linux? - linux

I am logged in as root in Linux. I have a file with 777 permissions. I copied the file in the same directory with cp.
cp settings.php settings_copy.php
However, the copied file has different file permissions.
[root#localhost default]# ls -l setting*
-rwxr-xr-x. 1 root root 29105 Apr 26 11:48 settings_copy.php
-rwxrwxrwx. 1 root root 29105 Apr 26 09:48 settings.php
Is this normal? How can I ensure that the copied file gets the same permissions? I believe that it is the default behaviour for the copy command in any OS.

Use the -p option to preserve the permissions:
cp -p settings.php settings_copy.php
When you copy a file, you are creating a new file. So, its (new file) permissions depends on the current file creation mask, which you change via umask command. Read man umask for more information.

have you looked at man cp
This is the relevant section:
-p same as --preserve=mode,ownership,timestamps
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
So to keep the same ownership and mode you would run the command:
cp --preserve=mode,ownership
If you know that's always what you want and don't want to remember it, you can add it as an alias to your .bashrc;
alias cp='cp --preserve=mode,ownership'

Related

Unable to change directory or file permission [chmod question]

I am trying to change the permission of a file to 444 (read-only).
This directory resides in a NTFS drive. All files in this drive seem to be owned by root.
This is the present permissions for the directory.
drwxrwxrwx 1 root root 0 Jul 23 11:41 xxx_directory
I've tried sudo bash and then execute chmod 444 xxx_directory to no avail.
The expected outcome was dr--r--r-- for xxx_directory.
Thoughts? Thank you.
The directories need at least R-X
First, create a mount point in a terminal using 'mkdir'. Then, type the following line to mount the partition with options 'permissions':
sudo mount /dev/sdXN -t ntfs-3g -o permissions [Mount point]
Example:
sudo mount /dev/sdb1 -t ntfs-3g -o permissions /media/Data/
Then, you will be able to edit the permissions of the files on the NTFS partition with 'chmod' and 'chown' !
Check thia reference LINUX - MOUNT NTFS PARTITION WITH PERMISSIONS
You should at least require the x permission for directories otherwise you won't be able to cd into it.
You can specify the permissions (via fmask and dmask) while mounting the partition ( or in fstab if an entry is there).
An example of an fstab entry :
#mounting by UUID
UUID=<partition UUID> /mount/point ntfs-3g user,uid=1000,gid=1000,dmask=0022,fmask=0033
This would give all directories in that partition drwxr-xr-x parmissions. Any file created will get .rw-r--r-- permissions. And the ownership would be by the user with uid 1000 so he can change permissions (without sudo)

change permissions on a not writable

UPDATE:
I moved my question to ask ubuntu community, but can not delete it from here... if you have an awenser, please share it on ubuntu community not here... Thanks
i want to make an change on a file but i cant do that because i have not correct permissions:
➜ ls -l pycharm64.vmoptions
-rw-r--r-- 1 root root 427 Dec 28 18:33 pycharm64.vmoptions
i tried to change permisions by these two command:
sudo chmod a+w pycharm64.vmoptions
and
sudo chown user:user pycharm64.vmoptions
but in i get an erro both time:
Read-only file system
how can i make an change on my file? (honestly i dont care about the owner and groups of the file... i just want to change my file anyway)
P.S: my OS is UBUNTU
You can change a file on read only by setting the "immutable property"
chattr +i [fileName]
If you want to revert it just change the "+" for a "-"
chattr -i [fileName]
Your filesystem could be mounted as read only. You have to change first before you can write anything to it. Changing file permissions also requires writing on the filesystem.
You may be able to mount it as read write with command like:
sudo mount -o remount,rw /dev/foo /mount/destination/dir
In this command you spesify that you want to remount the filesystem with different options, adding the readwrite, rw capability.
If you successd in changing the filesystem to read write, then you should be able to change to file permissions with the commands you tried earlier.
You can`t edit it directly (I'm not sure about Windows).
You should edit custom settings file instead:
Manually
nano ~/.config/JetBrains/PyCharm2022.3/pycharm64.vmoptions
or from IDE -- https://intellij-support.jetbrains.com/hc/en-us/articles/206544869.

ownership of file is changing automatically

How can I prevent changing the ownership of a file?
I have a file with permission as follows:
-rw-r-----. 1 netcool ncoadmin 1689 May 8 14:54 NCI_Constellation.proj
As part of RPM package installation, I am running a script which is supposed to write data into NCI_Constellation.proj file. Whereas the permission of the file is getting changed as follows during package installation and the writing to the file is not happening.
-rw-r-----. 1 root root 1689 May 8 14:54 NCI_Constellation.proj
Is there a way to not change the ownership of NCI_Constellation.proj file and keep it as it is as follows so that I will be able to write data to the file?
-rw-r-----. 1 netcool ncoadmin 1689 May 8 14:54 NCI_Constellation.proj
Please help.
The question is: what package does that file belong to and with what permissions?
rpm -qf /path/to/NCI_Constellation.proj
will give you the package owning this file (let's say NCI.rpm). Then
rpm -qlv NCI.rpm | grep NCI_Constellation.proj
will give you the owners and rights of this file as packaged by NCI.rpm. If you are the one packaging NCI.rpm; you should put something like this in your %files section:
%files
%attr(640,netcool,ncoadmin) /path/to/NCI_Constellation.proj
By the way make sure that you really can write to the file with those permissions; test that first... Who is running the script to change this file? As which user? then run it yourself manually as that user to make sure these file permissions will suffice.
you have two options in my opinion,
first : set netcool to root group by doing this:
$ sudo usermod -a -G root netcool
with this command you user is able to change and modify the file even after the permissions changed.
second : set netcool user a second root user by changing /etc/passwd file.
for this open the file with every file-editor you want then change UID and GID to 0. after doing this if you run $ grep netcool /etc/passwd you should see :
netcool:x:0:0: {the rest may change for anybody}.
We can prevent the changing of group of file by using setgid bit on directory. So if you add user netcool to ncoadmin and give write permission to ncoadmin then you can edit the file. Here is how you can set the SetGid bit on directory.
chmod g+s your_directory_containing_file(NCI_Constellation.proj)
Bit more about the setgid on directory:
setgid can be used on directories to make sure that all files inside the directory are owned
by the group owner of the directory. The setgid bit is displayed at the same location as the x
permission for group owner. The setgid bit is represented by an s (meaning x is also there)
or a S (when there is no x for the group owner). As this example shows, even though root
does not belong to the group proj55, the files created by root in /project55 will belong to
proj55 since the setgid is set.
root#RHELv4u4:~# groupadd proj55
root#RHELv4u4:~# chown root:proj55 /project55/
root#RHELv4u4:~# chmod 2775 /project55/
root#RHELv4u4:~# touch /project55/fromroot.txt
root#RHELv4u4:~# ls -ld /project55/
drwxrwsr-x 2 root proj55 4096 Feb 7 17:45 /project55/
root#RHELv4u4:~# ls -l /project55/
total 4
-rw-r--r-- 1 root proj55 0 Feb 7 17:45 fromroot.txt

barnyard2 for snort permission denied

I installed barnyard2 for snort, but when i run command below this error appear.
[root#localhost snort]# barnyard2 -c /etc/snort/barnyard2.conf -d /var/log/snort/ -f snort.log -w /etc/snort/bylog.waldo /etc/snort/gen-msg.map /etc/snort/sid-msg.map -C /etc/snort/classification.config
Running in Continuous mode
--== Initializing Barnyard2 ==--
Initializing Input Plugins!
Initializing Output Plugins!
Parsing config file "/etc/snort/barnyard2.conf"
+[ Signature Suppress list ]+
----------------------------
+[No entry in Signature Suppress List]+
----------------------------
+[ Signature Suppress list ]+
Barnyard2 spooler: Event cache size set to [2048]
ERROR: Can not get write access to logging directory "/var/log/barnyard2". (directory doesn't exist or permissions are set incorrectly or it is not a directory at all)
Fatal Error, Quitting..
Barnyard2 exiting
and permission is:
[root#localhost snort]# ls -l /var/log/barnyard2
-rwxrwxrwx. 1 root root 0 Aug 14 16:35 /var/log/barnyard2
in this link this problem was solved but i don't understand how ...
https://forums.freebsd.org/threads/barnyard2-start-service-error.51378/
It looks like directory flag is missing there. The error message says
ERROR: Can not get write access to logging directory "/var/log/barnyard2". (directory doesn't exist or permissions are set incorrectly or it is not a directory at all)
Probably the last case of /var/log/barnyard2 being not a directory at all might apply.
Backup the file and try creating a directory /var/log/barnyard2 with permissions 640 and corresponding ownership.
EDIT: As long as you do not know the contents of /var/log/barnyard2, rename or move the file to some place ( as root 'mv /var/log/barnyard2 /var/log/barnyard2.old'). Restarting barnyard2 now could help, it might create the directory with appropriate permissions by itself. Otherwise as root type 'mkdir /var/log/barnyard2' and then set permissions by typing 'chmod 640 /var/log/barnyard2'. Additionally check the user under which barnyard2 is running by typing 'ps -u | grep "barnyard2"'. Then find the appropriate group to that user by typing 'groups <user>' and then set the ownership of the directory to the corresponding user by typing 'chown <user>:<group> /var/log/barnyard2'.
'/var/log/barnyard2' should be the log directory. In your case it is a file. So, delete the file and create a directory instead. Here are the steps. Enter the commands as a root user.
rm /var/log/barnyard2
mkdir /var/log/barnyard2

Permission Denied when editing .bash_profile

I'm still learning the CLI and the ins and outs of it, especially this .bash_profile. I feel overwhelmed with what I'm learning about this.
Anyways, I can access .bash_profile. If I do nano ~/.bash_profile then the file appears and I'm free to edit. And then I tried addingin the line I'm supposed to include:
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
However, when I try to save the file (or whatever .bash_profile is), I get the following error: [ Error writing /home/myname.bash_profile Permission denied ]
While you can do sudo nano ~/.bash_profile to edit the file, I feel like it's a bit weird that your .bash_profile needs root to be modified.
If you try ls -la ~ | grep bash
-rw------- 1 Greg staff 8622 27 Jun 16:06 .bash_history
-rw-r--r-- 1 Greg staff 2189 28 Jun 01:24 .bash_profile
You see my .bash_profile is owned by me, Greg, not root. I think you'll find that when you do this, your .bash_profile will be owned by root. This means that when you want to edit the file, you need to use sudo, but I don't.
If you don't want to have to use sudo all the time to modify this file, you can change the owner of the file sudo chown yourusername ~/.bash_profile
Now if you run the ls command I showed above, you should see your name as the owner of the file, instead of root.
You need root permissions to edit.
Edit it with sudo or login as root and edit.
Thanks,
Sandeep.

Resources