Permission with tar command - linux

I have some problem with linux "tar" command.I want to unzip a config001.gz to a directory(with owner root ,group root and 777 permission as show in pic).
My origin dir and target dir is shown in pic.
My question is:
why there is en empty folder in a config001.gz file(I try to tar two file together and look in the the config002.tgz file ,there is no empty dir)?
if I don't append --no-overwrite-dir after command,it will raise error, what permission does "tar" want to change?
why --no-overwrite-dir option can fix the problem
my mount infomation:
fuse.mfs rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other 0 0

why there is en empty folder in a config001.tgz file?
There is the "root folder" included in the tar archive. The folder the tar was in. The privileges, owner and group, permissions and creation/modification (or one of them, I am not sure) dates are included. You can create such archive with:
mkdir -p /tmp/a
cd /tmp/a
echo 123 > 1
echo 234 > 2
tar cfvp /tmp/test.tar .
# and inspect with:
tar -tvf ./test.tar
drwxr-xr-x kamil/kamil 0 2019-07-15 12:50 ./
-rw-r--r-- kamil/kamil 4 2019-07-15 12:50 ./2
-rw-r--r-- kamil/kamil 4 2019-07-15 12:50 ./1
By specifing the ., ie. the current directory, the information about the current directory itself will be included in he tar. Ie. the information about the owner and group, permissions and dates.
if I don't append --no-overwrite-dir after command,it will raise error, what permission does "tar" want to change?
tar wants to change the permissions of the directory you are inside. The hpc_dir directory. The hpc_dir is owned by root, so tar can't change/touch it.
why --no-overwrite-dir option can fix the problem?
Because then tar sees the the currect directory hpc_dir exists. Because of that, tar doesn't try to create the directory, nor tries to change the owner and group permissions of the directory, nor tries to restore the creation date of the directory.
You could just go with mkdir somedir; tar xzfv archive.tar -C somedir - that way the somedir will be created by current user, so tar will be able to change it's properties.
Or you could just change the owner of hpc_dir directory, letting your user modify it.

Related

Restore bash script

I have a backup directory with a tar file in that directory (/home/username/userhome/backup/userbackup-${currentdate}.tar.gz).
I would like a create a script that:
creates the restore directory if it does not exist
displays the contents of the backup directory containing tar files of previous backups
asks the user to enter the name of the tar file to restore
uses the tar command to restore the file to the new restore directory and log file
So far my script has
#!/bin/bash
mkdir restore -p
ls /home/username/userhome/backup
echo "Enter the file name to be restored"
read $filename
tar xvf "/home/username/userhome/restore/$filename" &>/home/username/userhome/restore/restore.log
I am a complete newbie so any help will be greatly appreciated.
Continuing from the comment, one thing you always want to do when writing a script is to validate each step along the way. For instance, if you cannot create the new restore directory, you don't want to loop and then attempt to extract a tar.gz file to the directory that doesn't exits.
Virtually all commands return 0 on success or a nonzero error on failure. You can use this to your advantage to check if the command succeeded and if it didn't you simply exit. A quick way to do that is:
command || exit 1
In your case creating the restore directory that could be:
mkdir -p restore || exit 1
You can add additional error messages if you like, but most time the error generated by the failure will be sufficient to tell you what went wrong.
Whenever you are operating on a fixed base directory using subdirectories of that base, it is best to create a variable for that base directory you can use in your script. For example:
#!/bin/bash
userdir="${1:-/home/username/userhome}"
budir="$userdir"/backup
restore="$userdir"/restore
Here userdir is the base directory and you have additional variables for the backup directory and restore directory. This makes it convenient to reference or operate on files in any of the directories. Note also how userdir can be set from the first command line argument or uses /home/username/userhome by default if no argument is given.
You can create restore and change to that directory, validating each step as follows:
mkdir -p restore || exit 1
cd "$restore" || exit 1
For the menu, let select create the menu for you (now if you have hundreds of .tar.gz files, you may need to write a custom pager, but for a few dozen files, select will be fine). You can generate the menu and restore the selected file with:
select choice in "$budir"/*.tar.gz; do
tar -xvf "$choice" &>"$restore/restore.log"
break
done
Putting it altogether, you would have:
#!/bin/bash
userdir="${1:-/home/username/userhome}"
budir="$userdir"/backup
restore="$userdir"/restore
mkdir -p restore || exit 1
cd "$restore" || exit 1
select choice in "$budir"/*.tar.gz; do
tar -xvf "$choice" &>"$restore/restore.log"
break
done
Example Use/Output
Say I have a couple of .tar.gz files in a directory, e.g.
$ tree /home/david/tmpd/backup
backup
├── v.tar.gz
└── x.tar.gz
Then to create a restore directory under the tmpd directory I can run the script as:
$ bash ~/scr/tmp/restore.sh /home/david/tmpd
1) /home/david/tmpd/backup/v.tar.gz
2) /home/david/tmpd/backup/x.tar.gz
#? 2
By choosing 2 above the x.tar.gz file is restored under the restore directory, e.g.
$ ls -al restore/
total 4
drwxr-xr-x 3 david david 80 Oct 29 01:00 .
drwxr-xr-x 4 david david 80 Oct 29 01:00 ..
drwxr-xr-x 3 david david 60 Oct 29 01:00 home
-rw-r--r-- 1 david david 57 Oct 29 01:00 restore.log
So the restore directory was successfully created, the restore.log was created and the .tar.gz file was restored under the restore directory.
The contents of restore.log are
$ cat restore/restore.log
home/david/scr/utl/xscrd.sh
home/david/scr/utl/xzdate.sh
(which were the two sample files I added to the x.tar.gz file)
Look things over and let me know if you have further questions.

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

why vim can overwrite other user file under its home directory

For example, root touch a new file under a common user's(name it bob) home directory:
/home/bob $ ls -alh a.txt
-rw-r--r-- 1 root root 0 Jul 16 17:45 a.txt
Now user bob open it with vim, and it should be readonly.
I tried to force overwrite it with :w!, and it was saved and the owner:group changed to bob:
/home/bob $ ls -alh a.txt
-rw-r--r-- 1 bob bob 4 Jul 16 17:47 a.txt
IMO, I think it can't be saved with permission denied, but it could, and the owner:group also changed.
And this can only under bob's home directory, if under outer directory, such as /tmp or others, it can't be written as I thought.
Can anyone explain this? what processes does :w! actual do? thx.
The file is readable by Bob, so Bob can open it in Vim.
The directory is writable by Bob, so Bob will be able to unlink (remove) any file therein, and write any new file to it. This is what happens when you use :w!.
This is not true for the /tmp directory, because it probably has the "sticky" bit set.
From the OS X sticky(8) manual:
A directory whose 'sticky bit' is set becomes an append-only directory, or, more accurately, a directory in which the deletion of files
is restricted. A file in a sticky directory may only be removed or
renamed by a user if the user has write permission for the directory
and the user is the owner of the file, the owner of the directory, or
the super-user. This feature is usefully applied to directories such
as /tmp which must be publicly writable but should deny users the
license to arbitrarily delete or rename each others' files.

permission denied in a folder for a user after chown and chmod

I have a directory at
/home/ec2-user/vertica1
and I'm trying to get user dbadmin all privilages in that folder.
I've done chown to dbadmin and chmod 777 on that folder but dbadmin still gets a permission denied error.
If I put sudo in front of the command(I put dbadmi in sudoers), then it works. Why can't I get it to work without sudo?
Can dbadmin traverse /home/ec2-user? Try doing chmod a+x /home/ec2-user
There could be more reasons for being denied, like a specific acl or a LSM but this is the most likely cause.
UNIX permissions on directories
The UNIX permissions rwx¹ work on directories as follows:
r: You can view the contents of the directory (the names of the files or folders inside)
w: You can create new files, delete or rename existing files.
x: You can traverse the folder.
The traverse permission means that you can access the folder children (assuming you know its name -which you can obtain if you also have read permission-).
In this case dbadmin could read and traverse / as well as /home, but /home/ec2-user probably had a mode like drwx------ 2 ec2-user in order to protect its contents. Thus, even if you had an important file readable by anyone deep inside your home folder, other users can't get into it, since they wouldn't be able to go pass /home/ec2-user (which is exactly what you wanted to do, in this case).
¹ Note that I am skipping over the more exotic ones.
what is the result of ls -la for this dir and also parent dir? Maybe the directory doesn't have read permissions for your user.
sudo chmod ug+r vertica1
Also ec2-user directory should be writable by the user dbadmin.

mkdir: cannot create directory `pgsql': Permission denied

I want to create directory like below:
ajs#ajs-HP-Compaq-dc5800-Small-Form-Factor:/usr/local$ mkdir pgsql
mkdir: cannot create directory `pgsql': Permission denied
But I am getting error:
Permission denied
How can I resolve and create directory pgsql in this location /usr/local$
Kindly suggest me, hope for reply.
Thanks
You have to check your user name to have permission for creating directory in the folder /usr/local$
Check your permission for the folder by the command
ls -ltr /usr
Link to refer about file permissions.
You are getting a Permission denied error because you do not have access rights to create a directory in /usr/local. You can determine the access rights for these directories by using the stat command. The output will look something like this.
$> stat -c '%n %A %G %U' /usr /usr/local
/usr drwxr-xr-x root root
/usr/local drwxr-xr-x root root
Now double check who you are. You can use the whoami command or the id command invoked below twice to reveal both username and group.
$> id -un; id -gn
In the stat output, root:root owns both /usr and /usr/local and only the owner may create (write) new directories based on the access rights. In order to create the directories, I'd recommend either becoming root or trying the command with sudo. If this is not possible, I'm afraid you'll have to create the directory elsewhere or contact the administrator of that machine.
You probably have to be root to do such things in /usr/local.

Resources