tramp never stops asking for password - emacs

I've having serious trouble using tramp on Emacs. I am continuously being asked for my password when I ssh into a server via tramp. This is to the point where it blocks me being able to do anything.
I have looked far and wide on the internet and the most widely used solutions do not seem to work for me.
I have Emacs 24.*
I have (setq password-cache-expiry nil)
I have (setq projectile-mode-line " Projectile")
Here is my tramp setup in my .emacs.d for the curious http://pastebin.com/NJCaa0RH.
Has anyone else had this problem and can point me in the way of a fix? Or do you see a glaring error in tramp config?

Make sure you can login via keys instead of password. This would involve copying ~/.ssh/id_rsa.pub or ~/.ssh/id_ed25519.pub from your local machine and pasting the same in ~/.ssh/authorized_keys on the server
Having a ssh config should help simplify the syntax
Having a persistent connection would also help with your problem
(customize-set-variable
'tramp-ssh-controlmaster-options
(concat
"-o ControlPath=/tmp/ssh-ControlPath-%%r#%%h:%%p "
"-o ControlMaster=auto -o ControlPersist=yes"))

Related

emacs:open file through ssh failed

When trying to open a file through ssh in emacs, I got an error:Couldn't find a POSIX `id' command
QUESTION: What is the problem?
Does your remote system has proper id command? You can verify what TRAMP is trying to do if you set
(setq tramp-verbose 10)
Then try to connect and monitor buffers *tramp/...* and *debug tramp/...*. If the remote path does not get set correctly by TRAMP, you can change it:
(add-to-list 'tramp-remote-path "/path/to/id/command")
This just happened to me recently for the first time... after never having the issue before on the same cluster of servers. It ended up being that the server I was trying to connect to was out of disk space...and this error was just a side effect of that. So it's worth doing a df -h if this issue seems to come out of nowhere.

Tramp mode in emacs using ssh config

I think this is very basic question in using tramp, but it doesn't work for me.
I have my ~/.ssh/config file that points to my amazon ec2 machine
Host amazon
Hostname xxxx.amazonaws.com
Port yyy
User me
IdentityFile ~/.ssh/ubuntu
ForwardAgent yes
I can easily do ssh amazon from my terminal and I go to amazon ec2 (so my config is right), but in emacs
I do:
C-x C-f /ssh1:amazon:
I always get this error
In Aquamacs:
Process *tramp/ssh1 amz* exited abnormally with code 255
In Emacs:
tramp: Opening connection at amz using ssh1...
tramp: Waiting for prompts from remote shell
tramp: Waiting 60s for prompt from remote shell
tramp-process-actions: Login failed
I also have other ssh configurations that they ssh to my virtual boxes on my local machine and they have the same problem.
I really appreciate any help.
One thing that's worth trying is using the sshx connection method. That makes tramp try to avoid any non-standard shell configuration on the remote host.
Like this:
C-x C-f /sshx:amazon:
The tramp method ssh1 forces ssh to be run in ssh v1 protocol mode with the parameter -1. ssh v1 has known weaknesses and is insecure. Hence a lot of sites disable the ssh v1 protocol.
You can verify this from the shell with ssh -1 me#xxxx.amazonaws.com.
Try other tramp connection methods like ssh, sftp or scpx. You can see all pre-configured connection methods with C-h v tramp-methods.
If Moritz Bunkus's answer doesn't solve the issue, then you can configure the verbosity of tramp's output with
M-x customize-variable RET tramp-verbose RET
In particular, level 6 is "sent and received strings" which might help you to determine whether the "Waiting for prompts from remote shell" is because it isn't receiving a prompt pattern that it recognises, or because of some more critical failure.
If it's simply receiving a prompt it doesn't recognise, then you might look at customizing the tramp-login-prompt-regexp or tramp-shell-prompt-pattern variables.
(Of course if your ssh agent is working correctly, then login prompts shouldn't be relevant.)
If you're running Emacs in Windows, then also see these Q&As:
Emacs: Tramp doesn't work
Using tramp with EmacsW32 and cygwin, possible?

Emacs Tramp ssh double hop

Could somebody please help me setup Emacs Tramp to do a double hop?
I want to work on machine2.abc.def.edu to which I can connect only through machine1.abc.def.edu. My username is myname, on both machines same.
I've tried to add .emacs:
(add-to-list 'tramp-default-proxies-alist
'("\\`machine2\\.abc\\.def\\.edu\\'"
"\\`myname\\'"
"/ssh:machine1\\.abc\\.def\\.edu:"))
This is my best guess interpretation of what's in the manual. Then I do:
C-x C-f /ssh:machine2.abc.def.edu
or:
C-x C-f /ssh:rsuhada#machine2.abc.def.edu
But both give:
ssh: Could not resolve hostname ssh: nodename nor servname provided, or not known
Process *tramp/scpc ssh* exited abnormally with code 255
And my Aquamacs can't be quitted and have to killed from shell... There is a 2 years thread here with same question. I've tried the answer from there:
(add-to-list 'tramp-default-proxies-alist
'("machine2.abc.def.edu"
nil
"/ssh:myname#machine1.abc.def.edu:"))
With same results... also for all combinations I could come up with... Remote editing on machine1.abc.def.edu works fine, though.
The answer it to use the ssh_proxy command available in ssh_config. Documented here and here. Basically you create a config file in your ssh folder that you can write shortcuts in. One of your shortcuts is to use a proxy through another end point. All of your shortcuts work for any tool that uses ssh including git and emacs.
Host endpoint2
User myusername
HostName mysite.com
Port 3000
ProxyCommand ssh endpoint1 nc -w300 %h %p
Host endpoint1
User somename
HostName otherdomainorip.com
Port 6893
In this example running ssh endpoint2 will automatically hop through endpoint1.
Okay, let's try something different then, without opening a tunnel. How about the following in your .emacs file:
(add-to-list 'tramp-default-proxies-alist
'("\\`machine2\\'"
nil
"/ssh:%u#machine1.abc.def.edu:"))
This is different from the code you found in the forum post in two points:
it adds ticks around the target host
name (Emacs regexp syntax to avoid
matching partial names)
it uses only
the subdomain name in the target
host (you reported in a comment
below that you cannot ssh to
machine2 when you use the full
domain name)
Does that help when you try to access a file on machine2?
Set up an ssh tunnel from machine1 to machine2 (assuming that sshd runs on port 22 on machine2):
machine1.abc.def.edu> ssh -f -N -L 2222:localhost:22 machine2.abc.def.edu
Then either connect to machine2 from Emacs like this:
/ssh:machine1.abc.def.edu#2222
or add the following line to your .emacs:
(add-to-list 'tramp-default-proxies-alist
'("\\`machine2\\.abc\\.def\\.edu\\'" nil
"/tunnel:machine1.abc.def.edu#2222:"))

Open file via SSH and Sudo with Emacs

I want to open a file inside Emacs which is located on a remote server, with sudo powers on the server. I can open local files with sudo via Tramp like this:
C-x C-f /sudo::/home/user/file
But I want to use sudo on the server:
C-x C-f /sudo::user#server/home/user/file
But this gives me sudo powers on my local machine, it asks for my sudo password on the local machine. Is there a way to use sudo on the server?
BTW: Emacs is not installed on the server
As of Emacs 24.3, an analog of the old multi: syntax has been layered on top of the modern tramp-default-proxies-alist approach, meaning that you can once again perform multi-hops without any prior configuration. For details, see:
C-hig (tramp)Ad-hoc multi-hops RET
With the new syntax, each 'hop' is separated by |. The example in the manual is:
C-xC-f /ssh:bird#bastion|ssh:you#remotehost:/path RET
Which connects firstly as bird#bastion, and from there to you#remotehost:/path
/su: or /sudo: on remote hosts
You can also use this syntax to sudo/su to root (or of course any other user) on a remote host:
C-xC-f /ssh:you#remotehost|sudo:remotehost:/path/to/file RET
Important: be sure to specify the hostname explicitly: sudo:remotehost: rather than sudo:: (see below).
As this still uses the proxy mechanism underneath, tramp-default-proxies-alist should now include the value ("remotehost" "root" "/ssh:you#remotehost:")
Meaning that the proxy /ssh:you#remotehost: is going to be used whenever you request a file as root#remotehost.
root is the default user for these methods, but you can of course also change to a non-root user with:
C-xC-f /ssh:you#remotehost|sudo:them#remotehost:/path/to/file RET
Always specify the remote hostname explicitly
You are probably used to using sudo:: or su:: and omitting the hostname. If you are staying on the localhost then this is still fine, but if you are hopping to a remote server then you must specify the hostname for every hop -- even if it is the same as for the previous hop. Always use sudo:hostname: or su:hostname: with remote hosts.
The trap here is that sudo:: does actually appear to work -- however when you do that the HOST for the dynamic proxy entry will be the hostname you originated from rather than the host you connected to. This will not only look confusing (as the wrong host will be displayed in the file paths), but it will also mean that any subsequent attempt to use sudo:: on your localhost will instead be proxied to the remote server! (and the proxy would also presumably be clobbered if you did the same thing on a second server, causing further issues).
In short, don't use :: when you multi-hop!
Emacs 27+
Starting from Emacs 27.1 (or Tramp 2.4.2, if using the GNU ELPA package) the :: case works intuitively, such that /ssh:you#remotehost|sudo:: will re-use remotehost rather than your own local host, and so you won't end up with a bad proxy entry.
In addition, the likes of /ssh:you#remotehost|sudo:localhost: are detected and flagged as user errors.
If you are liable to use a mixture of Emacs versions including versions earlier than 27 (or you are advising someone else who may be using an older version), then it would be safest to continue to treat :: as unsafe when multi-hopping, to avoid potential mishap. (I.e. specifying the correct remote host explicitly will remain the safest approach if the Tramp version is unknown.)
Update: Although this answer solved the original problem, it was written for emacs 20 or 21. For emacs 24, I recommend you use phils's answer because it offers more explanation and is up to date.
I think multi-hop filenames in tramp is what you're looking for.
The first hop would be ssh and the second would be sudo.
Update: Recent versions of emacs support multiple hops using proxies:
(add-to-list 'tramp-default-proxies-alist ("my-sudo-alias" nil "/ssh:user#ssh-host"))
Then invoke by opening:
/sudo:my-sudo-alias:file-on-ssh-host
I had some troubles with the selected answer. However, it worked when I added this line to .emacs:
(add-to-list 'tramp-default-proxies-alist '(".*" "\\`root\\'" "/ssh:%h:"))
And then executed the following:
/sudo:ssh-host:file-on-ssh-host
It was slightly confusing because at one point I was prompted for the "root" password, but entering my user's password granted me access. It also universally works on all hosts on the network. Also, I can still do this to not be root:
/ssh:ssh-host:file-on-ssh-host
From the tramp multi-hops configuration webpage
(add-to-list 'tramp-default-proxies-alist
'(nil "\\`root\\'" "/ssh:%h:"))
(add-to-list 'tramp-default-proxies-alist
'((regexp-quote (system-name)) nil nil))
Then any
C-x C-f /sudo:remote-host:/file
will open file using sudo after logged with the same username of the user running emacs but on the remote machine.
You have to ssh into the server first, then you have to run emacs locally.
Or you can use NFS with no_root_squash, or you can try with emacs server/client, although I have no idea of what may happen (do not use emacs myself)

Is it possible for Emacs to run a Matlab shell that is located on a remote server?

I would like to be able to use Matlab-mode (matlab.el) to run a matlab shell. The only problem is, Matlab is located on a remote host. Is this even possible? If so, please explain. I'm currently playing around with tramp but to no avail.
Thanks.
I just tried using the emacs matlab mode through ssh, no problem
had to make a script which loads matlab, couldn't just specify the command in matlab.el
so:
/home/second/remoteMatlab:
#!/bin/bash
ssh orac matlab -nodisplay
where orac is the host which has matlab. you can pass commands to ssh here. i use ssh config and key authentication so only need the hostname here
in matlab.el, find
(defcustom matlab-shell-command
and set to eg
(defcustom matlab-shell-command "/home/second/remoteMatlab"
I am not 100% sure what you are doing, but this seems possible. If
you use Tramp (or sshfs, my personal choice) to edit a script on a
remote machine, you will be able to run it on that remote machine. If
you have a *shell* buffer open, it can easily be ssh'd to that remote
machine. If you are doing something REPL-like where emacs sends input
to this buffer that's connected to a process over ssh, it should still
work. If it is communicating with a remote process over a socket, you
can use ssh forwarding to make the socket appear local.
Anyway, there is not much gap between local and remote, so this should
all work out.
Finally, to toe the (GNU) party line... have you tried Octave running locally?
#Yotham : I can't comment yet on earlier answers, but your problem (Script working, malab-shell command in emacs not) may be due to the fact that you didn't delete/rename your matlab.elc file?
This way emacs never gets to know of the change since it prefers .elc over .el files for speed.
Resolved the same issue for me.
cheers
tnt