AIX

AIX

Connect with fellow AIX users and experts to gain knowledge, share insights, and solve problems.


#Power
 View Only
  • 1.  Using ssh in a script and then su to root from there.

    Posted Mon October 22, 2007 07:27 PM

    Originally posted by: SystemAdmin


    Does anyone know how I can ssh to my account on a remote server and then su to root? something like this:
    ssh userMe@myServer "su - root , rm -r myDirectory"

    I tried above command but does not work.
    The reason I am not suering root@myServer is beacuse I am not allowed to connect to the server directly as root. I have to sign in as my own ID and su to root from there.

    THX
    #AIX-Forum


  • 2.  Re: Using ssh in a script and then su to root from there.

    Posted Mon October 22, 2007 09:32 PM

    Originally posted by: orphy


    Pardon my dumb question but you don't own myDirectory and its files? If you do, what's the need for root here? Did someone else (or root) create files in that dir?
    Orphy
    #AIX-Forum


  • 3.  Re: Using ssh in a script and then su to root from there.

    Posted Tue October 23, 2007 07:39 AM

    Originally posted by: SystemAdmin


    I think you have a syntax error here. The su command to run on the remote server should look something like:

    su - root -c "rm -r myDirectory"

    The -c operand specifies the command to be run as root.

    HTH

    Jim Lane
    #AIX-Forum


  • 4.  Re: Using ssh in a script and then su to root from there.

    Posted Tue October 23, 2007 10:20 AM

    Originally posted by: SystemAdmin


    OK...I tried the following but I am getting "Cannot su to "root" : Authentication is denied." it does not even prompt for the root password. Any idea?
    ssh RemoteServer su - root -c "ls -l /home/someDir"
    #AIX-Forum


  • 5.  Re: Using ssh in a script and then su to root from there.

    Posted Wed October 24, 2007 07:17 AM

    Originally posted by: SystemAdmin


    Is your account on the remote server valid? If you just try to ssh and get a login shell does that work?
    #AIX-Forum


  • 6.  Re: Using ssh in a script and then su to root from there.

    Posted Tue October 23, 2007 09:10 AM

    Originally posted by: hwyguy


    One way to do this would be by using sudo on the remote server.

    On the remote server, create a Cmnd_Alias in /etc/sudoers to run the rm command with the directory. then create the user alias to allow the non-root user you want to run the command either with a password or without.

    The /etc/sudoers entries would look something like this if you wanted to do this without a password prompt:

    Cmnd_Alias RM = /usr/bin/rm -r /tmp/myDirectory

    yourusername ALL = NOPASSWD: RM

    Then on your source host, you would run:

    ssh yourusername@destinationhost "sudo /usr/bin/rm -r /tmp/myDirectory"

    I would stress that for security reasons, you don't just add the /usr/bin/rm command without the specified directory. Otherwise if someone broke into the yourusername account, they could run the "sudo rm" command to cause mass havoc.

    Hope this helps!


    #AIX-Forum