Mountain Lion, export $PATH to .bash_profile, permission denied? - osx-mountain-lion

I can do this:
sudo nano .bash_profile
But when I do this:
sudo echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
I get this error message:
-bash: /Users/mycomputer/.bash_profile: Permission denied
When I do ls -al:
-rw-r--r-- 1 root staff 27 10 Aug 12:22 .bash_profile

Quick fix: do "sudo bash", to actually assume root privileges, THEN do the echo. It will work. sudo echo still uses your real uid, so it fails.

Related

Reset /usr/local/bin ownership as rootless on macos ventura

I set /usr/local/bin directory using the sudo chown -R root /usr/local/bin for the permission on the current user session.
Before I set this permission, I was getting the following output when I used the ls -ld /usr/local/bin command:
$ myuser> ls -ld /usr/local
/usr/local/bin
Now I get the following output with the above command:
$ myuser> ls -ld /usr/local/bin
drwxr-xr-x# 16 root wheel 512 Feb 16 00:03 /usr/local/bin
What can I do to undo the change?
You can run :
sudo chown -R $(id -un):$(id -gn) /usr/local/bin
or even better :
sudo chown -R $(id -un):$(id -gn) /usr/local/*
in case you have other directories in /usr/local which belong to root.

Permission denied editing .bash_profile rbenv init on mac

Hung up on editing the .bash_profile while running rbenv init on mac.
$ rbenv init
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
but get the message:
-bash: /Users/macbookpro/.bash_profile: Permission denied
I try to edit the .bash_profile directly but get the message:
I found this command to see the users:
$ ls -la ~ | grep bash
It shows:
-rw------- 1 macbookpro .bash_history
-rw-r--r-- 1 root .bash_profile
If I open the file using command:
$ sudo nano ~/.bash_profile
I guess I need to add this to .bash_profile:
$ export PATH="$HOME/.rbenv/bin:$PATH"
Or is it just the inner "part" of this?
Also how do I save this or do we have any easier solution (pretending my name is Fname-Lname)?
You'll need to use sudo in order to edit/append ~/.bash_profile on macOS.
sudo echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
Or just as you had done at the end of your question.
I was having the same issue what i did was i gave .bash_profile writable permission.
cd ~
sudo chmod 0777 .bash_profile
nano .bash_profile
After adding your modifcations you can now save the file with ^O and enter (return) after that you use ^x to exit.
Note: ^ is the control button

Unable to modify /etc/shells on macOS to include brew installed version of bash

I am trying to update my /etc/shells file to include the path to a homebrew installed version of bash which resides at /usr/local/bin/bash
$ sudo echo /usr/local/bin/bash >> /etc/shells returns Permission denied and attempting to manually update is not allowed as it appears to be read-only.
Upon inspecting the file, the permissions are set as follows:
-rw-r--r-- 1 root wheel 179 Feb 21 2017 /etc/shells
So, with this in mind, and after looking at this article about Updating you shell with Homebrew I tried to initiate a shell as the root user and then try command above, i.e:
$ sudo -s
$ echo /usr/local/bin/bash >> /etc/shells
$ chsh -s /usr/local/bin/bash
However, this seems to hang on the first command ($ sudo -s). This spawns a bash process that eats up ~ 70% CPU and nothing happens.
Is there an alternative way one can update the /etc/shells/ file?
An approach to adding to a root-only file is echo /usr/local/bin/bash | sudo tee -a /etc/shells.
– Petesh
Would you be able to explain why that works and the sudo echo /usr/local/bin/bash >> /etc/shells does not though.
The latter doesn't work because the output redirection >> is (tried to be) applied by the shell before the sudo … is executed, and of course the user shell has no permission to do that.
Or you can just use this (I had to do this on macOS Mojave):
sudo sh -c "echo $(which zsh) >> /etc/shells"
chsh -s $(which zsh)

OSX: Custom Command Line Commands Not Working on Mavericks

I'm trying to get Sublime Text 2 to work on command line. Since I upgraded to Mavericks I haven't had any luck.
$ echo $PATH
/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
$ ls -l /usr/local/bin/subl
lrwxr-xr-x 1 root wheel 62 Mar 13 13:38 /usr/local/bin/subl -> /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl
$ subl htdocs/index.html
-bash: subl: command not found

Why can't I echo contents into a new file as sudo? [duplicate]

This question already has answers here:
Bash Deployment Script Permission Problem
(2 answers)
Closed 8 years ago.
I came across this weird problem just now and I can't seem to get to the bottom of it. I'm trying to add a config file to /etc/sudoers.d/ but get permission denied. In the example below, the file "tweedle" doesn't exist and:
drwxr-xr-x 2 root root 4.0K Jan 2 18:27 sudoers.d/
So here's the command:
$ sudo echo "tweedle ALL=(ALL) ALL" > /etc/sudoers.d/tweedle
-bash: /etc/sudoers.d/tweedle: Permission denied
It doesn't even work when I break it into two commands:
$ sudo touch /etc/sudoers.d/tweedle
$ sudo echo "poodle" > /etc/sudoers.d/tweedle
When I tested it locally, same problem:
$ cd ~
$ mkdir -m 755 tweedle
$ sudo chown root:root tweedle
$ sudo echo "battle" > ~/tweedle/beetle
-bash: /home/spanky/tweedle/beetle: Permission denied
$ sudo touch tweedle/beetle
$ sudo echo "battle" > tweedle/beetle
-bash: tweedle/beetle: Permission denied
Without sudo, all is well:
$ cd ~
$ mkdir poodle
$ echo "noodle" > poodle/bottle
$ cat poodle/bottle
noodle
Thoughts?
The echo command is being run as root, but the redirection is done by your shell, so it's executed as the current user, not as root.
The simplest solution is to invoke a root shell to run both the command and the redirection.
Rather than:
sudo echo line > file
try this:
sudo sh -c 'echo line > file'
or
sudo bash -c 'echo line > file'
The answer is to use "tee" with a pipe, a command I wasn't familiar with, so you can use sudo for the second half:
$ echo "tweedle ALL=(ALL) ALL" | sudo tee /etc/sudoers.d/tweedle

Resources