Shell Script Need to run as a root user - linux

I have shell scripts like below for changing/switch to another ISP connection.
#!/bin/bash
/sbin/route add default gw 192.168.1
/sbin/route del default gw 192.168.1.2
/sbin/route del default gw 192.168.1.3
/sbin/route -n
I have root access to my Ubuntu machine but I need to run the above shell script as a normal user. How can I do that?
NOTE:
Case-1: Our local machine login to LDAP server, so I can't add my Linux username to sudoers/visudo.
Case-2: I have already move that script to /bin directory and added the SUID special permission to my script. But the normal user can't run this script.

I was fixed the issue. Fix method is follows:
Added SUID permission to the route command and added execute permission to that shell script and moved file to /bin directory.Now you can run the script as command.

Related

ssh sudo to a different user execute commands on remote Linux server

We have a password less authentication between the server for root user, I am trying to run the alias on remote server as below
#ssh remoteserver runuser -l wasadmin wasstart
But it is not working. Any suggestions or any other method to achieve it
Based on your comments as you need to sudo to wasadmin in order to run wasadmin, you can try this:
ssh remoteserver 'echo /path/to/wasadmin wasstart | sudo su - wasadmin'
For add an alias in linux you must run
alias youcommandname=‘command’
Notice:
This will work until you close or exit from current shell . To fix this issue just add this to you .bash_profile and run source .bash_profile
Also your profile file name depending on which shell you using . bash , zsh ,...

In Linux, how to change user to root in perl and continue to run this script? How to export proxy in perl?

My perl scripts should be able to change the common account to root account during execution and then continue to the rest commands. But I don't know how to do:
Change to root account and able to automatically pass authentication, I used following code, but failed.
system "echo \"$password\" | sudo -S su root";
Able to continue to do the rest scriptsI found when using scripts to change account (simply try system "sudo su";) , it is not able to execute rest scripts
I need to export proxy, but when i using the following code, it does not work:
system "export http_proxy=http://proxy.companyname.com:8080";
If you have to execute other scripts to be executed with root and you have perl ssh module installed then you can open a ssh session to the localhost with root user and start executing your scripts from there.
my $ssh = Net::SSH::Perl->new("host1");
$ssh->login("root", "pass1");
$ssh->cmd("sh /opt/...");
perhaps try backticks?
my $sudo = `echo \"$password\" | sudo -S su root`;
also similar for the export.
my $exp = `export http_proxy=http://proxy.companyname.com:8080`;

connection closed in Linux server ssh

I am trying to connect to my Linux server.
After entering the password it is showing below output:
Last Logan: Mon Jun 24 12:22:48 2013 from xxx.xxx.xxx.xxx
/bin/bash: No such file or directory
Connection to xxx.xxx.x.xx closed.
How do I connect to the server?
you are trying to enter your server as a user privileges and you see /bin/bash file. When you chroot, you can reach /bin/bash directroy. And you can add your user in sudo.
Then you should see directory /home/username/bin/bash/
Edit :
When you chroot, the named directory becomes /. The correct shell path inside the chroot is then /bin/bash, not /home/username/bin/bash.
You will also need to make sure there's enough other stuff inside the chroot for the system to work. You can test this with sudo chroot /home/username /bin/bash and see what works and what doesn't
And also there is good information about chroot configuration
Your user is associated with an incorrect shell. The path to the associated shell "/bin/bash" doesn't exist on the system.
Correct your user's shell from the root or ask the administrator to do it.
Similar question: changing default shell in linux

On Linux, how can I share scripts across an SSH connection for the session only?

For work, I have to connect to dozens of Linux machines via SSH (to perform maintenance, monitor the system, install software, etc).
I have a small arsenal of scripts that help me do some of these tasks, and these are located in a folder on my Mac in /Users/me/bin. I want to be able to run these scripts on the remote Linux machine, but for several reasons I do not want these scripts permanently located on these machines (e.g., other people also connect to these remote machines, and it would be unwise to let them execute these files).
So, is possible to share scripts across an SSH connection for the lifetime of the session only?
I have a couple of ideas on how to do this, but I don't know if any of them will work. Firstly, if SSH allows file mounting, I could automatically mount me#mymac:/Users/me/bin to me#linux:/remote_bin when I connect to the remote Linux box, and set my PATH variable to "$PATH:/remote_bin". Secondly, I could set up port forwarding in the connection string (e.g., ssh me#linux -R 9999:127.0.0.1:<SMBPORT|ETC> and every time I connect mount the share and set the $PATH variable.
EDIT: I've come up with a semi-solution. On the linux machine, edit /etc/ssh/sshd_config to add the following subsystem: Subsystem shareduserbinary sudo su -l -c "/bin/mount -t cifs -o port=9999,username=me,nounix,sec=ntlmssp //127.0.0.1/exported_bin /mnt/remote_bin" && bash -l -i -s. When connecting to the remote machine, set up a reverse port forward and invoke the subsystem. E.g.: ssh -R 9999:127.0.0.1:445 -s shareduserbinary me#linux.
EDIT 2: You can make the solution above cleaner, by removing the -l from the sudo command and changing the path from /mnt/remote_bin to $HOME/rbin.
Interesting question. Perhaps you can add a command to ~/.bash_login (assuming you are using bash) to copy the scripts from a remote host (such as your mac) when you login, then add a command to ~/.bash_logout to delete the scripts when you logout. But, as bmargulies points out, it would be a good idea to go a step further and make sure that nobody else has permissions to read or execute the scripts.
You can use OpenSSH's LocalCommand to upload the files (using e.g. scp or rsync) when initiating an SSH session (see man ssh_config and this):
Host server1 server2 [...]
PermitLocalCommand yes
LocalCommand scp -q /Users/bin/me/* %h:temp_bin/
and use .bash_logout or an EXIT-trap that you specify in your .bashrc to delete the contents of the directory on logout.

How to run a shell script over ssh with resource(.txt files) in one machine and the script in another machine?

I want to run a shell script using SSH which takes resource from other machine while the script is in some other machine, all on the same network. I don't want to copy the resource to the local machine.
Note: The shell script takes .txt file as input
If you have script.sh on server1 and file.txt on server2, you can connect through ssh to server1, and then do:
[user#server1]$ ssh user#server2 "cd mydir && cat file.txt" | ./script.sh
Try this:
ssh USER_NAME#HOST_ADDRESS "BASH_SCRIPT_FILE_PATH"
You will need to provide password whenever required.
If your script is in Machine A, you can't run that on Machine B without copying it over. First, copy the script over to Machine B using scp
[user#machineA]$ scp /path/to/script user#machineB:/home/user/path
Then, just run the script
[user#machineA]$ ssh user#machineB "/home/user/path/script"
This will work if you have given executable permission to the script.
OR
Try this one..
<hostA_shell_prompt>$ ssh user#hostB "ls -la"
That will prompt you for password, unless you have copied your hostA user's public key to the authorized_keys file on the home of user .ssh's directory. That will allow for passwordless authentication (if accepted as an auth method on the ssh server's configuration)
I not fully understand your question. Other answers gave "How to run remote script?"
But i think question is Remote script has to take remote file, even I not sure about this
Login Remote PC using ssh.
Install sshfs if not installed .
Then mount other remote machine directory which has the file you want to use in script to local directory. This can be done using sshfs
Then run the script with file from locally mounted directory
Then unmount the directory when you finished.
Somewhat large procedure.
Mounting remote directory with sshfs
man sshfs

Resources