Home System Tutorial LINUX How to solve Linux command line error prompts: Use sudo command

How to solve Linux command line error prompts: Use sudo command

Jan 15, 2024 pm 04:42 PM
linux linux tutorial Red Hat linux system linux command linux certification red hat linux linux video

Have you ever gotten a "Permission denied" error message when using the Linux command line? This may be because you are trying to perform an operation that requires root privileges. For example, the screenshot below shows the error that occurred when I tried to copy a binary file to a system directory.

How to solve Linux command line error prompts: Use sudo command

Access Denied for shell

So how to solve this error? It's very simple, use the sudo command.

How to solve Linux command line error prompts: Use sudo command

Run the command with sudo

Users will be prompted to enter their (own) login password after running this command. Once the correct password is entered, the operation will be executed successfully.

There is no doubt that sudo is a command that anyone who uses the command line on Linux must know. However, in order to use this command more responsibly and effectively, you still need to know some relevant (and in-depth) details. This is exactly what we will discuss in this article.

Before we continue, it’s worth mentioning that all command instructions mentioned in this article have been tested under Bash version 4.3.11 under Ubuntu 14.04 LTS.

What is sudo

As most of you know, sudo is used to execute commands that require elevated privileges (usually as the root user). One such example has been discussed earlier in this article in the introductory section. However, you can use sudo to run commands as another (non-root) user if you want.

This is achieved by the -u command line option provided by the tool. For example, as shown in the following example, I (himanshu) tried to rename a file in the Home directory of another user (howtoforge), but got an "Access Denied" error. Then I added sudo -u howtoforge and used the same "mv" command, and the command was successfully executed:

How to solve Linux command line error prompts: Use sudo command

What is sudo

Can anyone use sudo?

no. For a user to be able to use sudo, there should be a piece of information related to the user in the /etc/sudoers file. The following excerpt from the Ubuntu website can make it clearer:

The

/etc/sudoers file controls who can run what commands on which machine as which user, and can also control special situations, such as whether a password is required for a specific command. This file consists of aliases (basic variables) and user specifications (controls who can run what commands).

If you are using Ubuntu, enabling a user to run sudo commands is easy: all you need to do is change the account type to administrator. This can be done directly in System Settings -> User Accounts.

How to solve Linux command line error prompts: Use sudo command

sudo user

First unlock the window:

How to solve Linux command line error prompts: Use sudo command

unlocking window

Then select the user you want to change the user type, and then change the type to administrator administrator.

How to solve Linux command line error prompts: Use sudo command

choose sudo accounts

However, if you are not using Ubuntu, or your distribution does not provide this feature, you can manually edit the /etc/sudoers file to implement this change. To add a line like this to the file:

[user]    ALL=(ALL:ALL) ALL

Needless to say, [user] should be replaced with the username of the user you want to increase sudo privileges. One important thing worth mentioning here is that it is officially recommended to edit the file via visudo command - all you need to do is run the following command:

sudo visudo

To explain what is going on, here is an excerpt from the visudo manual:

visudo 以安全的模式编辑 sudoers 文件。visudo 锁定 sudoers 文件以防多个编辑同时进行,提供基本的检查(sanity checks)和语法错误检查。如果 sudoers 文件现在正在被编辑,你将会收到一个信息提示稍后再试。

关于 visudo 的更多信息,前往这里。

什么是 sudo 会话

如果你经常使用 sudo 命令,你肯定注意到过当你成功输入一次密码后,可以不用输入密码再运行几次 sudo 命令。但是一段时间后,sudo 命令会再次要求你的密码。

这种现象跟运行 sudo 命令数目无关,跟时间有关。是的,sudo 默认在输入一次密码后 15 分钟内不会再次要求密码。15 分钟后,你会再次被要求输入密码。

然而,如果你想的话,你能改变这种现象。用以下命令打开 /etc/sudoers 文件:

sudo visudo

找到这一行:

Defaults env_reset

How to solve Linux command line error prompts: Use sudo command

env_reset

然后在这行最后添加以下变量:

Defaults env_reset,timestamp_timeout=[new-value]

[new-value] 为想要 sudo 会话持续的时间数。例如,设数值为 40。

How to solve Linux command line error prompts: Use sudo command

sudo timeout value

如果你希望每次使用 sudo 命令时都要求输入密码,你可以把这个变量赋值为 0 。想要 sudo 会话永远不过时,应赋值为 -1。

注意将 timestamp_timeout 的值赋为 “-1” 是强烈不推荐的。

sudo 密码

你可能注意过,当 sudo 要求输入密码然后你开始输入时,不会显示任何东西 —— 甚至连常规的星号都没有。虽然这不是什么大问题,不过一些用户就是希望显示星号。

好消息是那有可能也很容易做到。所有你需要做的就是在 /etc/sudoers 文件里将下述的行:

Defaults env_reset

改成

Defaults env_reset,pwfeedback

然后保存文件。

现在,无论什么时候输入 sudo 密码,星号都会显示。

How to solve Linux command line error prompts: Use sudo command

hide the sudo password

一些重要的 sudo 命令行参数

除了 -u 命令行参数(我们已经在这篇教程的开始部分讨论过了),还有其他重要的 sudo 命令行参数值得注意。在这部分,我们将会讨论其中一些。

-k 参数

考虑下这种情况:输入密码后你刚刚运行了几个 sudo 驱动的命令。现在,如你所知,sudo 会话默认保持 15 分钟。假设在这会话期间,你需要让某些人访问你的终端,但你不想让他们可以使用 sudo ,你将会怎么做?

还好,有 -k 命令行参数允许用户取消 sudo 权限。这是 sudo 帮助页面(man page)对此的解释:

-k, --reset-timestamp

不带任何命令使用时,撤销用户缓存的凭据。换句话讲,下一次使用 sudo 将会要求输入密码。使用这个参数不需要密码,也可以放到一个 .logout 文件中来撤销 sudo 权限。

当与一个命令,或者一个可能需要密码的操作一起用时,这个参数将会导致 sudo 忽略用户缓存的凭据。结果是 sudo 要求输入密码(如果这是被安全策略所要求的),而且不会更新用户缓存的凭据。

-s 参数

有时你的工作要求你运行一堆需要 root 权限的命令,你不想每次都输入密码。你也不想通过改变 /etc/sudoers 文件调整 sudo 会话的过期时限。

这种情况下,你可以用 sudo 的 -s 参数。这是 sudo 帮助页面对此的解释:

-s, --shell

If the SHELL environment variable is set or the shell is specified by calling the user's password database, the shell will be run. If a command is specified, the command will be passed to the shell for execution via the shell's -c parameter. If no command is specified, an interactive shell is executed.

So, basically what this command parameter does is:

Start a new shell - As for which shell, refer to the SHELL environment variable assignment. If $SHELL is empty, the shell defined in /etc/passwd will be used.
If you pass a command name with the -s parameter (such as sudo -s whoami), what is actually executed is sudo /bin/bash -c whoami.
If you try to execute no other commands (that is, you just run sudo -s), you will get an interactive shell with root privileges.

Please remember that the -s command line parameter gives you a shell with root privileges, but it is not a root environment - it is still executing your own .bashrc. For example, in a new shell run with sudo -s, executing the whoami command will still return your username, not root.

-i parameters

The -i parameter is similar to the -s parameter we discussed. However, there is a difference. One important difference is that -i gives you a root environment, meaning your (user's) .bashrc is ignored. This is like becoming root without explicitly logging in as root. Additionally, you don’t have to enter the root user password.

Important: Please note that the su command also allows you to switch users (default to root). This command requires you to enter your root password. To avoid this, you can execute it using sudo (sudo su) so that you only need to enter your login password. However, there are implicit differences between su and sudo su - to learn about them, and how they differ from sudo -i, see here.

Summarize

I hope now you know at least the basics of sudo, and how to adjust sudo's default behavior. Please try adjusting /etc/sudoers as we explained. Also browse the forum discussions to learn more about the sudo command.

The above is the detailed content of How to solve Linux command line error prompts: Use sudo command. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
How to install Linux alongside Windows (dual boot)? How to install Linux alongside Windows (dual boot)? Jun 18, 2025 am 12:19 AM

The key to installing dual systems in Linux and Windows is partitioning and boot settings. 1. Preparation includes backing up data and compressing existing partitions to make space; 2. Use Ventoy or Rufus to make Linux boot USB disk, recommend Ubuntu; 3. Select "Coexist with other systems" or manually partition during installation (/at least 20GB, /home remaining space, swap optional); 4. Check the installation of third-party drivers to avoid hardware problems; 5. If you do not enter the Grub boot menu after installation, you can use boot-repair to repair the boot or adjust the BIOS startup sequence. As long as the steps are clear and the operation is done properly, the whole process is not complicated.

How to enable the EPEL (Extra Packages for Enterprise Linux) repository? How to enable the EPEL (Extra Packages for Enterprise Linux) repository? Jun 17, 2025 am 09:15 AM

The key to enabling EPEL repository is to select the correct installation method according to the system version. First, confirm the system type and version, and use the command cat/etc/os-release to obtain information; second, enable EPEL through dnfinstallepel-release on CentOS/RockyLinux, and the 8 and 9 version commands are the same; third, you need to manually download the corresponding version of the .repo file and install it on RHEL; fourth, you can re-import the GPG key when encountering problems. Note that the old version may not be supported, and you can also consider enabling epel-next to obtain the test package. After completing the above steps, use dnfrepolist to verify that the EPEL repository is successfully added.

How to choose a Linux distro for a beginner? How to choose a Linux distro for a beginner? Jun 19, 2025 am 12:09 AM

Newbie users should first clarify their usage requirements when choosing a Linux distribution. 1. Choose Ubuntu or LinuxMint for daily use; programming and development are suitable for Manjaro or Fedora; use Lubuntu and other lightweight systems for old devices; recommend CentOSStream or Debian to learn the underlying principles. 2. Stability is preferred for UbuntuLTS or Debian; you can choose Arch or Manjaro to pursue new features. 3. In terms of community support, Ubuntu and LinuxMint are rich in resources, and Arch documents are technically oriented. 4. In terms of installation difficulty, Ubuntu and LinuxMint are relatively simple, and Arch is suitable for those with basic needs. It is recommended to try it first and then decide.

How to add a new disk to Linux How to add a new disk to Linux Jun 27, 2025 am 12:15 AM

The steps to add a new hard disk to the Linux system are as follows: 1. Confirm that the hard disk is recognized and use lsblk or fdisk-l to check; 2. Use fdisk or parted partitions, such as fdisk/dev/sdb and create and save; 3. Format the partition to a file system, such as mkfs.ext4/dev/sdb1; 4. Use the mount command for temporary mounts, such as mount/dev/sdb1/mnt/data; 5. Modify /etc/fstab to achieve automatic mount on the computer, and test the mount first to ensure correctness. Be sure to confirm data security before operation to avoid hardware connection problems.

Fixed the failure to upload files in Windows Google Chrome Fixed the failure to upload files in Windows Google Chrome Jul 08, 2025 pm 02:33 PM

Have problems uploading files in Google Chrome? This may be annoying, right? Whether you are attaching documents to emails, sharing images on social media, or submitting important files for work or school, a smooth file upload process is crucial. So, it can be frustrating if your file uploads continue to fail in Chrome on Windows PC. If you're not ready to give up your favorite browser, here are some tips for fixes that can't upload files on Windows Google Chrome 1. Start with Universal Repair Before we learn about any advanced troubleshooting tips, it's best to try some of the basic solutions mentioned below. Troubleshooting Internet connection issues: Internet connection

Where are system logs located in Linux? Where are system logs located in Linux? Jun 24, 2025 am 12:15 AM

Logs in Linux systems are usually stored in the /var/log directory, which contains a variety of key log files, such as syslog or messages (record system logs), auth.log (record authentication events), kern.log (record kernel messages), dpkg.log or yum.log (record package operations), boot.log (record startup information); log content can be viewed through cat, tail-f or journalctl commands; application logs are often located in subdirectories under /var/log, such as Apache's apache2 or httpd directory, MySQL log files, etc.; at the same time, it is necessary to note that log permissions usually require s

What is the sudo command and when should I use it? What is the sudo command and when should I use it? Jul 02, 2025 am 12:20 AM

sudo stands for "substituteuserdo" or "superuserdo", allowing users to run commands with permissions of other users (usually root). Its core uses include: 1. Perform system-level operations such as installing software or editing system files; 2. Accessing protected directories or logs; 3. Manage services such as restarting nginx; 4. Modify global settings such as /etc/hosts. When using it, the system will check the /etc/sudoers configuration and verify the user password, provide temporary permissions instead of continuously logging in as root, ensuring security. Best practices include: only when necessary, avoid blindly executing network commands, editing sudoers files with visudo, and considering continuous operations.

How to manage groups on Linux How to manage groups on Linux Jul 06, 2025 am 12:02 AM

To manage Linux user groups, you need to master the operation of viewing, creating, deleting, modifying, and user attribute adjustment. To view user group information, you can use cat/etc/group or getentgroup, use groups [username] or id [username] to view the group to which the user belongs; use groupadd to create a group, and use groupdel to specify the GID; use groupdel to delete empty groups; use usermod-aG to add users to the group, and use usermod-g to modify the main group; use usermod-g to remove users from the group by editing /etc/group or using the vigr command; use groupmod-n (change name) or groupmod-g (change GID) to modify group properties, and remember to update the permissions of relevant files.

See all articles