Unix administration - how to become root in vi?

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > June 2004 > how to become root in vi?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author how to become root in vi?
Bing Du

2004-05-08, 10:40 am

Greetings,

I was creating a file in a system privileged directory as me. When I
tried to save the file, of course, I got 'Can't open file for writing'.
How can I become root in vi (I can sudo to root) and then save the
file (very long) all done without leaving vi? I don't want to lose
anything I've just typed.

Thanks for any ideas,

Bing

Dave Hinz

2004-05-08, 10:40 am

On Fri, 07 May 2004 16:07:11 -0500, Bing Du <bdu@iastate.edu> wrote:
> Greetings,
>
> I was creating a file in a system privileged directory as me. When I
> tried to save the file, of course, I got 'Can't open file for writing'.
> How can I become root in vi (I can sudo to root) and then save the
> file (very long) all done without leaving vi? I don't want to lose
> anything I've just typed.


Have you tried:
sudo vi filename
....which should execute "vi" as root, and give you the permissions
you need to write to filename?

Dave Hinz
S. Anthony Sequeira

2004-05-08, 10:40 am

On Fri, 07 May 2004 16:07:11 -0500, Bing Du penned:

> Greetings,
>
> I was creating a file in a system privileged directory as me. When I
> tried to save the file, of course, I got 'Can't open file for writing'.
> How can I become root in vi (I can sudo to root) and then save the
> file (very long) all done without leaving vi? I don't want to lose
> anything I've just typed.
>
> Thanks for any ideas,
>
> Bing


Save the file in another directory and then, as root, move it where you
want it.

:w /dir/filename
--
Tony

Experience is directly proportional to the cost of the equipment ruined.

Bing Du

2004-05-10, 5:44 pm

That's too late. When I realized I did not have enough permission to
save the file, I had already been in vi. Thanks for the response.

Bing

Dave Hinz wrote:

> On Fri, 07 May 2004 16:07:11 -0500, Bing Du <bdu@iastate.edu> wrote:
>
>
>
> Have you tried:
> sudo vi filename
> ...which should execute "vi" as root, and give you the permissions
> you need to write to filename?
>
> Dave Hinz


Bing Du

2004-05-10, 5:44 pm

Ah, I kinda like this workaround. But still curious to know if there
would be a way that could save the file directly in the directory I
wanted. Thanks for the heads-up.

Bing

S. Anthony Sequeira wrote:

> On Fri, 07 May 2004 16:07:11 -0500, Bing Du penned:
>
>
>
>
> Save the file in another directory and then, as root, move it where you
> want it.
>
> :w /dir/filename


Dave Hinz

2004-05-10, 5:44 pm

On Mon, 10 May 2004 08:06:41 -0500, Bing Du <bdu@iastate.edu> wrote:
> That's too late. When I realized I did not have enough permission to
> save the file, I had already been in vi. Thanks for the response.


Yes, then save it with a different filename, in a place where you have
permission to write (/tmp at least, and/or your home account). Move it
using sudo to it's intended location. There isn't a way to become root
during a vi session that isn't more work than just saving it elsewhere
and moving it into the right place.

That having been said - how long do you go between saves? If you've made
so many changes that losing them would be painful, I would suggest that
you're not saving often enough.

Good luck,
Dave Hinz

joe durusau

2004-05-10, 5:44 pm

I wouldn't think that you could do that without doing something
that is inherently denagerous - like, maybe building a private copy of vi
that only you use and making it suid root. I certainly can't think
of any I would reccommend.

Speaking only for myself,

Joe Durusau


Bing Du wrote:
[vbcol=seagreen]
> Ah, I kinda like this workaround. But still curious to know if there
> would be a way that could save the file directly in the directory I
> wanted. Thanks for the heads-up.
>
> Bing
>
> S. Anthony Sequeira wrote:
>

Doug Freyburger

2004-05-10, 5:44 pm

Bing Du wrote:
>
> I was creating a file in a system privileged directory as me. When I
> tried to save the file, of course, I got 'Can't open file for writing'.
> How can I become root in vi (I can sudo to root) and then save the
> file (very long) all done without leaving vi? I don't want to lose
> anything I've just typed.


Use the "!" to get a subshell. Use sudo to get privileges. Use
"chown" or "chmod" on the directory in question so the regular user
your vi session is running as can write the file.

The cleaner apporach is to save it elsewhere and handle it outside
of vi.
Birger Blixt

2004-06-10, 5:09 pm

On 2004-05-07 23:07, Bing Du wrote:
> Greetings,
>
> I was creating a file in a system privileged directory as me. When I
> tried to save the file, of course, I got 'Can't open file for writing'.
> How can I become root in vi (I can sudo to root) and then save the file
> (very long) all done without leaving vi? I don't want to lose anything
> I've just typed.
>
> Thanks for any ideas,
>
> Bing
>


This is an ugly one :-)

:map W :!sudo chown $USER % ^M :w! ^M :!sudo chown root % ^M

where ^M is typed <ctrl>V<ctrl>M

Then save with W

If you are really brave, put it in .exrc

But seriously , save it in /tmp and copy the file as root later.


/bb


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com