how to become root in vi?

Ask a Question related to Linux / Unix Administration, Design and Development.

  1. #1

    Default how to become root in vi?

    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

    Bing Du Guest

  2. Similar Questions and Discussions

    1. ExpandPath evaluates to ColdFusion root instead ofvirtual web server root
      Hi, I am running ColdFusion MX7 on a Windows 2000 Server with IIS 5. The server is running multiple web sites using virtual hosting (host...
    2. Web content root ?= CF application root?
      Are there any benefits to using a web content root that differs from the CF application root of (on Windows)...
    3. su from root
      I have encountered an unusual issue where the behavior is different between FreeBSD 4.6 and 5.3. If I login and then su to root successfully,...
    4. Document root not website root
      Could someone please help me with this. I want to use PHP in my website; but run into a snag, the recommended method of calling an include is:...
    5. ftp root
      Hi all, How can I set the ftp root of user? By default, the ftp root of user abc is /home/abc. I want to set it as /home/aaa/folder. Is it...
  3. #2

    Default Re: how to become root in vi?

    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
    Dave Hinz Guest

  4. #3

    Default Re: how to become root in vi?

    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.

    S. Anthony Sequeira Guest

  5. #4

    Default Re: how to become root in vi?

    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:
    >
    >>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
    Bing Du Guest

  6. #5

    Default Re: how to become root in vi?

    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:
    >
    >
    >>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
    Bing Du Guest

  7. #6

    Default Re: how to become root in vi?

    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

    Dave Hinz Guest

  8. #7

    Default Re: how to become root in vi?

    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:
    > 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:
    > >
    > >
    > >>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
    joe durusau Guest

  9. #8

    Default Re: how to become root in vi

    On Mon, 10 May 2004, Bing Du wrote:
    > 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.
    Saving it then moving it is definitely your best and easiest way to go. You
    might (MIGHT, I haven't and wouldn't test this, personally) be able to do
    something funky like

    :w !sudo cat > /filename/needing/root/privs

    or something. I still wouldn't recommend it; anything you do as root opens you
    up to potential problems, so it's best not to be too tricky.
    adam@cec.wustl.edu Guest

  10. #9

    Default Re: how to become root in vi?

    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.
    Doug Freyburger Guest

  11. #10

    Default Re: how to become root in vi?

    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


    Birger Blixt Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139