1

I have a virtual machine in Google Cloud Compute where I can ssh/login as my own user, and then sudo to root in order to access files used by a systemd service. I would like to edit these files through tramp. How can I achieve this?

Current tramp method to connect as my own user:

;; make sure you've set your default project with:
;; gcloud config set project <project-name>

(require 'tramp)
(add-to-list
 'tramp-methods
 '("gcssh"
   (tramp-login-program "gcloud compute ssh")
   (tramp-login-args (("%h")))
   (tramp-async-args (("-q")))
   (tramp-remote-shell "/bin/sh")
   (tramp-remote-shell-args ("-c"))
   (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
                   ("-o" "UserKnownHostsFile=/dev/null")
                   ("-o" "StrictHostKeyChecking=no")))
   (tramp-default-port 22)))

;; ... after which it's as easy as:
;;
;; C-x C-f /gcssh:compute-instance:/path/to/filename.smile

source

2 Answers 2

3

You’ve already given one possible answer, but there is no need to create a special tramp method for root access; there are generic su and sudo methods, and you can chain multiple methods together into a single path.

So if /gcssh:compute-instance:/etc/systemd/system/foo.service opens the systemd unit file that you care about on the host compute-instance, then /gcssh:compute-instance|sudo::/etc/systemd/system/foo.service will sudo to root first, before opening the file.

Edit:

TRAMP has it’s own manual, but it does come with Emacs. Use C-h i to open the Info browser, and select the TRAMP manual from the list. You might start with chapter 4.1 File name syntax. Each TRAMP filename is three parts — the method, “remote” hostname, and path — separated by colons. For ssh, you can specify a username, host, and port as part of the hostname, so /ssh:[email protected]#2222:/path/to/file. The sudo method also lets you specify a user name; it assumes you mean root if it is left out. Weirdly you can also give it a host name, and it will ssh there first. /sudo::/path/to/file is the same as /sudo:root@localhost:/path/to/file.

1
  • I was trying a permutation of that, but couldn't get it right. What's the :: for, and where can I learn about it in the documentation?
    – dmvianna
    Commented Mar 12 at 5:03
0

The trick was to add all connect commands to the list in tramp-login-args. So

gcloud compute ssh test -- sudo su

became

(add-to-list
 'tramp-methods
 '("gcsroot"
   (tramp-login-program "gcloud compute ssh")
   (tramp-login-args (("%h" "--" "sudo" "su")))
   (tramp-async-args (("-q")))
   (tramp-remote-shell "/bin/sh")
   (tramp-remote-shell-args ("-c"))
   (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
                   ("-o" "UserKnownHostsFile=/dev/null")
                   ("-o" "StrictHostKeyChecking=no")))
   (tramp-default-port 22)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.