How to run a sudo command on startup? - linux

I'm trying to connect to my vpn on startup. I normally enter protonvpn c -f into command line.
I have tried the method of creating an rc.local file however it didn't work.
This is the code I have inside of it (I got this from a post about a similar issue):
#!/bin/sh -e
/usr/local/bin/protonvpn c CH-NL#1
exit 0
I also made the file executable with chmod +x.
Im running Kali 2020.1 if that helps.
How do I fix this?

create a file ==> /etc/rc.local
Open and Edit rc.local like below,
replace your command with apt-get update below
#!/bin/sh -e
apt-get update
exit 0
save the edit with Crtl+X
after that
sudo chmod +x /etc/rc.local
then do a reboot and check
It should 100% work like a charm

This solution should work for ubuntu systems.
https://askubuntu.com/a/290107/1051584
Kali is debian based so I believe this will probably work as well.

Related

Command without sudo works but requires sudo

I have already solved this problem but I do not know why this solves the problem and I don't like not knowing what went wrong. I'm using the terminal on Ubuntu
Here was the issue...
If I run $ ngm -args it runs but fails because it requires sudo
If I run $ sudo ngm -args I get the error like ngm not found
If I run $ sudo /usr/local/lib/ngm -args it runs with sudo and works perfectly.
I don't understand why the 3 works and 2 doesn't work.
When trying to run $ ngm -args, the shell will look for the executable in it's $PATH variable, and it finds it. When trying to run the executable it finds that it has to be sudo and exits.
When you run $ sudo ngm -args, the shell will look for the executable in the $PATH environment of the Root user, and it can't find it.
When running it like the last option, the shell doesn't need to look in the $PATH of the root user, because it finds it in the path that you specified /usr/local/lib/ngm, so both issues are gone.
This might be a starting point in order to understand the PATH

Ubuntu login loop and bad interpreter everytime i call sudo

I was trying to install wireshark when I accidentally had to restart my computer. Now I am stucked in a infinite login loop in Ubuntu. I tried to do something from the bash but every time I try to run a sudo command, as sudo su it gets:
-bash: /usr/lib/command-not-found: /usr/non/python3: bad interpreter: permission denied.
I tried to change the permission to that but it does not work.
EDIT:
i solved by entering in recovery mode as root user and sudo chmod 777 /usr/lib/command-not-found and even sudo chmod 777 /usr/bin/python3. I tried everything possible, and i dont even know why it worked.
Try this
sudo dpkg -i python2.7-miminal*.deb
sudo dpkg -i python2.7*.deb
I ran into the same problem when I have added export $PATH="~/bin" as last line in ~/.bash_profile.
I solved it by removing the line after entering into tty2 command line using "ctrl + alt + F3" and all commands like "ls, cd, nano" where available as "/bin/ls, /bin/cd, /bin/nano".
Using command "/bin/nano ~/.bash_profile" I changed the file content and now it's working normally.

linux deploy, kali, start an app service after its boots up

Hi there does anybody knows how to make an apache2 service to run at startup?
After Kali linux boots up,
I have tried editing rc.local, rc.init and rc file.
Adding
service apache start
command I have also made bash script, giving its permissions to run it with chmod +x b.sh - placed in bin folder for ability to run in anywhere in command line.
I am able to run it in terminal, it does what I need. But can't get it running on start up.
Also I tried:
update-rc.d mystartup.sh defaults 100
still no success. Any ideas?
Thanks.
Place your script inside init /etc/init.d/ , then place a link to it inside /etc/rc5.d/ (be sure to get your accurate run-level)...
Start the second link with the string S71 like S71apastart
I.e do the following:
Place the text:
#!/bin/bash
sudo service apache start
inside /etc/init.d/apaStart (a new file created by you)
Then
sudo chmod +x /etc/init.d/apaStart
sudo ln -s /etc/init.d/tomcat /etc/rc5.d/S71apaStart (change rc5.d to rc3.d if needed depending on your runlevel)
I won't suggest this method but it'll work with your situation.

cgMiner Auto-Start on Raspbian

I am trying to make cgMiner auto-start when my Raspberry Pi (Raspbian Linux) starts.
Edited the rc.local file:
sudo nano /etc/rc.local
and added this line:
nohup ./cgminer-3.1.1/cgminer --config /home/pi/cgminer.conf -S /dev/ttyUSB0 -S /dev/ttyUSB1 >/dev/null 2>&1&
and cgMiner doesn't start. If I type in terminal the exact same line with sudo in front it works perfectly.
sudo nohup ./cgminer-3.1.1/cgminer --config /home/pi/cgminer.conf -S /dev/ttyUSB0 -S /dev/ttyUSB1 >/dev/null 2>&1&
What can I do?
I think it is related to the path or better the current working directory.
You are using ./cgminer and not a full path. So either use the full path or first cd to the directory containing the cgminer program.
Also have a look at the following page over at adafruit doing the exact same thing you are trying to accomplish
http://learn.adafruit.com/piminer-raspberry-pi-bitcoin-miner/configure-auto-start
Adafruit has the following code in /etc/rc.local
cd /home/pi/PiMiner
python PiMiner.py &
cd ..
nohup ./cgminer-3.1.1/cgminer --config /home/pi/cgminer.conf -S /dev/ttyUSB0 -S /dev/ttyUSB1 >/dev/null 2>&1&
The first and third line (the cd commands) ensure that the folder containing the folder containing the cgminer command is the current directory.
From the two cd commands adding the following command before the line containing the cgminer command would solve your issue
cd /home/pi
I seem to have found the solution for this problem, assuming it is the same as for bfgminer.
After spending a full day playing with init.d scripts, I found the simplest way is to make sure your cgminier.conf file has all the arguments in then add the following to the end of /etc/rc.local
cd /home/YOURNAME/bfgminer
sudo ./bfgminer
It ran without the sudo part, but didn't start up my block erupter without it.
I also managed to get it running from init.d, but had issues with it preventing system rebooting when done that way.
Hope that helps

Running a file as nonroot from a root bash script

Okay, I currently use an eggdrop IRC bot on my server. I want to make a bash script to start it up as well as a few other scripts at the same time. All that's left is to make it start, but it won't run as root.
However, I also need to be in the current directory of the file to run it, or it displays an error.
For example:
/home/eggdrop/eggdropbot/eggdrop will display an error, so to run it I need to
cd /home/eggdrop/eggdropbot and then ./eggdrop
So I can't just use "sudo -u eggdrop /home/eggdrop/eggdropbot/eggdrop" and as you probably know, sudo won't cd, either, since that would be pointless.
Any suggestions?
Why not just cd first and then sudo -u ./eggdrop .?
What about doing the cd, and, only then, launch the command with sudo ?
I suppose something like this should do the trick :
cd /home/eggdrop/eggdropbot && sudo -u eggdrop ./eggdrop
You can cd to the directory as the root user and then use sudo -u to invoke the program from the working directory.

Resources