Showing posts with label sudo. Show all posts
Showing posts with label sudo. Show all posts

2009-07-12

Re-issue Previous Command as Root

Here's a quick Bash tip. Don't you just hate it when you key in a command at the terminal and press enter only to realize that you didn't preface the command with sudo? Well, have no fear, because the command !! will re-issue the previous command.

Now, I know you're probably thinking, "what good does that do me? I can just press the up arrow and then hit enter to run the last command." And you'd be right; however, what if you wanted to re-issue the previous command, but you wanted to put sudoat the beginning of it? Well, you could simply press the up arrow and then move the cursor to the beginning of the prompt and then key in sudo and press the enter key, or you could just do this:

$ sudo !!

The time that this happens to me most frequently is when I want to edit a file, for example, if I wanted to add a new repository to my /etc/apt/sources.list file, and run the following by mistake:

$ vim /etc/apt/sources.list

This will open up the sources.list file as read only, a fact which becomes perfectly clear once you change to insert mode in vim, with a Warning: changing a readonly file warning across the bottom of the window. At this point, it's easy to close the file, and re-open it as root with:

$ sudo !!

Well, that pretty much does it for tonight. See you next time.

2009-05-09

Bonus Post: Using sudo in Debian

If you're familiar with Ubuntu or other Linux distros, you may be familiar with the sudo command. If you're not familiar with sudo, this is a command that lets you run a command as another user. Typically, on home computers, this is used to allow you to run commands as the root user, rather than having to log in as the root user. You may notice that, by default, your user is not in the sudoers file. Personally, I like sudo, so I'm going to add my user to the sudoers file.

In order to edit the sudoers file, we have to be logged in as root. Let's pull up a terminal session and run the following command:

$ su

This will prompt you for the root password and then you should notice you're at a # prompt rather than a $ prompt. If you try to edit the /etc/sudoers file right now, you'll notice that it does not allow any user write access. This is to prevent user error when editing this file by formatting things incorrectly or something. The way you should edit this file is with the visudo command. It is possible to edit this with some other commands, but it is not recommended. To open up the file for editing, we should run the following command:

# visudo

You should see a line in the file that looks like:

root ALL=(ALL) ALL

To get the same behaviour you may be used to seeing in Ubuntu or other Linux distros, you'll just want to add a line almost identical to the above line, only substituting your user name for "root":

grabag-linux ALL=(ALL) ALL

To save the changes, hit Ctrl + x, this will prompt you with a question asking to save the changes. Answer "y" to save the changes, and this will prompt you for the location of the file to save. By default, it should populate the file name /etc/sudoers.tmp, just delete the .tmp off the end of the file name to save it to the correct location. This will prompt you saying that the file already exists and asks if you want to overwrite the file; answer "y" to save and quit visudo. Now you can exit your root session and test out your new sudo authority:

# exit
$ sudo apt-get update

That does it for this bonus post. I'll be back tomorrow with the regularly scheduled post on configuring the wireless adapter on the Mini 9.