个人工具

“UbuntuHelp:RootSudo”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第3行: 第3行:
  
  
In Linux (and Unix in general), there is a superuser named root.  The Windows analog of root is Administrator.  The superuser can do anything and everything, and thus doing daily work as the superuser can be dangerous.  You could type a command incorrectly and crash the system.  Ideally, you run as a user that has only the privileges needed for the task at hand.  In some cases, this is necessarily root, but most of the time it is a regular user.
+
In Linux (and Unix in general), there is a superuser named root.  The Windows analog of root is Administrator.  The superuser can do anything and everything, and thus doing daily work as the superuser can be dangerous.  You could type a command incorrectly and destroy the system.  Ideally, you run as a user that has only the privileges needed for the task at hand.  In some cases, this is necessarily root, but most of the time it is a regular user.
  
By default, the root account is locked in Ubuntu. This means you cannot login as root or use su. Instead, the installer will setup sudo to allow the user that is created during install to run all administrative commands.
+
'''By default, the root account password is locked in Ubuntu.''' This means that you cannot login as root directly or use the su command to become the root user, however, since the root account physically exists it is still possible to run programs with root-level privileges. This is where '''sudo''' comes in; it allows authorized users (normally "Administrative" users; for further information please refer to AddUsersHowto) to run certain programs as root without having to know the root password.
 +
 
 +
This means that in the terminal you should use sudo for commands that require root privileges; simply prepend "sudo" to all the commands you would normally run as root. For more extensive usage examples, please see below. Similarly, when you run GUI programs that require root privileges (e.g. the network configuration applet), you will also be prompted for a password. Just remember, when sudo asks for a password, it needs '''YOUR USER Password''', and not the root account password.
 +
 
 +
 
 +
=== Benefits of using sudo ===
 +
 
 +
The benefits of leaving '''root''' logins disabled by default include the following:
 +
 
 +
* The installer has to ask fewer questions.
 +
* Users don't have to remember an extra password (i.e. the root password), which they are likely to forget.
 +
* It avoids the "I can do ''anything''" interactive login by default (e.g. the tendency by users to login as an "Administrator" user in Microsoft Windows systems), you will be prompted for a password before major changes can happen, which should make you think about the consequences of what you are doing.
 +
* Sudo adds a log entry of the command(s) run (In <code><nowiki>/var/log/auth.log</nowiki></code>). If you mess up, you can always go back and see what commands were run. It is also nice for auditing.
 +
* Every cracker trying to ''brute-force'' their way into your box will know it has an account named ''root'' and will try that first. What they don't know is what the usernames of your other users are. Since the root account password is locked, this attack becomes essentially meaningless, since there is no password to crack or guess in the first place.
 +
* Allows easy transfer for admin rights, in a short term or long term period, by adding and removing users from groups, while not compromising the ''root'' account.
 +
* sudo can be setup with a much more fine-grained security policy.
 +
* The authentication automatically expires after a short time (which can be set to as little as desired or 0); so if you walk away from the terminal after running commands as root using sudo, you will not be leaving a root terminal open indefinitely.
  
This means that in the terminal you can use sudo for commands that require root privileges. All programs in the menu will use a graphical sudo to prompt for a password. When sudo asks for a password, it needs '''YOUR USER Password'''; this means that a root password is not needed.
 
  
 
=== Notes ===
 
=== Notes ===
* The password is stored by default for 15 minutes. After that time, you will need to enter your password again.
+
 
* Your password will '''not''' be shown on the screen as you type it, not even as a row of stars (******). It is going in, however!
+
* To run the graphical configuration utilities with <code><nowiki>sudo</nowiki></code>, simply launch the application via the menu.
+
* To run a program using <code><nowiki>sudo</nowiki></code> that normally is run as the user, such as '''gedit''', press Alt+F2 and enter <code><nowiki>gksudo gedit</nowiki></code>.
+
*For users of ''Kubuntu'', use <code><nowiki>kdesu</nowiki></code> in replacement for <code><nowiki>gksudo</nowiki></code>.
+
 
* To use <code><nowiki>sudo</nowiki></code> on the command line, preface the command with <code><nowiki>sudo</nowiki></code>, as below:
 
* To use <code><nowiki>sudo</nowiki></code> on the command line, preface the command with <code><nowiki>sudo</nowiki></code>, as below:
 
''Example #1''
 
''Example #1''
第24行: 第35行:
 
sudo /etc/init.d/networking restart
 
sudo /etc/init.d/networking restart
 
</nowiki></pre>
 
</nowiki></pre>
* '''NEVER''' use sudo to start graphical programs. You should always use <code><nowiki>gksudo</nowiki></code> or <code><nowiki>kdesu</nowiki></code> to run such programs, otherwise new login attempts may fail. If this happens and at login an error message reports: "Unable to read ICE authority file", log in using the failsafe terminal and execute the command below substituting your username for ''user.''
+
* The password is stored by default for 15 minutes. After that time, you will need to enter your password again.
<pre><nowiki>
+
* Your password will '''not''' be shown on the screen as you type it, not even as a row of stars (******). It is being entered with each keystroke!
rm /home/user/.{ICE,X}authority
+
* <code><nowiki>sudo !!</nowiki></code> will repeat the last command entered, except with sudo prepended to it.
</nowiki></pre>
+
 
* To start a ''root shell'' (i.e. a command window where you can run root commands), starting root's environment and login scripts, use:
+
* You should '''never''' use sudo to start graphical applications as root. You should use <code><nowiki>gksudo</nowiki></code> to run such programs (eg. '''ALT+F2''' gksudo gedit).
<pre><nowiki>
+
*** For users of ''Kubuntu'', use <code><nowiki>kdesu</nowiki></code> instead of <code><nowiki>gksudo</nowiki></code>.
sudo -i    (equivalent to sudo su -)
+
* To run the graphical configuration utilities, simply launch the application via the menu.
</nowiki></pre>
+
* To start a ''root shell'', but keep the current shell's environment, use:
+
<pre><nowiki>
+
sudo -s    (equivalent to sudo su)
+
</nowiki></pre>
+
* To login as another user (on the command line, use something like gdmflexiserver for a graphical login)
+
<pre><nowiki>
+
sudo -i -u username
+
</nowiki></pre>
+
  
 
=== Allowing other users to run sudo  ===
 
=== Allowing other users to run sudo  ===
第47行: 第49行:
 
/!\ In the terminal this would be: <code><nowiki>sudo adduser $user admin</nowiki></code>, where you replace $user with the name of the user.
 
/!\ In the terminal this would be: <code><nowiki>sudo adduser $user admin</nowiki></code>, where you replace $user with the name of the user.
  
=== Benefits of using sudo ===
+
=== Logging in as another user ===
  
The benefits of leaving '''root''' disabled by default include the following:
+
* To login as another user.
  
* The installer has to ask fewer questions
+
NB Please don't use this to become root, please see the bottom of the page for some more information.
* Users don't have to remember an extra password, which they are likely to forget
+
 
* It avoids the "I can do ''anything''" interactive login by default -you will be prompted for a password before major changes can happen, which should make you think about the consequences of what you are doing.
+
<pre><nowiki>
* Sudo adds a log entry of the command(s) run (In <code><nowiki>/var/log/auth.log</nowiki></code>). If you mess up, you can always go back and see what commands were run. It is also nice for auditing.
+
sudo -i -u username
* Every cracker trying to ''brute-force'' their way into your box will know it has an account named ''root'' and will try that first. What they don't know is what the usernames of your other users are.
+
</nowiki></pre>
* Allows easy transfer for admin rights, in a short term or long term period, by adding and removing users from groups, while not compromising the ''root'' account.
+
 
* sudo can be setup with a much more fine-grained security policy
+
For example to become amanda for tape management purposes.
* The authentication automatically expires after a short time (which can be set to as little as desired or 0); so if you walk away from the terminal after running commands as root using sudo, you will not be leaving a root terminal open indefinitely.
+
 
 +
<pre><nowiki>
 +
sudo -i -u amanda
 +
</nowiki></pre>
 +
 
 +
The password being asked for is your own.
  
 
=== Downsides of using sudo ===
 
=== Downsides of using sudo ===
第64行: 第71行:
 
Although for desktops the benefits of using sudo are great, there are possible issues which need to be noted:
 
Although for desktops the benefits of using sudo are great, there are possible issues which need to be noted:
  
* Redirecting the output of commands run with sudo can catch new users out. For instance consider <code><nowiki>sudo ls > /root/somefile</nowiki></code> will not work since it is the shell that tries to write to that file. You can use `ls | sudo tee -a /root/somefile` to append, or `ls | sudo tee /root/somefile` to overwrite contents. You could also pass the whole command to a shell process run under sudo to have the file written to with root permissions, such as `sudo bash -c "ls > /root/somefile"`.
+
* Redirecting the output of commands run with sudo requires a different approach. For instance consider <code><nowiki>sudo ls > /root/somefile</nowiki></code> will not work since it is the shell that tries to write to that file. You can use `ls | sudo tee -a /root/somefile` to append, or `ls | sudo tee /root/somefile` to overwrite contents. You could also pass the whole command to a shell process run under sudo to have the file written to with root permissions, such as `sudo bash -c "ls > /root/somefile"`.
 
* In a lot of office environments the ONLY local user on a system is root. All other users are imported using NSS techniques such as nss-ldap. To setup a workstation, or fix it, in the case of a network failure where nss-ldap is broken, root is required. This tends to leave the system unusable unless cracked. An extra local user, or an enabled root password is needed here.
 
* In a lot of office environments the ONLY local user on a system is root. All other users are imported using NSS techniques such as nss-ldap. To setup a workstation, or fix it, in the case of a network failure where nss-ldap is broken, root is required. This tends to leave the system unusable unless cracked. An extra local user, or an enabled root password is needed here.
 +
 +
  
 
=== Misconceptions ===
 
=== Misconceptions ===
第85行: 第94行:
 
Console users have access to the boot loader, and can gain administrative privileges in various ways during the boot process.  For example, by specifying an alternate `init(8)` program.  Linux systems are not typically configured to be secure at the console, and additional steps (for example, setting a root password, a boot loader password and a BIOS password) are necessary in order to make them so.  Note that console users usually have physical access to the machine and so can manipulate it in other ways as well.
 
Console users have access to the boot loader, and can gain administrative privileges in various ways during the boot process.  For example, by specifying an alternate `init(8)` program.  Linux systems are not typically configured to be secure at the console, and additional steps (for example, setting a root password, a boot loader password and a BIOS password) are necessary in order to make them so.  Note that console users usually have physical access to the machine and so can manipulate it in other ways as well.
  
=== Going back to a traditional root account ===
+
=== Re-disabling your root account ===
<!> '''This is not recommended!'''
+
  
Consider using the below command instead:
+
If for some reason you have enabled your root account and wish to disable it again, open a terminal and issue the following command:
 
<pre><nowiki>
 
<pre><nowiki>
sudo -i
+
sudo passwd -l root
 
</nowiki></pre>
 
</nowiki></pre>
  
That will open up a root console temporarily. Enabling the root account in Ubuntu is neither supported nor necessary. Anything you need to do as administrator of an Ubuntu system can be done via sudo or gksudo.
+
=== Enabling the root account ===
 +
<!> '''Enabling the root account is neither supported nor necessary.'''<br>
 +
Anything you need to do as administrator of an Ubuntu system can be done via sudo or gksudo. If you really need a persistent root login, use <code><nowiki>sudo -i</nowiki></code>. Logging in to X as root is most likely to cause very serious trouble. If you believe you need a root account to perform a certain action, please consult the official support channels first, to make sure there is not a better alternative.
 +
 
  
If the root account is enabled and you wish to disable it, open a terminal and issue the following command:
 
<pre><nowiki>
 
sudo passwd -l root
 
</nowiki></pre>
 
  
  
第111行: 第118行:
 
When you drag and drop any file on this launcher (it's useful to put it on the desktop or on a panel), it will be opened as root with its own associated application. This is helpful especially when you're editing config files owned by root, since they will be opened as read only by default with gedit, etc.
 
When you drag and drop any file on this launcher (it's useful to put it on the desktop or on a panel), it will be opened as root with its own associated application. This is helpful especially when you're editing config files owned by root, since they will be opened as read only by default with gedit, etc.
  
 +
=== Special notes on sudo and shells ===
 +
 +
* '''None of the methods below are suggested or supported by the designers of Ubuntu.'''
 +
 +
* Please do not suggest this to others unless you personally are available 24/7 to support the user  if they have issues as a result of running a shell as root.
 +
 +
* To start a ''root shell'' (i.e. a command window where you can run root commands), starting root's environment and login scripts, use:
 +
<pre><nowiki>
 +
sudo -i    (equivalent to sudo su - , gives you roots environment configuration)
 +
</nowiki></pre>
 +
* To start a ''root shell'', but keep the current shell's environment, use:
 +
<pre><nowiki>
 +
sudo -s    (equivalent to sudo su)
 +
</nowiki></pre>
 
----
 
----
[[category:CategorySecurity]]
+
[[category:CategorySecurity]] [[category:CategoryDocumentation]]
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2007年11月21日 (三) 18:08的版本



In Linux (and Unix in general), there is a superuser named root. The Windows analog of root is Administrator. The superuser can do anything and everything, and thus doing daily work as the superuser can be dangerous. You could type a command incorrectly and destroy the system. Ideally, you run as a user that has only the privileges needed for the task at hand. In some cases, this is necessarily root, but most of the time it is a regular user.

By default, the root account password is locked in Ubuntu. This means that you cannot login as root directly or use the su command to become the root user, however, since the root account physically exists it is still possible to run programs with root-level privileges. This is where sudo comes in; it allows authorized users (normally "Administrative" users; for further information please refer to AddUsersHowto) to run certain programs as root without having to know the root password.

This means that in the terminal you should use sudo for commands that require root privileges; simply prepend "sudo" to all the commands you would normally run as root. For more extensive usage examples, please see below. Similarly, when you run GUI programs that require root privileges (e.g. the network configuration applet), you will also be prompted for a password. Just remember, when sudo asks for a password, it needs YOUR USER Password, and not the root account password.


Benefits of using sudo

The benefits of leaving root logins disabled by default include the following:

  • The installer has to ask fewer questions.
  • Users don't have to remember an extra password (i.e. the root password), which they are likely to forget.
  • It avoids the "I can do anything" interactive login by default (e.g. the tendency by users to login as an "Administrator" user in Microsoft Windows systems), you will be prompted for a password before major changes can happen, which should make you think about the consequences of what you are doing.
  • Sudo adds a log entry of the command(s) run (In /var/log/auth.log). If you mess up, you can always go back and see what commands were run. It is also nice for auditing.
  • Every cracker trying to brute-force their way into your box will know it has an account named root and will try that first. What they don't know is what the usernames of your other users are. Since the root account password is locked, this attack becomes essentially meaningless, since there is no password to crack or guess in the first place.
  • Allows easy transfer for admin rights, in a short term or long term period, by adding and removing users from groups, while not compromising the root account.
  • sudo can be setup with a much more fine-grained security policy.
  • The authentication automatically expires after a short time (which can be set to as little as desired or 0); so if you walk away from the terminal after running commands as root using sudo, you will not be leaving a root terminal open indefinitely.


Notes

  • To use sudo on the command line, preface the command with sudo, as below:

Example #1

sudo chown bob:bob /home/bob/*

Example #2

sudo /etc/init.d/networking restart
  • The password is stored by default for 15 minutes. After that time, you will need to enter your password again.
  • Your password will not be shown on the screen as you type it, not even as a row of stars (******). It is being entered with each keystroke!
  • sudo !! will repeat the last command entered, except with sudo prepended to it.
  • You should never use sudo to start graphical applications as root. You should use gksudo to run such programs (eg. ALT+F2 gksudo gedit).
      • For users of Kubuntu, use kdesu instead of gksudo.
  • To run the graphical configuration utilities, simply launch the application via the menu.

Allowing other users to run sudo

To add a new user to sudo, open the Users and Groups tool from System --> Administration menu. Then click on the user and then on properties. Choose the User Privileges tab. In the tab, find Executing system administration tasks and check that.

/!\ In the terminal this would be: sudo adduser $user admin, where you replace $user with the name of the user.

Logging in as another user

  • To login as another user.

NB Please don't use this to become root, please see the bottom of the page for some more information.

sudo -i -u username

For example to become amanda for tape management purposes.

sudo -i -u amanda

The password being asked for is your own.

Downsides of using sudo

Although for desktops the benefits of using sudo are great, there are possible issues which need to be noted:

  • Redirecting the output of commands run with sudo requires a different approach. For instance consider sudo ls > /root/somefile will not work since it is the shell that tries to write to that file. You can use `ls | sudo tee -a /root/somefile` to append, or `ls | sudo tee /root/somefile` to overwrite contents. You could also pass the whole command to a shell process run under sudo to have the file written to with root permissions, such as `sudo bash -c "ls > /root/somefile"`.
  • In a lot of office environments the ONLY local user on a system is root. All other users are imported using NSS techniques such as nss-ldap. To setup a workstation, or fix it, in the case of a network failure where nss-ldap is broken, root is required. This tends to leave the system unusable unless cracked. An extra local user, or an enabled root password is needed here.


Misconceptions

  • Isn't sudo less secure than su?

The basic security model is the same, and therefore these two systems share their primary weaknesses. Any user who uses `su` or `sudo` must be considered to be a privileged user. If that user's account is compromised by an attacker, the attacker can also gain root privileges the next time the user does so. The user account is the weak link in this chain, and so must be protected with the same care as root.

On a more esoteric level, `sudo` provides some features which encourage different work habits, which can positively impact the security of the system. `sudo` is commonly used to execute only a single command, while `su` is generally used to open a shell and execute multiple commands. The `sudo` approach reduces the likelihood of a root shell being left open indefinitely, and encourages the user to minimize their use of root privileges.

  • I won't be able to enter single-user mode!

The sulogin program in Ubuntu is patched to handle the default case of a locked root password.

  • I can get a root shell from the console without entering a password!

You have to enter your password.

Console users have access to the boot loader, and can gain administrative privileges in various ways during the boot process. For example, by specifying an alternate `init(8)` program. Linux systems are not typically configured to be secure at the console, and additional steps (for example, setting a root password, a boot loader password and a BIOS password) are necessary in order to make them so. Note that console users usually have physical access to the machine and so can manipulate it in other ways as well.

Re-disabling your root account

If for some reason you have enabled your root account and wish to disable it again, open a terminal and issue the following command:

sudo passwd -l root

Enabling the root account

<!> Enabling the root account is neither supported nor necessary.
Anything you need to do as administrator of an Ubuntu system can be done via sudo or gksudo. If you really need a persistent root login, use sudo -i. Logging in to X as root is most likely to cause very serious trouble. If you believe you need a root account to perform a certain action, please consult the official support channels first, to make sure there is not a better alternative.



Drag & Drop Sudo

This is a trick from the forums.

Create a launcher with the following command:

gksudo "gnome-open %u"

When you drag and drop any file on this launcher (it's useful to put it on the desktop or on a panel), it will be opened as root with its own associated application. This is helpful especially when you're editing config files owned by root, since they will be opened as read only by default with gedit, etc.

Special notes on sudo and shells

  • None of the methods below are suggested or supported by the designers of Ubuntu.
  • Please do not suggest this to others unless you personally are available 24/7 to support the user if they have issues as a result of running a shell as root.
  • To start a root shell (i.e. a command window where you can run root commands), starting root's environment and login scripts, use:
sudo -i     (equivalent to sudo su - , gives you roots environment configuration)
  • To start a root shell, but keep the current shell's environment, use:
sudo -s     (equivalent to sudo su)