InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Linux/Unix Command Line tutorials

  • Awk
  • Curl
  • Edit a file without changing its timestamp on Linux
  • Find which process is listening on a port on Linux
  • Get file size in bytes on Linux
  • How to delete files starting with dash/hyphen
  • How to kill unresponsive ssh session using escape sequence
  • How to show environment variable for a process id (pid)
  • How to sort using a specific field on Linux
  • How to specify environment variable for a command on Linux
  • How to zip/unzip a directory with password
  • ImageMagick
  • Impact on LC_ALL on Linux sort
  • Linux - find listening ports
  • Linux - find top directories by used disk size (excluding size of subdirectories)
  • Linux - how to run a command as different user
  • Linux - list only directories
  • Linux - providing sudo access to a users - some best practices
  • Linux - sending mail from command using mailutils
  • Linux file timestamps
  • Linux how to modify a user using usermod
  • Linux ping a port using netcat
  • Linux replace comma with newline
  • Linux screen - quick start guide
  • Linux what package provides a file
  • Linux/Unix - How to go to previous directory
  • Linux/Unix - find inode number of a file
  • Linux/Unix - truncate a large log file without deleting it
  • Linux/Unix history with date and time
  • Memcache - how to dump all keys and values on command line
  • Mongo - cli quick start guide
  • Perl command line - replace multi line comments
  • Python/Perl/Unix one liners
  • Rsync
  • Ruby gem - handy reference
  • SSH
  • Some handy linux gnu date commands
  • Use watch to monitor a command at some frequency on Linux
  • bower - installation and quick start guide
  • csvkit - parse csv file and data on Linux command line
  • ffmpeg
  • grep without regex (fixed string)
  • redis cli quick start tutorial
  • wget handy commands

Linux find

  • Find recently modified files on Linux
  • Linux - find and delete files older than 30 days
  • Linux - find files containing specific text
  • Linux find - ignore case in name
  • find - exclude directory or file pattern
 
  • Home
  • > Tutorials
  • > Linux/Unix Command Line

Linux – providing sudo access to a users – some best practices

By admin | Last updated on May 31, 2020

Giving sudo to a user is frequently occurring activity on Linux. Here are some scenarios and best practices for providing sudo to users on Linux. We’ll use use Ubuntu Linux for the purpose of this tutorial.

Location of sudoers file and sudoers.d directory

Default sudoers file is located at /etc/sudoers and the directory for including other files is /etc/sudoers.d/

There is an include directive in /etc/sudoers as shown below:

#includedir /etc/sudoers.d

To list existing sudoers we can run

$ sudo cat /etc/sudoers
$ sudo ls /etc/sudoers.d/
$ sudo cat /etc/sudoers.d/user1
...

For better management (and automation) it is better to create a sudoer file for each user inside /etc/sudoers.d/.

Use visudo for editing sudoers file

It is better to use visudo to edit a sudoers file. This command checks the syntax of sudoer file before saving. This avoid accidental saving of a wrong syntax file. Saving a wrong syntax file can create problem and sudo command may stop working for users having sudo access. This may cause problem if root login is disabled on that Linux machine. Here is how you can use visudo.

// Edit default /etc/sudoers file
$ sudo visudo

//or edit specific file
$ sudo visudo -f /etc/sudoers.d/user1

visudo example with syntax error

Run sudo visudo -f /etc/sudoers.d/user1 add the following lines (syntax errors)

(wrong syntax

When you try to save this file, you will get the following error message. You will have a choice to either re-edit the file or exit. Never save wrong syntax file.
linux-visudo-syntax-error-save-failure-example

Giving sudo to user with full access and no password

To give sudo (with full access with no password prompt) to user1 add run:

$ visudo /etc/sudoers.d/user1

And add the following line to it

gopj ALL=(ALL) NOPASSWD: ALL

Note that with this approach, user will never be asked to enter password when using sudo.

Validate if user has sudo

To validate if user has sudo run run command (say pwd) as that user as sudo.

$ sudo -i -u user1 sudo pwd
/home/user1

Suggested posts:

  1. How to fix a broken sudoers file on AWS/EC2 Linux
  2. How to locally override website domain (or hostname) to IP mapping
  3. Linux – how to run a command as different user
  4. Setup xdebug for remote wordpress debugging
  5. Linux how to modify a user using usermod
  6. Linux rsync tutorial
  7. Linux – how to add a user using useradd
  8. Mac finder – change search default folder to current folder
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Devops, Linux, Linux/Unix Command Line, Tutorials, Ubuntu Linux
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress