Emacs. read and write file of another user account on same machine - emacs

I have two users ( rajiv which is default ) and hduser(hduser is not in the sudoers file) on my machine.
How do I read/write the files in account hduser from my emacs in user rajiv?
Currently, when I switch to hduser in the terminal and open the file /home/hduser/.bashrc via pico,I can edit the file. But it opens in read-only mode from my emacs in user rajiv

You should be able to use Emacs' "remote file" capability (which is via the Ange-FTP or Tramp packages). You would do it like so:
C-x C-f /hduser#localhost:/home/hduser/.bashrc RET

Related

Emacs TRAMP - find-file can't enter user

I've recently upgrading to emacs 24.5 with TRAMP 2.2.12.
I use plink to access files on a remote server and am having trouble using the ampersand syntax shortcut to enter a different user than my default.
Problem
Executing:
'C-x C-f' /user#server:
After entering '#', I get the error:
"Opening directory: no such file or directory d:/user#".
It appears that find-file ignores that I am trying to send this input to plink. All works fine without specifying the user.
Are you using ido-mode? If so, then it's a bug I've previously reported.

Why am I unable to use emacs to open remote file using tramp?

I am attempting to open a remote file, using a double hop (through a gateway) with ssh keys and key forwarding.
The command I give emacs:
ssh:user#gateway|ssh:otheruser#desthost:~/myfile.txt
And I keep getting the below output instead of the file:
Use M-x make-directory RET RET to create the directory and its parents

What is the best way to open remote files with emacs and ssh

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.

how do I switch to admin user in emacs on windows?

Perhaps I should be asking this on Superuser, but there are many other Emacs questions here so I thought I would try my luck.
I use GNU Emacs 23.2.1 on Windows 7 with User Access Control enabled.
In Emacs, I would like to make changes to some admin files (eg. hosts file). However, on attempting to save the file I get a warning that I do not have permission to write to the file.
Is there a way to get Emacs to escalate to the admin user for editing these files ?
You could always just admit defeat and run the emacs session with elevated privileges.
There's a bunch of ways to do this, and this page lists them, including automatically running stuff as administrator.
This is probably not quite what you want. Otherwise you might create a script that gets a filename as it's first argument, and then just bind that program to a shortcut or something, like admin-save.
If you give that administrator rights, and then call the file with the buffer name as it's first argument and it's destination as it's second, you should be good.
The script could be a simple BATfile, something ala (I'm not on a Windows box so I can't test it for myself)
:: Administrator-copy.bat copies a file with adminstrator privileges.
:: Remember to give it administrator privileges!
: START
COPY %1 %2
: END
But that's a rather clunky solution though.
How to elevate an already running application I do not know.

emacs tramp ftp or ssh

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.