Permission Denied when editing .bash_profile - linux

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.

Related

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.

Edit file with shell script that is in the same group but not the caller user

I would like to edit a file with a shell script which is in the same group but not the "caller" user.
-rwsr-xr-x 1 root root 4896 Oct 21 00:59 /usr/bin/luxus
-rw-rw-r-- 1 root root 4096 Oct 20 23:01 <path>/file1
lrwxrwxrwx 1 root root 0 Oct 20 23:00 <path>/dir1
/usr/bin/luxus: the shell script
file1: the file to edit
dir1: file1's parent directory (symlink)
In the shell script, this is the line where the permission issue is:
echo "string" > /usr/bin/tee <path>/file1
Output:
/usr/bin/tee: <path>/file1: Permission denied
I am trying to create an AUR package for the shell script. The latter is meant to be install on the system from a PKGBUILD. In vain, I attempted to give enough permissions to the script to edit the targeted file.
This is a PKGBUILD's sample:
install -Dm755 src/${pkgname} "${pkgdir}/usr/bin/${pkgname}"
chown root.root "${pkgdir}/usr/bin/${pkgname}"
chmod 4755 "${pkgdir}/usr/bin/${pkgname}"
After investigating, I think it is quite simply impossible because of security or because of the parent dir's rights.
Nevertheless I am asking you for a solution.
As you said "you can't do anything".
The following post quite simply explains why it won't work:
https://unix.stackexchange.com/questions/364/allow-setuid-on-shell-scripts

Why copied file has different permissions in 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'

Is it possible that when using sudo command it should first source a file?

I need to run a bunch of scripts (with sudo) that use a single file.sh as a configuration file for all. Initially I've put the file.sh in /etc/profile.d and when I ran the scripts as root everything was ok (because when I connected to the machine it first sourced the file.sh and all vars in that file were available) but now, for security reasons, I need to run them with another user with sudo rights.
When running with sudo the "configuration file" in /etc/profile.d does not get sourced even if I'm root and do sudo - it's the same.
Using "sudo -E" is not an option, also this kind of solution "Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"" does not work for me as the vars in the file change a lot and it's easier to throw a file, with all the vars, in a location - like /etc/profile.d/ - instead to adding options to /etc/sudoers.
Later Edit (working):
Moved original sudo command to sudo.orig. Created a new sudo bash script
[root#NS1 bin]# cat sudo
#!/bin/bash
source /etc/profile.d/set_env_vmdeploy.sh
sh /usr/bin/sudo.orig "$#"
and gave it permissions
[root#NS1 bin]# chmod 4111 sudo
[root#NS1 bin]# ll sudo*
---s--x--x 1 root root 78 May 7 13:42 sudo
---s--x--x 1 root root 123832 Jul 31 2014 sudo.orig
If you want sudo to execute all the profile scripts in the child shell, you can tell it to invoke the shell as a login shell: sudo -i /usr/local/bin/my_script.sh. (Note that the child shell will start with the working directory set to /root, and also that this may have other unintended side effects.)
Alternatively, invoke bash explicitly with a command parameter: sudo /bin/bash -c "source ./config.sh; ./real_script.sh".

/etc/apache2/sites-available/000-default.conf Permission Denied

/etc/apache2/sites-available/000-default.conf
What should I do to access this file
I want to change DocumentRoot From /var/www/html To /var/www
First check to see who owns the file:
ls -l /etc/apache2/sites-available/000-default.conf
It should probably be something like www-data. For the rest of this answer I assume the user is www-data, replace it with the correct user on your system.
Verify you have permissions to act as that user, e.g.:
sudo -u www-data id
This command just runs the id command as the www-data user, you may have to enter a password.
Edit the file as the owner of the file:
sudo -u www-data vi /etc/apache2/sites-available/000-default.conf
(or emacs or nano or whatever your favorite editor is).
If none of that works, the file permissions might be messed up, for example maybe the user of the file is not allowed to write to it. In that case posting the output of:
ls -l /etc/apache2/sites-available/000-default.conf
may be helpful in resolving your issue, but you probably can't go wrong by doing a:
sudo -u www-data chmod o+rw /etc/apache2/sites-available/000-default.conf
This grants the owner read and write permissions on the file, then try the edit again.
If none of that works please post the exact command you are running, and the exact error message.
You need to access this file as a root user. Issue the command
sudo -s
to get a root shell, then edit that file with the editor of your choice.
Just run this command and you can edit that file
gedit admin:///etc/apache2/sites-available/000-default.conf

Resources