[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Would like to have tramp pass sudo arg

From: Michael Albinus
Subject: Re: Would like to have tramp pass sudo arg
Date: Mon, 03 Sep 2018 11:42:17 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Tom Wurgler <address@hidden> writes:

Hi Tom,

> I have the following code, which works well to a point:
> (defun sudo-shell-as-user (host user &optional arg)
>   "Use sudo to open a shell on HOST as USER
> The password it asks for is the root password on the specified
> machine."
>   (interactive "sMachine? \nsUser? ")
>   (let* ((tramp-default-proxies-alist
>       `((,(regexp-quote host)
>          ,(regexp-quote user)
>          "/ssh:address@hidden:")))
>      (dir "")
>      (bname)
>      (arg (if arg arg 1))
>      (newone (if (not (string-match "/sudo:" default-directory)) t
> nil))
>      (default-directory (if newone (concat "/sudo:" user "@" host ":")
> default-directory)))
>     (setq bname (concat "*ssh-sudo:" user "@" host "*<"
> (number-to-string arg) ">"))
>     (shell bname)
>     (if (string-match dir ".") (setq dir "~"))
>     (if newone
>     (progn
>       (sit-for 2)
>       (insert (concat "cd " dir))
>       (comint-send-input)))))
> This lets me log in as the user using sudo to the specified computer.
> But I don't have that that user's environment.  I *think* what I need
> is the option "-i" or "--login" to the command sudo.  But I can't
> figure how to get it in there.
> Is this possible?  Is this what I even need? 

The arguments for all methods are declared in tramp-methods. For the
sudo method, we have

--8<---------------cut here---------------start------------->8---
  (tramp-login-program "sudo")
   (("-u" "%u")
    ("-p" "P\"\"a\"\"s\"\"s\"\"w\"\"o\"\"r\"\"d\"\":")))
  (tramp-remote-shell "/bin/sh")
  (tramp-connection-timeout 10))
--8<---------------cut here---------------end--------------->8---

What you need is to change in tramp-login-args "-s" to "-i". The
technique to change values in tramp-methods is described in the Tramp
manual, (info "(tramp) Predefined connection information")

The following addition to your code should do the trick:

--8<---------------cut here---------------start------------->8---
     (newone (if (not (string-match "/sudo:" default-directory)) t nil))
     (default-directory (if newone (concat "/sudo:" user "@" host ":") 
+    (tramp-connection-properties
+     `((,(regexp-quote (file-remote-p default-directory))
+        "login-args" (("-u" "%u") ("-i") ("-H")
+                      ("-p" "P\"\"a\"\"s\"\"s\"\"w\"\"o\"\"r\"\"d\"\":")))
+       . ,tramp-connection-properties)))
    (setq bname (concat "*ssh-sudo:" user "@" host "*<" (number-to-string arg) 
    (shell bname)
--8<---------------cut here---------------end--------------->8---

> Any input appreciated.
> thanks!
> tom

Best regards, Michael.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]