[SOLVED] permission issues with alsactl in bash script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a script that stores the audio volumes with
Code:
alsactl --file ~/bin/shabbat/data/asound.state store
This script works well on the laptop as a normal user but fails on the desktop as a normal user. Not sure if I were to run it as root, see below.
There is some kind of permissions issue that I have not been able to find.
The same paths and file structures exist on both machines.
Code:
rbees@TVBee:~$ alsactl --file ~/bin/shabbat/data/asound.state store
-bash: alsactl: command not found
so I
Code:
rbees@TVBee:~$ su
Password:
root@TVBee:/home/rbees# alsactl --file ~/bin/shabbat/data/asound.state store
X11 connection rejected because of wrong authentication.
xcb_connection_has_error() returned true
XDG_RUNTIME_DIR (/run/user/1000) is not owned by us (uid 0), but by uid 1000! (This could e g happen if you try to connect to a non-root PulseAudio as a root user, over the native protocol. Don't do that.)
alsactl: save_state:1626: Cannot open /root/bin/shabbat/data/asound.state for writing: No such file or directory
opps, wrong path, so I fixed that and
Code:
root@TVBee:/home/rbees# alsactl --file home/rbees/bin/shabbat/data/asound.state store
X11 connection rejected because of wrong authentication.
xcb_connection_has_error() returned true
XDG_RUNTIME_DIR (/run/user/1000) is not owned by us (uid 0), but by uid 1000! (This could e g happen if you try to connect to a non-root PulseAudio as a root user, over the native protocol. Don't do that.)
alsactl: save_state:1626: Cannot open home/rbees/bin/shabbat/data/asound.state for writing: No such file or directory
but the file is there.
Code:
root@TVBee:/home/rbees# cd /home/rbees/bin/shabbat/data
root@TVBee:/home/rbees/bin/shabbat/data# ls
.........
asound.state
.........
Needless to say i really don't understand what is NOT happening.
I have goggled for the correct permissions for alsactl but so far no joy.
rbees@TVBee:~$ alsactl --file ~/bin/shabbat/data/asound.state store
-bash: alsactl: command not found
The above indicates that on this machine alsactl does not exist or cannot be found. So either it is not installed or the user executing the call does not have the path to the executable.
Have you tried to see the output of the following:
Code:
which alsactl
Obviously this needs to be run on the machine with the above error (desktop I believe)
You should not store data files in /bin. That directory is for executable programs. Normally, you should keep your data files under your home directory so that you can find them easily in programs or to back up.
ondoho:
which distro? Debian Testing (Stretch), updated weekly on both machines.
which version of alsa? 1.0.27+1
why is this posted in non-nix??? Because it is a program script issue !!!
grail:
as normal user on the desktop
Code:
rbees@TVBee:~$ which alsactl
You have new mail in /var/mail/rbees
rbees@TVBee:~$
and as root
Code:
root@TVBee:/home/rbees# which alsactl
/usr/sbin/alsactl
root@TVBee:/home/rbees#
and as a normal user on the laptop
Code:
kingbee@BumbleBee:~$ which alsactl
/usr/sbin/alsactl
kingbee@BumbleBee:~$
The desktop I am certain is pristine. The laptop on the other hand has been upgraded through at least 2 versions of testing and maybe three. I am certain that I have done something to the laptop that gave my account more privilege than normal. I just don't know what it was and it would have been a long time ago.
And yes this is the same script that you have been kind enough to help me through.
smallpond: Check the paths I provided again. /home/user/bin/shabbat/data...
keefaz: good thought but both pulseaudio and alsa are installed on both
So following on the notion that I changed something on the laptop I checked the executable and on the laptop /usr/sbin/alsactl is -rwxr-xr-x (0755) Owner:root/root. And on the destop /usr/sbin/alsactl is -rwxr-xr-x (0755) Owner:root/root. So I did not change that. grrr.
Clearly something is different but I have not found it yet. Any other ideas?
So just to be sure that the changed permissions were properly applied I rebooted the desktop.
I still get this
Code:
rbees@TVBee:~$ which alsactl
rbees@TVBee:~$ alsactl --file ~/bin/shabbat/data/asound.state store
-bash: alsactl: command not found
rbees@TVBee:~$ sudo alsactl --file /home/rbees/bin/shabbat/data/asound.state store
[sudo] password for rbees:
X11 connection rejected because of wrong authentication.
xcb_connection_has_error() returned true
rbees@TVBee:~$
What I don't understand is why X11 is even being polled in this case. This is completely a command line thing. The above is via an ssh login. When logged in locally I don't get the X11 error, but it returns nothing either.
It does work locally with sudo after the reboot. But I had logged out and back in before just not a reboot. But it still does not work as a normal user even though that user is a member of the adm group which I thought gave the user certain elevated privileges.
Perhaps I am not understanding the groups correctly.
So after looking at the user permissions I think I may have found it. At one time I was trying to set up my laptop to dialout through my cell to my landline to get a low speed Internet connection as we did not have smart phones at the time. It so happens that the dialout group has root and the local user as members.
Not sure why root would need to be a member of any group but...
Would that allow the local user to execute some privileged programs?
Ok, so the issue for rbees user is that /usr/sbin is not in their path and hence the call to which returns nothing and the command errors when called as it cannot be found.
I think the issue for it being run as root is that you already have alsa running in the current x11 environment which is not associated with root but the user rbees and hence when called as root
it is trying to connect to an environment as root, which is not there (this may be wrong so don't quote me )
Try using the full path to the executable and see if that works for rbees user:
Code:
/usr/sbin/alsactl --file ~/bin/shabbat/data/asound.state store
On a side note, whilst the bin directory you are using is local to the user in their home directory, it is still not the correct place to store a data file in the linux way of thinking as bin directories
anywhere on the machine are meant to be used for executable files.
Now that you mention it I do remember that I did edit ~/.profile on the laptop and add /usr/sbin to the path for my user account.
So now I need to decide what is the best way to take care of this issue on a larger scale.
1. I could just make a simple readme containing the system changes that need to be made manually so the script will work correctly. And I will have to to some extent because root will be needed to make the changes to the user account and I am not willing to have the script run as root.
2. I could add the path to the dozen or so small scripts that have the volume control feature. Not sure I am interested in the work involved and I would surely miss one or two files that need the edit. Or perhaps edit the .profile with the $PATH changes. But then the user has access to a bunch of executables that they probably should not have access to. In both of these cases the path is then hard coded, not sure I like that.
3. I could write the volume control line so that it dynamically adds the correct path at runtime. Still have to edit a bunch of files that way but at least the script is more cross platform.
4. I could try (and fail) to set it up to take care of it at install time.
So attempting option 3 I am running into an issue. Big surprise no? Anyway.
I have edited one of the files to contain a dynamic call to alsactl
Code:
#!/bin/bash
fan=$(which alsactl)
STORDIR="${HOME}/bin/shabbat/data"
"$fan --file $STORDIR/asound.state store"
killall -9 mpg123
sleep 10
amixer set Master 70%
mpg123 $STORDIR/bin/shabbat/music/Shabbas.mp3
espeak -s115 "Shabbat is comming,
Shabbat is comming,
Shabbat is comming and it is on it's way,
It will be here in less than 6 days."
sleep 3
"$fan --file $STORDIR/asound.state restore"
sleep 20
~/bin/shabbat/radio
But I keep getting "no such file or directory" on both alsactl lines. Yet if I cat that file at that location it comes up.
I have been doing my test runs with something like
Code:
rbees@TVBee:~$ bin/shabbat/data/havdalah
Which all seams to boil down to, once again, not having /usr/sbin in the users path, which I am trying to avoid so the call to "which alsactl" is failing.
What is the least invasive way to the system to work around this issue? Yet be simple enough that most people would be able to set it up? Even if they don't have root/admin on the machine?
Perhaps a different way of saving the current volume and restoring it is needed. Any idea how that may be done simply?
As you saw on the previous call to 'which' on the desktop, it returns nothing so your script would of course still fail. The simplest method is just to write the full path at the top and use
as required. The esy why to find all your files to be edited is just use a simple grep recursively, then you could simply feed that into a loop and do a sed to make the changes required.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.