By default, Ubuntu doesn’t allow the user to log in as “root” (administrator). However, some tasks, such as installation of software or editing system files, require root privileges.
The sudo command allows authorized users (generally users in the Administrator group) to run certain programs and commands as root in a Terminal window without having to know the root password. It is an abbreviation for “substitute user do” (as in, do a command with another user’s privileges).
Now, say you’ve typed a long command that requires sudo to work, but you forgot to type sudo at the beginning of the line. You probably end up with an error or end up editing a file in read-only mode. There is a quick and easy way to repeat the last command entered at the prompt with sudo prepended to it, without having to type the whole command again.
To open a Terminal window, select Accessories | Terminal from the Applications menu.
As an example, we will open the sources.list file in vi for editing. Maybe we need to add a repository to the list. This is a system file for which you need root privileges to edit. Enter the following line at the prompt in the Terminal window and press Enter.
vi /etc/apt/sources.list
Notice that the file opens in vi. However, it is opened in vi, but is read-only and cannot be edited, as indicated by [readonly] at the bottom of the screen.
To exit vi without saving the file, type the following and press Enter.
:q!
Now, we will open the file with root privileges so we can edit it. To do this, type the following at the prompt in the Terminal window and press Enter.
sudo !!
The previous command is repeated on the next line preceded by sudo. Enter your user password when prompted.
The sources.list file is opened in vi again, and is now editable.
Alternatively, you could press the up arrow key to enter the last typed command at the prompt, press either the Home key or Ctrl + A, and then type sudo.
No comments:
Post a Comment