ubuntu sudo,linux中sudo命令_Linux中的Sudo命令

 2023-11-19 阅读 29 评论 0

摘要:linux中sudo命令Sudo is one of the most widely used commands by Linux administrators and beginners alike. In this guide, we will look at sudo command and its usages. Sudo是Linux管理員和初學者最廣泛使用的命令之一。 在本指南中,我們將介紹sudo命令及其用

linux中sudo命令

Sudo is one of the most widely used commands by Linux administrators and beginners alike. In this guide, we will look at sudo command and its usages.

Sudo是Linux管理員和初學者最廣泛使用的命令之一。 在本指南中,我們將介紹sudo命令及其用法。

During the installation of most Linux Systems, one is usually required to create a root user, also known as a superuser and a regular user as well. The root user has all the privileges and permissions to perform any task in the system. As a root user, you can perform sensitive tasks such as installing/uninstalling packages, updating/upgrading system repositories, making modifications to system configurations and creating new users to mention just but a few.

在大多數Linux系統的安裝過程中,通常需要創建一個root用戶(也稱為超級用戶和普通用戶)。 超級用戶具有執行系統中任何任務的所有特權和權限。 作為root用戶,您可以執行敏感任務,例如安裝/卸載軟件包,更新/升級系統存儲庫,修改系統配置以及創建新用戶(僅舉幾例)。

ubuntu sudo。As the saying goes, with great power comes great responsibility. Making some changes as the root user can be detrimental to your system and can cause irreparable damage and ultimate crashing of your system. For example, deleting boot configuration files will render your system unable to boot. For this reason, it’s recommended that by default, users should operate the system as a regular or non-root user.

俗話說,能力越大,責任就越大。 以root用戶身份進行一些更改可能對您的系統有害,并且可能導致無法彌補的損害并最終導致系統崩潰。 例如,刪除啟動配置文件將使您的系統無法啟動。 因此,建議默認情況下,用戶應以普通用戶或非root用戶身份操作系統。



So what happens when you need root privileges as a regular user to execute administrative tasks? In that case, the regular user needs to be granted sudo privileges. The regular user then needs to prefix the command with ‘sudo’ to execute them as a root user.

那么,當您需要普通用戶的root特權來執行管理任務時會發生什么呢? 在這種情況下,需要向普通用戶授予sudo特權。 然后,普通用戶需要在命令前加上“ sudo”前綴,以root用戶身份執行它們。

在Ubuntu / Debian中創建新的sudo用戶 (Creating a new sudo user in Ubuntu/Debian)

To add the user to the sudo group execute the following command as the root user.

要將用戶添加到sudo組,請以root用戶身份執行以下命令。

usermod -aG  sudo username

linux基本命令的使用、For example, let’s assume we have a regular user named “james” in our systems, the command will be:

例如,假設我們的系統中有一個名為“ james”的普通用戶,該命令將為:

usermod -aG sudo james

Output

輸出量

在RHEL / CentOS / Fedora中創建新的sudo用戶 (Creating a new sudo user in RHEL/CentOS/Fedora)

To add a regular user to the sudoers group in RHEL/CentOS and Fedora, we have to modify the /etc/sudoers file. To achieve this run visudo command.

要將常規用戶添加到RHEL / CentOS和Fedora中的sudoers組,我們必須修改/etc/sudoers文件。 要實現此運行visudo命令。

visudo

linux 刪除文件命令、Output

輸出量

Append the line below and replace the username attribute with your real username.

追加以下行,并用您的真實用戶名替換username屬性。

username ALL = (ALL) ALL

Output

輸出量

linux命令速查手冊,Save by pressing ESC and typing : wq and finally press ‘ENTER’.

通過按ESC并輸入: wq保存,最后按“ ENTER”。

Sudo命令示例 (Sudo Command Examples)

Let’s now delve in and have a look at example usages of the sudo command.

現在讓我們深入研究一下sudo命令的示例用法。

以普通用戶身份運行具有管理特權的命令 (To run commands with administrative privileges as a regular user)

If you are running as a normal non-root user, prefix the command with sudo as shown.

如果您以普通的非root用戶身份運行,請為命令添加sudo前綴,如圖所示。

sudo command_to_execute

linux、For example, to update the system in Ubuntu & Debian as a regular user run

例如,以常規用戶身份在Ubuntu&Debian中更新系統

sudo apt update

You will be prompted for the password. Provide your password and hit ‘ENTER’. Note that you need to provide your password and not root password, even though you are running the command as a root user.

系統將提示您輸入密碼。 提供您的密碼并點擊“ ENTER”。 請注意,即使您以root用戶身份運行命令,也需要提供密碼而不是root密碼。

Output

輸出量

sudo bash -c bash、For Fedora Systems execute:

對于Fedora系統,執行:

sudo dnf update

Output

輸出量

For RHEL/CentOS execute:

對于RHEL / CentOS,執行:

sudo yum update

清除sudo緩存 (Clearing sudo cache)

sudo命令不能用、When a user tries to execute any command with sudo after the first time, they will not be prompted for a password in subsequent operations. This is because the sudo command is cached for only a few minutes before expiring.

當用戶第一次嘗試使用sudo執行任何命令時,在后續操作中將不會提示他們輸入密碼。 這是因為sudo命令在過期前僅緩存了幾分鐘。

To clear this cache run:

要清除此緩存運行:

sudo -k

If you try to execute any operation after running the above command, you will be prompted for a password.

如果您在運行上述命令后嘗試執行任何操作,將提示您輸入密碼。

sudo -i命令,Output

輸出量

查看允許的命令 (View commands allowed )

If you are a little curious and wish to know the commands you can execute, simply run

如果您有點好奇并希望知道可以執行的命令,只需運行

sudo -l

Output

輸出量

在Ubuntu系統中切換到root用戶 (Switching to root user in Ubuntu Systems)

linux創建sudo用戶,By default, Ubuntu Systems do not ship in with a root user. To execute administrative tasks, you must first switch to root first and proceed with the execution of commands.

默認情況下,Ubuntu Systems不附帶root用戶。 要執行管理任務,必須首先首先切換到root并繼續執行命令。

To change to root user in Ubuntu systems execute:

要在Ubuntu系統中更改為root用戶,請執行:

sudo su

使用sudo以其他用戶身份運行命令 (Run commands as another user with sudo)

Sudo command can also allow you to run commands as another user. The syntax for achieving this is

Sudo命令還可以允許您以其他用戶身份運行命令。 實現此目的的語法是

sudo -u username command_to_be_run

linux編輯命令、For example;

例如;

sudo -u james whoami

Output

輸出量

For more usages of the sudo command simply run the command below to access its help page.

有關sudo命令的更多用法,只需運行以下命令以訪問其幫助頁面。

man sudo

我們可以使用sudo命令更改root密碼嗎? (Can we change root password using sudo command?)

ubuntu安裝sudo命令。As a matter of fact, Yes we can change the root password by running following command. We can change any user password by running passwd with sudo privilege.

實際上,是的,我們可以通過運行以下命令來更改root密碼。 我們可以通過使用sudo特權運行passwd來更改任何用戶密碼。

$ sudo passwd root

It will ask for the new password for the root user. Just provide the new password and root password will be changed.

它將要求root用戶輸入新密碼。 只要提供新密碼,root密碼就會更改。

Sudo重定向輸出 (Sudo Redirect Output)

Sometimes we have to redirect the output to a location where we don’t have direct access. We can’t use sudo command as is to perform this because our shell does the redirection and it doesn’t have access to that directory.

有時,我們必須將輸出重定向到沒有直接訪問權限的位置。 我們不能按原樣使用sudo命令來執行此操作,因為我們的shell會執行重定向,并且它無權訪問該目錄。

$ sudo ls /root > /root/ls.out
-bash: /root/ls.out: Permission denied
$

sudo是什么命令、There are a few alternative ways to perform this. First one is to start a new shell with sudo and pass the command to it using -c option. This new shell will have sudo access to write to the specified location.

有幾種替代方法可以執行此操作。 第一個是使用sudo啟動新的shell,然后使用-c選項將命令傳遞給它。 這個新的shell將具有sudo訪問權限以寫入指定位置。

$ sudo sh -c 'ls /root > /root/ls.out'

There are some other alternatives too. You can create a shell script with the command and then execute it with sudo.

也有其他選擇。 您可以使用該命令創建Shell腳本,然后使用sudo執行它。

$ cat ls1.sh 
#!/bin/sh
ls /root > /root/ls1.out
$ sudo ls1.sh

We can also launch a shell with sudo -s command and then run these redirection commands.

我們還可以使用sudo -s命令啟動shell,然后運行這些重定向命令。

如何找到sudoers用戶列表? (How to find the sudoers users list?)

wget命令。The list of users having “sudo” privilege is present in “/etc/group” file.

具有“ sudo”特權的用戶列表位于“ / etc / group”文件中。

$ cat /etc/group | grep -P '^sudo'
sudo:x:27:pankaj
$

The last part of the output contains the users’ list having sudo access. You can modify the command further to print only the list of users.

輸出的最后一部分包含具有sudo訪問權限的用戶列表。 您可以進一步修改命令以僅打印用戶列表。

$ cat /etc/group | grep -P '^sudo' | cut -d: -f4
pankaj
$

sudo和su命令之間的區別 (Differences between sudo and su command)

There exists some misconception about the usage of sudo and su commands. While some think they can be used interchangeably, a few subtle differences exist between the two.

關于sudosu命令的使用存在一些誤解。 盡管有些人認為它們可以互換使用,但是兩者之間存在一些細微的差異。

linux sudo命令詳解。sudo command allows a regular user to execute or perform system-level commands that are a special reserve for the root user. sudo is prefixed before command and prompts the regular user’s password before the command can be executed. Also, for the command to be executed, the user needs to be added to the sudoers group.

sudo命令允許普通用戶執行或執行系統級命令,這些命令是root用戶的特殊保留。 sudo在命令之前添加前綴,并在執行命令之前提示普通用戶的密碼。 另外,要執行命令,需要將用戶添加到sudoers組。

On the other hand, su command, enables switching entirely from a regular user to a root user. In this case, you need to provide the root password to enter into the root prompt.

另一方面, su命令可讓您完全從常規用戶切換為root用戶。 在這種情況下,您需要提供root密碼才能輸入root提示符。

In unique cases like in Ubuntu distros, a root user is created without a password. This is meant to discourage anyone from logging in as root. Thus, a password needs to be created for the root user. However, the two commands are used together when a regular user needs to get into root prompt and perform administration tasks as shown.

在像Ubuntu發行版這樣的獨特情況下,將創建一個沒有密碼的root用戶。 這是為了阻止任何人以root用戶身份登錄。 因此,需要為root用戶創建密碼。 但是,當普通用戶需要進入root提示符并執行如圖所示的管理任務時,這兩個命令會一起使用。

pankaj@ubuntu:~$ su -
Password: 
root@ubuntu:~# exit
pankaj@ubuntu:~$

linux chown命令。We have to provide the root password above, not the user password. We can also switch to any other user.

我們必須提供上面的root密碼,而不是用戶密碼。 我們還可以切換到任何其他用戶。

pankaj@ubuntu:~$ su - james
Password: 
james@ubuntu:~# exit
pankaj@ubuntu:~$
pankaj@ubuntu:~$ sudo su
[sudo] password for pankaj: 
root@ubuntu:/home/pankaj#

The above command prompts the user for their password and drops into the root prompt.

上面的命令提示用戶輸入密碼,并進入根提示。

結論 (Conclusion)

When a user has sudo privilege, he can run any command. So, make sure you are providing sudo access to only those users who actually need it, such as system administrators.

當用戶具有sudo特權時,他可以運行任何命令。 因此,請確保僅向真正需要它的用戶(例如系統管理員)提供sudo訪問。

linux tar命令、翻譯自: https://www.journaldev.com/25058/sudo-command-in-linux

linux中sudo命令

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/182920.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息