Execute a command with root user


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute a command with root user
# 1  
Old 12-16-2016
Tools Execute a command with root user

i m logged in with user1 id.

i wish to execute the below as root user for which i tried several commands but all of them fail.

Code:
sudo 'cat /tmp/tmp.file >>/etc/logger'
Password:
sudo: cat /tmp/tmp.file >>/etc/logger: command not found

sudo cat /tmp/tmp.file >>/etc/logger
bash: /etc/logger: Permission denied

su - root -c 'cat /tmp/tmp.file >>/etc/logger'
Password:
su: Sorry

sudo -u root 'cat /tmp/tmp.file >>/etc/logger'
sudo: cat /tmp/tmp.file >>/etc/logger: command not found

uname -a
SunOS mymac 5.11 11.2 sun4v sparc sun4v

Can you please point out what am i doing wrong ?
This User Gave Thanks to mohtashims For This Post:
# 2  
Old 12-16-2016
sudo is not a shell. Feed it the string cat /tmp/tmp.file >> filename and it will look for a file named cat /tmp/tmp.file >> filename and complain that no such file exists.

Don't run script with sudo, use sudo to run scripts.
# 3  
Old 12-16-2016
Do you mind explaining to me what is /etc/logger ?
Im missing something...
# 4  
Old 12-16-2016
Tools

Quote:
Originally Posted by Corona688
sudo is not a shell. Feed it the string cat /tmp/tmp.file >> filename and it will look for a file named cat /tmp/tmp.file >> filename and complain that no such file exists.

Don't run script with sudo, use sudo to run scripts.
I m sorry but that was difficult for me to understand.
# 5  
Old 12-17-2016
sudo is not a shell and therefore does not understand redirection (>>/etc/logger) and when you pass the command cat /tmp/tmp.file >>/etc/logger to it in quotes, it sees >>/etc/logger as part of the command name. Instead what you could do is use sudo to run a shell who does this for you:
Code:
sudo bash -c 'cat /tmp/tmp.file >>/etc/logger'

Similar to what Corona688 suggested, if cat /tmp/tmp.file >>/etc/logger is part of a shell script, then you could use sudo /path/to/script to make it happen, since running a script will invoke a new shell that will run as root and that will interpret that shell script.

Last edited by Scrutinizer; 12-17-2016 at 01:44 AM..
# 6  
Old 12-19-2016
Additionally, if you run this:-
Code:
sudo cat /tmp/tmp.file >>/etc/logger

... what you are trying to do is to run cat with privileges so it can open whatever file out want, HOWEVER the >> is in your current, non-privileged shell so if you cannot open the file, then you will get the bash: /etc/logger: Permission denied seen in post 1

You haven't answered vbe's question about what /etc/logger is. Is this just a plain log file, or are you trying to write to the system logs? If it is the latter, overwriting/appending to it would be a bad thing. Usually /etc contains configuration information that you would not want to routinely overwrite/append. Depending on your actual desires, this is more likely to be a file in /var somewhere.

What are you actually trying to achieve?



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute ssh command with additional terminal command to any remote user not working script

Hello i am having an issue with bash script and this is the code now=$(cat hosts1.txt | awk '{print $2;}') while read n ;do ssh root@$now 'useradd test1; echo -e "test1\ntest1" | passwd test1 && echo "test1 ALL=(ALL:ALL) ALL" >> /etc/sudoers' When i execute only part with cat, it... (8 Replies)
Discussion started by: tomislav91
8 Replies

2. UNIX for Beginners Questions & Answers

Running a command as another non-root user

Hi, I am trying to run a command within my KSH script as another user due to permission issues, now both users are non root. I have tried the following command and was unsuccessful: echo "<password>" | sudo -S -u <username> -k command Can I use sudo to run a command as a non-root user? (5 Replies)
Discussion started by: MIA651
5 Replies

3. UNIX for Dummies Questions & Answers

Execute a command as root from normal user

Hi , I am trying to stop and start a process using the below code. I have sudo access on my machine ## PID = process id echo "$PASSWD" | sudo -S kill -9 <PID> echo "$PASSWD" | sudo -S /opt/abc/startserver /opt/abc/startserver: error while loading shared libraries: librts.so: cannot open... (6 Replies)
Discussion started by: rakeshkumar
6 Replies

4. Shell Programming and Scripting

How to take input from the user from the command line and execute commands basedon that?

Hi, I am using solaris 10 and bash shell.Script execution follows below.Initially it will check whether a directory exists or not if does not exist it will create it.(This I have completed) Second step:I have four users say user1,user2,user3,user4.Script should prompt for the user id and... (11 Replies)
Discussion started by: muraliinfy04
11 Replies

5. Shell Programming and Scripting

Execute Root command as Normal user

Hi, We need to execute a root commmand to change the expiry period of a user but we are getting error as permission denied Q How can we execute a root command by a normal user ? :mad: any thing or suggestion will be good .... :b: (3 Replies)
Discussion started by: abhishek1979
3 Replies

6. UNIX for Dummies Questions & Answers

Allow a user use a specific root command!

Hi, I like to allow an user to permit an root command " /usr/ucb/ps -auxwww", do you know how? Kind regards Mehrdad (6 Replies)
Discussion started by: mehrdad68
6 Replies

7. Shell Programming and Scripting

root user command in shell script execute as normal user

Hi All I have written one shell script for GPRS route add is given below named GPRSRouteSet.sh URL="www.google.com" VBURL="10.5.2.211" echo "Setting route for $URL for GPRS" URL_Address=`nslookup $URL|grep Address:|grep -v "#"|awk -F " " '{print $2}'|head -1` echo "Executing ... (3 Replies)
Discussion started by: mnmonu
3 Replies

8. Shell Programming and Scripting

login into root from user and execute command through script

i have logged in as user. I want to write a script to login into root and execute commands for eg. ifconfig or other command. kindly help me out. (6 Replies)
Discussion started by: pradeepreddy
6 Replies

9. Shell Programming and Scripting

shell script to execute user command

I don't know why the following shell script doesn't work. Could you please help me out? #!/usr/bin/ksh test="cal > /tmp/tmp.txt 2>&1" $test I know it will work for the following format: #!/usr/bin/ksh cal > /tmp/tmp.txt 2>&1 However, I need to get the command from the user in... (1 Reply)
Discussion started by: redtiger
1 Replies

10. UNIX for Dummies Questions & Answers

Maint user cannot execute ping command

I want give rights for the maint user to execute the "ping" command. Currently root user can execute the "ping" command, but the maint user is not able to execute the command. (3 Replies)
Discussion started by: kabeer_n
3 Replies
Login or Register to Ask a Question