I want to use a ssh config file that is not in
~/.ssh/config.
When I use the command line
sh -F my_ssh_config
it works.
Now I want to do the same from Emacs dired (tramp).
Whatever I try it hangs on connecting to my vagrant VM .
What I am trying to do :
I have multiple vagrant project directories, each with its own ssh
private key, some of these are discarded/temporary
I like to
browse my VM folders using Emacs dired
I don't want to use my
~/.ssh/config file - it will get cluttered with these ssh connections
Attempted tweak (got "process died" from Tramp)
ELISP> ( tramp-get-completion-function "ssh")
((tramp-parse-connection-properties "ssh")
(tramp-parse-shosts "~/.ssh/known_hosts")
(tramp-parse-sconfig "/path/to/my_ssh_config_file"))
Also "my_ssh_config_file" functions correctly when used in the shell command "ssh -f my_ssh_config_file" - and gets into my vagrant box.
Tramp does not support alternative config files. Connection arguments are defined in tramp-methods. Check this variable, and double an entry (for example the "ssh" entry) to "myssh" with the arguments you need. However, be careful, because Tramp has several assumptions on the arguments.
Related
I connect to the remote machine with ssh user#192.168.1.5. When I need to open a file in the remote machine I do, e.g.,
emacs /usr/share/nginx/html/index.html
and that opens the index.html file in the shell. I noticed that some emacs commands work but others do not work. For instance, C-w does not work; M-< does not work. How can I fix this, and what is the best way to work with emacs and ssh?
I found this question but it made me more confused.
I generally prefer opening remote files from a local Emacs instance.
While running Emacs on your local machine, opening a remote file over ssh is not much different than opening any other file besides a slightly different syntax.
For ssh, you can type C-x C-f. Now, in the minubuffer you want to type /ssh:user#host:/path/to/file (Note that tab completion will work once you start typing a path. Also note the leading / character). See the full docs here.
In your example, that would be:
C-x C-f /ssh:user#192.168.1.5:/usr/share/nginx/html/index.html
Now you can edit remote files over ssh in Emacs while using your local configuration and any installed packages, etc...
Just to add to the answer above, you can write shortcuts for machines that you use
frequently:
(defun connect-remote ()
(interactive)
(dired "/user#192.168.1.5:/"))
This will open a dired buffer on a remote machine. You can navigate this buffer
as you would a local one.
If you have set up ssh keys for the remote machine, you don't even have to enter the password.
If you have a bunch of remote machines, you can give some recognizable name
to each function, e.g. connect-cupcake, connect-kitkat and use smex package for completion.
And to add to #abo-abo's post about "shortcuts" --
Use Emacs bookmarks. Just create bookmarks normally, when you visit a remote file or directory. Then just use C-x r b to jump to a remote bookmark, whose name you provide (with completion).
If you use Bookmark+ then remote bookmarks are highlighted specially in the *Bookmark List*, so you can recognize them more easily. And remote bookmarks that must be accessed by su or sudo (root) are highlighted differently.
If you use Dired+ then you can also quickly bookmark multiple remote files or directories, by visiting their containing remote directory in Dired, marking them, and hitting C-x b. No need to give the bookmarks names; they are named after the files. Even if you never use those bookmarks for navigating to the remote files, you can use them with Bookmark+ tags to organize the files and thus operate on subsets of them.
If you use Icicles then whenever you use a command to jump to a bookmark, you can narrow the completion candidates to those that are remote by hitting C-M-# during completion.
The original poster expressed interest in opening remote files as the root user. This can be done with the command:
C-x C-f /ssh:you#remotehost|sudo:remotehost:/path/to/file RET
More documentation can be found here: https://www.emacswiki.org/emacs/TrampMode#toc14
A Simple Answer that focuses on the remote machine:
If I plan to do all my emacs work on the remote machine, I use
ssh -X username#hostname
and then run emacs in the remote session, displaying back on my local machine. It's an old question but I wanted to throw this in for completeness. Granted there are some xhost / X config issues but in many networks this will work right off the bat!
SSH mode for emacs is what you're looking for.
Once you have it set up you just run
M-x ssh RET hostname RET
Then it prompts you for your password twice (once for the command line, once for loading files).
For the most part you can treat it like any other shell (non-interactive and a few minor differences, but that's it).
It keeps track of which directory you're in, so when you want to open a file from the directory you're looking at it automatically starts in the right directory and you just need to enter in the file name.
Emacs Wiki has more info too.
Every time I'm visiting a file system in remote machine and I execute a command (e.g. grep) the emacs buffer with the result of that command also contains my verbose .bash_profile output.
How could I disable this behavior? That is, how to make the remote command only load .bashrc? (In tramp my remote method is ssh)
I suggest to move everything in your .bash_profile that outputs text to the end of the file and put the following before these commands:
If [ "$TERM" == "dumb" ]; then
return
fi
That should do it. Reread http://www.gnu.org/software/tramp/#Remote-shell-setup for more information.
I tried to open a remote file via Emacs via Tramp.
(require 'tramp)
(setq tramp-default-method "ssh")
I get a message from Emacs
Tramp: Waiting for prompts from remote shell
Emacs hung and did not respond to any action after that
Emacs was installed on Windows; the remote file was on a Linux machine.
If the account you're connecting to uses some weird fancy shell prompt, then there is a good chance that this is what makes tramp trip.
Log in as root, then enter
PS1="> "
(that's a normal, standard shell (ZSH, BASH, younameit) prompt, one that tramp will understand)
then switch to the user account, and launch emacs -q (to make sure that your .emacs is not causing this mess) and try to C-x C-f /sudo:root#localhost:/etc/hosts and see what's what.
You can (not recommended) also customize the regexp that defines what tramp expects :
M-x customize-variable RET tramp-terminal-prompt-regexp
My approach :
Make sure the variable tramp-terminal-type is set to "dumb"
M-x customize-variable RET tramp-terminal-type
Test that in your .*shrc and serve the correct prompt :
case "$TERM" in
"dumb")
PS1="> "
;;
xterm*|rxvt*|eterm*|screen*)
PS1="my fancy multi-line \n prompt > "
;;
*)
PS1="> "
;;
esac
Your Windows ssh client is the key here, and the 'ssh' Tramp method is almost certainly wrong.
If you're using Cygwin, then you need to use the 'sshx' method, and you probably need to use ssh-agent to handle authentication. Details are here:
Using tramp with EmacsW32 and cygwin, possible?
I imagine the same applies to any stand-alone ssh client which does not require a full Cygwin installation, but does use the Cygwin DLLs. (I mention this, because I'm pretty sure I remember seeing such a thing.)
If you're using PuTTY then you want the 'plink' method, as Alex Ott pointed out. If the Wiki doesn't suffice, a search here will probably turn up solutions for configuring that approach.
Other alternatives I can suggest are:
Use the Cygwin-native Emacs. That will be slower than NTEmacs, but Tramp seems to work well with the 'ssh' method, and password-prompting works as well.
Host a Linux VM on your Windows box, and run Emacs on that. That's a fairly large hoop to jump through, but it's my preferred way of using Tramp when working in Windows.
Well, this is a defect of tramp.
The real solution is to prevent loading .bashrc when tramp is used. (because now it is PS1, but it can be PATH, or any other thing that your .bashrc will do that will displease tramp...).
This can be done by asking ssh to set an environment variable, and testing it in .bashrc:
Add this to ~/.emacs:
(require 'tramp-sh nil t)
(setf tramp-ssh-controlmaster-options (concat "-o SendEnv TRAMP=yes " tramp-ssh-controlmaster-options))
and that at the beginning of ~/.bashrc:
if [ ! -z ${TRAMP-x} ] ; then
return
fi
Another default of tramp is that it doesn't have a variable to pass random arguments to the ssh command, we have to piggy-back on tramp-ssh-controlmaster-options.
Had you checked Emacs wiki for solution? ssh is in PATH? It's also recommended to use plink on MS Windows - see section "Inline methods" in Tramp documentation
If the problem is your fancy custom prompt in the remote shell, an easy workaround is to add to your .bashrc or equivalent:
if [[ $TERM == "dumb" ]]; then
export PS1="$ "
fi
After you define your PS1.
Note: the credit goes to ChasingLogic as this is their suggestion in this thread.
By the way -- if You need tramp to sudo -- You can actually sudo without tramp using sudoedit.
Currently I'm using this bash function:
erf () { SUDO_EDITOR="emacsclient -a emacs" sudoedit $#; }
I'm using Aquamacs and started the emacs server with M-x server-start. My emacsclient connects just fine. However, when I start another ssh session (on the same pc) it cannot locate my socket-name / server file. It's not in ~/.emacs.d/server/server where I would expect it.
How can I find out which server-file / socket-name the emacs server is currently using?
This is my working configuration:
.emacs
(setq server-socket-dir "~/.emacs.d/server")
(server-start)
em.sh
location="/`whoami`#`hostname`:$(readlink -f $1)"
ssh <YOU#LOCAL-WORKSTATION> emacsclient -n -s '~/.emacs.d/server/server' $location
The variable server-socket-dir (atleast on GNU Emacs) is what holds the location of the socket. The socket file in my case is simply called server.
It's in $TMPDIR/emacsXXX/server where XXX is a number that depends on the version of Aquamacs you're running
This should also fix your problem without needing to manually delete the file.
M-x server-force-delete
I'm using emacs 23.1 on Windows XP with the following configuration
(require 'tramp)
(setq tramp-default-method "ftp")
I can open/save remote files. Find-file auto-completion with TAB works as well. But I cannot open a remote directory, it shows an empty buffer with a directory name on the top.
Here are log messages
ls -lhA d:/temp/ange-ftp2876pvQ
200 PORT command successful.
150 Opening data connection for /bin/ls.
226 Transfer complete.
quote mdtm /soft/mysrv/jboss-4.2.3.GA/bin/
550 /soft/mysrv/jboss-4.2.3.GA/bin/: not a plain file.
quote mdtm /soft/mysrv/jboss-4.2.3.GA/bin/
550 /soft/mysrv/jboss-4.2.3.GA/bin/: not a plain file.
And I cannot dired-do-copy several marked files to the remote directory. It copies only the first file and says "No file on this line". Should I switch to ssh tramp to resolve these problems?
What are other advantages of changing to ssh if I don't care about security and clear text passwords?
UPDATE: I changed
(setq tramp-default-method "pscp")
and it works now.
I can't tell you how well the ftp method works in general as I usually work with scp or ssh. Advice like "If the ftp method doesn't work, use it" is probably not what you are looking for.
However, you may want to have a look at the tramp-default-method-alist variable that lets you specify which method to use based upon the remote user and the remote host.
Yes, it was necessary to change tramp-default-method
(setq tramp-default-method "pscp")
Now it works correctly on the servers where ssh is installed.