linux中sudo与su_Linux中的Sudo和Su有什么区别?

linux中sudo与su

linux中sudo与su

image

If you’re a Linux user, you’ve probably seen references to both sudo and su. Articles here on How-To Geek and elsewhere instruct Ubuntu users to use sudo and other Linux distributions’ users to use su, but what’s the difference?

如果您是Linux用户,则可能已经看到了对sudo和su的引用。 关于How-To Geek和其他地方的文章指示Ubuntu用户使用sudo和其他Linux发行版的用户使用su,但是有什么区别?

Sudo and su are two different ways to gain root privileges. Each functions in a different way, and different Linux distributions use different configurations by default.

Sudo和su是获得root特权的两种不同方法。 每个功能都有不同的方式,默认情况下,不同Linux发行版使用不同的配置。

根用户 (The Root User)

Both su and sudo are used to run commands with root permissions. The root user is basically equivalent to the administrator user on Windows – the root user has maximum permissions and can do anything to the system. Normal users on Linux run with reduced permissions – for example, they can’t install software or write to system directories.

su和sudo均用于以root权限运行命令。 root用户基本上等效于Windows上的管理员用户– root用户具有最大权限,并且可以对系统执行任何操作。 Linux上的普通用户以较低的权限运行–例如,他们不能安装软件或写入系统目录。

To do something that requires these permissions, you’ll have to acquire them with su or sudo.

要执行需要这些权限的操作,您必须使用su或sudo获得它们。

Su vs. Sudo (Su vs. Sudo)

The su command switches to the super user – or root user – when you execute it with no additional options. You’ll have to enter the root account’s password. This isn’t all the su command does, though – you can use it to switch to any user account. If you execute the su bob command, you’ll be prompted to enter Bob’s password and the shell will switch to Bob’s user account.

执行su命令时,如果没有其他选项,su命令将切换到超级用户或root用户。 您必须输入root帐户的密码。 但是,这并不是su命令的全部功能–您可以使用它切换到任何用户帐户。 如果执行su bob命令,系统将提示您输入Bob的密码,外壳程序将切换到Bob的用户帐户。

Once you’re done running commands in the root shell, you should type exit to leave the root shell and go back to limited-privileges mode.

在根shell中运行命令完成后,应键入exit离开根shell并返回到有限特权模式。

Sudo runs a single command with root privileges. When you execute sudo command, the system prompts you for your current user account’s password before running command as the root user. By default, Ubuntu remembers the password for fifteen minutes and won’t ask for a password again until the fifteen minutes are up.

Sudo使用root特权运行一个命令。 当执行sudo command时 ,系统会提示您输入当前用户帐户的密码,然后再以root用户身份运行command 。 默认情况下,Ubuntu会记住密码十五分钟,直到十五分钟后才会再次要求输入密码。

image

This is a key difference between su and sudo. Su switches you to the root user account and requires the root account’s password. Sudo runs a single command with root privileges – it doesn’t switch to the root user or require a separate root user password.

这是su和sudo之间的关键区别。 Su将您切换到root用户帐户,并需要root帐户的密码。 Sudo使用root特权运行一个命令-它不会切换到root用户,也不需要单独的root用户密码。

Ubuntu与其他Linux发行版 (Ubuntu vs. Other Linux Distributions)

The su command is the traditional way of acquiring root permissions on Linux. The sudo command has existed for a long time, but Ubuntu was the first popular Linux distribution to go sudo-only by default. When you install Ubuntu, the standard root account is created, but no password is assigned to it. You can’t log in as root until you assign a password to the root account.

su命令是在Linux上获取root权限的传统方法。 sudo命令已经存在很长时间了,但是Ubuntu是第一个默认情况下仅使用sudo的流行Linux发行版。 当您安装Ubuntu时,将创建标准的根帐户,但未为其分配密码。 您必须先为root帐户分配密码,然后才能以root用户身份登录。

image

There are several advantages to using sudo instead of su by default. Ubuntu users only have to provide and remember a single password, whereas Fedora and other distributions require you create separate root and user account passwords during installation.

默认情况下,使用sudo而不是su有许多优点。 Ubuntu用户仅需提供并记住一个密码,而Fedora和其他发行版则要求您在安装过程中分别创建root和用户帐户密码。

Another advantage is that it discourages users from logging in as the root user – or using su to get a root shell – and keeping the root shell open to do their normal work. Running fewer commands as root increases security and prevents accidental system-wide changes.

另一个优点是,它阻止用户以root用户身份登录-或使用su获取root shell-并保持root shell打开以执行其正常工作。 以root身份运行较少的命令可以提高安全性并防止系统范围内的意外更改。

Distributions based on Ubuntu, including Linux Mint, also use sudo instead of su by default.

默认情况下,基于Ubuntu的发行版(包括Linux Mint)也使用sudo而不是su。

一些技巧 (A Few Tricks)

Linux is flexible, so it doesn’t take much work to make su work similarly to sudo – or vice versa.

Linux是灵活的,因此使su与sudo类似地工作不需要太多工作,反之亦然。

To run a single command as the root user with su, run the following command:

要以root用户身份使用su运行单个命令,请运行以下命令:

su -c ‘command’

su -c'命令'

This is similar to running a command with sudo, but you’ll need the root account’s password instead of your current user account’s password.

这类似于使用sudo运行命令,但是您将需要root帐户的密码,而不是当前用户帐户的密码。

To get a full, interactive root shell with sudo, run sudo –i.

要获得具有sudo的完整的交互式root shell,请运行sudo –i。

image

You’ll have to provide your current user account’s password instead of the root account’s password.

您必须提供当前用户帐户的密码,而不是root帐户的密码。

在Ubuntu中启用root用户 (Enabling the Root User in Ubuntu)

To enable the root user account on Ubuntu, use the following command to set a password for it. Bear in mind that Ubuntu recommends against this.

要在Ubuntu上启用root用户帐户,请使用以下命令为其设置密码。 请记住,Ubuntu建议不要这样做。

sudo passwd root

须藤passwd根

Sudo will prompt you for your current user account’s password before you can set a new password. Use your new password to log in as root from a terminal login prompt or with the su command. You should never run a full graphical environment as the root user – this is a very poor security practice, and many programs will refuse to work.

在设置新密码之前,Sudo会提示您输入当前用户帐户的密码。 使用新密码从终端登录提示或su命令以root用户身份登录。 您绝对不应以root用户身份运行完整的图形环境-这是非常差的安全性做法,许多程序都会拒绝运行。

image

将用户添加到Sudoers文件 (Adding Users to the Sudoers File)

Only administrator-type accounts in Ubuntu can run commands with sudo. You can change a user account’s type from the User Accounts configuration window.

Ubuntu中只有管理员类型的帐户才能使用sudo运行命令。 您可以从“用户帐户”配置窗口中更改用户帐户的类型。

image

Ubuntu automatically designates the user account created during installation as an administrator account.

Ubuntu会自动将在安装过程中创建的用户帐户指定为管理员帐户。

image

If you’re using another Linux distribution, you can grant a user permission to use sudo by running the visudo command with root privileges (so run su first or use su -c).

如果使用的是其他Linux发行版,则可以通过以root特权运行visudo命令来授予用户使用sudo的权限(因此请先运行su或使用su -c )。

Add the following line to the file, replacing user with the name of the user account:

将以下行添加到文件中,将user替换为用户帐户的名称:

user    ALL=(ALL:ALL) ALL

用户ALL =(ALL:ALL)ALL

Press Ctrl-X and then Y to save the file. You may also be able to add a user to a group specified in the file. Users in the groups specified in the file will automatically have sudo privileges.

Ctrl-X ,然后按Y保存文件。 您也可以将用户添加到文件中指定的组中。 文件中指定的组中的用户将自动具有sudo特权。

image

Su的图形版本 (Graphical Versions of Su)

Linux also supports graphical versions of su, which ask for your password in a graphical environment. For example, you can run the following command to get a graphical password prompt and run the Nautilus file browser with root permissions. Press Alt-F2 to run the command from a graphical run dialog without launching a terminal.

Linux还支持su的图形版本,该版本在图形环境中要求您输入密码。 例如,您可以运行以下命令以获取图形密码提示,并以root用户权限运行Nautilus文件浏览器。 按Alt-F2键可从图形运行对话框运行命令,而无需启动终端。

gksu nautilus

鹦鹉螺

image

The gksu command also has a few other tricks up its sleeve – it preserves your current desktop settings, so graphical programs won’t look out of place when you launch them as a different user. Programs such as gksu are the preferred way of launching graphical applications with root privileges.

gksu命令还具有其他一些技巧–它保留了您当前的桌面设置,因此当您以其他用户身份启动图形程序时,它们不会显得格格不入。 诸如gksu之类的程序是使用root特权启动图形应用程序的首选方式。

Gksu uses either a su or sudo-based backend, depending on the Linux distribution you’re using.

Gksu使用基于su或sudo的后端,具体取决于您使用Linux发行版。



You should now be prepared to encounter both su and sudo! You’ll encounter both if you use different Linux distributions.

您现在应该准备同时遇到su和sudo! 如果您使用不同Linux发行版,那么您将遇到两者。

翻译自: https://www.howtogeek.com/111479/htg-explains-whats-the-difference-between-sudo-su/

linux中sudo与su

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值