how to make a file editable in linux without becoming root - linux

i am working with files placed in directory /etc/asterisk using vim editor, every time i have to do some editing i have to go to terminal to become root and opens file in vim editor and performs edition,but if i do it by directly opening the file from such directory(/etc/asterisk) in gedit and perform edition in the file but it doesn't show the save option if save as option is selected message becomes "you need to become root for such operation." is there any way to open these files(placed in /etc/asterisk) directly from gedit and save the changes without going into terminal

You can always do sudo gedit from the terminal to launch gedit as root to edit the files.

Related

How can I save a write protected file in vi?

I'm trying to save my modified "menu.lst" file in vi. When I save the file, vi says: 'menu.lst' is read only.
How can I fix this?
The file you are trying to save is read-only, meaning you cannot modify its contents. It needs to be marked as writable. The process varies depending on your OS. Here are some helpful resources on how to change permissions of files:
For Windows 10: Nibbleguru: How to remove read-only attribute in Windows 10
For Linux (using chmod): TLDP: File Permissions
For macOS: Chron: How to Change File Permission From Read-Only to Read-Write on a Mac
EDIT:
As filbranden pointed out, for Grub's files, you should be opening vi using the sudo command. Grub's files are meant to be modified by root only. You should be opening your files using sudo vi menu.lst instead.
I have this on my vimrc
cnoremap w!! execute 'silent! write !sudo tee % >/dev/null' <bar> edit!
command! SaveAsRoot w !sudo tee %
cnoreabbrev sudow SaveAsRoot
For instant use, just copy to the clipboard and run:
:#+
:SaveAsRoot
The :#+ loads your clipboard into vim memory which allows you to run the given commands while not saved on your vimrc.
People suggest using sudo vi(m) on unix, but this could have unwanted consequences: all commands executed in this window are done by root and so you could accidentally do unwanted things like deleting file or just creating files owned by root.
Instead you should think about using sudoedit instead. It will copy the file to /tmp and open it in $EDITOR (if you are using vim, you should set it in your ~/.profile / ~/.bash_profile).
But beware: Something I stumbled across: the original file is only replaced when you close vim - no matter how often you save! (This is, because you are editing the file in /tmp and not the original).
It is not that I don't use sudo vim but if I do, I am extra cautious about what I do ;) I do it for example if I know that I will need to edit multiple files as root, or that I want to execute other commands from within vim as root (e.g. git)
And something even more important to me: sudo vim is using roots vimrc instead of mine, but with sudoedit I have my own config...

How to open some files/directories read-write, others read-only

This is similar to "Split vim window with one file read/write and one file read-only".
From the command line, I want to open files in one directory as read-write, and files in another directory as read-only. How do I do this?
For example:
vim read-write-dir/**/* read-only-dir/**/* # zsh
Listing the files individually isn't an option, there's a large number of files under each directory (I use bufExplorer).
My motivation is, I'm editing two programs in different OSX Terminal tabs, and Vim gets unhappy about a file being edited twice.
If you want to open a read-only file and read-write file while starting Vim, you can use the -c option in your terminal:
vim -c ":view file_read_only" file_read_write
Then you can switch between them using :e #

vimrc settings for user dont work for root

I have modified the .vimrc file in my user. The settings dont work when i switch to superuser.
When i check the contents of the file (using vim editor) i can see the mapped keystrokes of newlines
(with all the text extending on the rightside beyond screen on same line), but when i check the contents of .vimrc as root, i see all the text in the same window screen one below the other (this may be a very stupid difference, but that is the only difference i noticed. all the data in the file is same).
i am unable to understand what is wrong and how can i correct it.
root is a user too, when you start vim with root, vim load the root's Home/.vimrc usually it is /root/.vimrc
You can cp or ln -s your user's vimrc to /root if this is a personal desktop machine.
I hope I understood your problem.
You did not state a precise question, so for the sake of completeness :
If what you want is being able to modify files as root using your user's vim and .vimrc, you can do
sudo -e /path/to/your/file
sudo will use the editor configured in $EDITOR as the current user, to edit a temporary copy of the file that will get copied over when you write the file.
The caveat is that you will not be able to have any edition history between sessions. For example, if you modify /etc/group once, save the changes and quit, and then reopen the file again, you will not be able to undo the modification you did at first.
For Neovim users, you can do what comes next:
If your root user settings for neovim are important, make a
backup for those dotfiles.
Create a symbolic link from your default user to /root/.config directory.
sudo mv /root/.config/nvim /root/.config/nvim.bkp; sudo ln -s $HOME/.config/nvim /root/.config/
Be aware that some plugins may need some files that are not under
.config/nvim directory.
On Debian GNU/Linux there is also a shared configuration file in /usr/share/vim/vimrc. (Actually this is a soft link to /etc/vim/vimrc.) See the bottom of the man page on vim.

Editing tomcat-users.xml in CentOS 7 Linux

I am trying to edit tomcat-users.xml in Tomcat 8, using the terminal of CentOS 7 Linux. I typed su - to use the terminal as the root account.
I then typed in vi $CATALINA_HOME/conf/tomcat-users.xml, but the result is that vi opens in the window with the contents of tomcat-users.xml in read-only mode, and there seems to be no way out of vi except to close the window, losing all the terminal history.
I also navigated to the physical location of tomcat-users.xml and opened up the file using Gedit, but Gedit will not allow me to save the file. How can I edit tomcat-users.xml?
When you open vi, you are in command mode. Type i to place yourself in insert mode. make your edits then when you are finished use the escape key to toggle back to command mode. Then type :q! to exit without saving or ZZ to save and close. I believe barjomet is correct that as root, :wq! will allow you to write to a read only file.
you might want to investigate why the file permissions are read only. See who owns the file and what the permissions are by using the ls command with the -l option
Example:
ls -l /some/folder/somefile.xml
change the permissions (if you know what you're doing) by using chmod command and change owner by using the chown command (be careful, don't mess up the permissions and create a security flaw, I can't help with that as I don't know tomcat)
You can edit read-only file as root with vi — just type :wq! when you finish.
If you not familiar with vi and have vim installed — I recommend you to run vimtutor and spent about an hour to learn basics, otherwise read this short tutorial.
First thing to edit tomcat-user.xml file you need administrative privilege. If you the admin access. you can use vi or vim to edit the file
when the file opens, to edit the hit key a or i, it will let you edit at the bottom you can see Insert text. After editing to save press esc key can type :wq!

Change vim's current directory upon starting from command line

Vim (actually, gvim is in question, but I believe it works both ways) upon starting has its current directory in its runtime directory.
I would like to upon starting, to have its cd in let's say, c:\pro
I know I can just do :cd c:\pro, but is there a way to change it upon starting it, with
something alike gvim c:\pro (only that doesn't work :-| ?
You can use
gvim -c "cd C:/pro"
-c allows you to run commands just after Vim has finished loading.
Vim's working directory is initially set to the working directory of the process that invokes Vim.
If you're double-clicking on the .exe in Explorer, that's why it's the runtime directory.
If you're using the Start Menu shortcut, then it has the Start in path set to the runtime directory. You could change the Start in path of the shortcut.
If you use the run dialog, then vim's initial working directory is likely your Documents and Settings directcory.
Can't you put cd path in your .vimrc ?

Resources