Linux Sudo in Bash Script Examples
If you’re an advanced Linux user, you must have faced situations where you want to log in as another user to run certain scripts. Well, the Sudo command helps users to run Linux commands at the root level of the system.
Here you will find out:
- what is Sudo command in Linux
- examples of Sudo command in Bash
- when DiskInternals can help you
Are you ready? Let's read!
Sudo command
Sudo is a powerful Linux command-line tool that grants a Linux user access to run commands as another user. In fact, Sudo stands for “Super User Do,” and it logs all arguments. Also, the Sudo command is mostly used when trying to install, launch and exit an application that requires root user privilege. Even without knowing the password of a user, the Sudo command can let you run scripts as that user.
How to use authentication only once
Sudo caches user credentials; thus, you use authentication only once and can run your scripts for a long time without having to use authentication again. You can run scripts for more than 15 minutes without inputting a password; all you need to do is to use Sudo.
For example:
Irrespective of whether the commands in your scripts contain Sudo or not, all commands will run in privileged mode, because the shell process runs in privileged mode. This does not have any Sudo effect.
How to run scripts with no password
It is possible to use the Sudo command to run scripts without entering a password. However, the process can be very stressful and inconvenient.
There are two processes to achieve this. The first one requires you to enter a password for the first time and then run many scripts for as long as you wish without entering another password. It sounds similar to “how to use authentication only once”, but they are different. In this process, you have to trick Sudo’s caching algorithm so that your initial password will never expire.
Method one:
Here’s the command to achieve this:
Defaults:
Using a negative value for timestamp_timeout will cache a password forever. On the other hand, a positive value will cause the password to expire in that number of minutes. Also, a value of 0 will disable the cache entirely, and you will have to enter a password for every Sudo command.
Method two:
Include an exception of script in /etc/sudoers
With this exception, you won’t need a password to use Sudo in running your scripts. However, this may not be efficient if you have lots of scripts to run.
Use your Linux files from Windows!
DiskInternals Linux Reader is an intuitive software designed to help users access Linux files on Windows OS. It is a handy software program for dual-boot PC users. Interestingly, DiskInternals Linux Reader does not work with any algorithm; its function is straightforward and easy to understand. Using DiskInternals Linux Reader, it is possible to access and view all Ext4, XFS, ZFS or HFS+ partitions where your Linux files are saved on a Windows computer.



Also, Linux Reader supports all different and less common partitions, as well as features a Windows Explorer-like interface.
Related articles
- About a bash date command
- How to Access Linux Ext2 or Ext3 on Windows
- How to Access Ext4 from Windows
- How to Mount Ext4 on Windows for Free
- Mount Linux Drive on Windows for Free
- Bash Script: All You Need to Know
- An Algorithm: How to Create Bash While Loop
- Linux Shell: What You Need to Know at First
- 5 Basic Shell Script Examples for Your First Script
- Bash: How to Check if the File Does Not Exist
- Bash Time Command on Linux
- How to use bash get script directory in Linux
- How to Check Bash String Equality
- How to Use Linux Wait Command
- Bash: How to Loop Through Files in Directory
- 20 Examples of Bash Find Command
- Bash Cat Command in Examples
- Bash Script SSH: How to Use It
- Bash: A Script For User Input
- Linux ZSH: the basic you need to know
- Basic of Grep in Linux Shell Script
- Shell Script Cut: Basic You Need to Know
- Bash: How to Check if String Not Empty in Linux
- Bash: how to split strings in Linux
- Hello World Script in Bash
- A Linux bin/bash Shell
- Linux: New Line in Shell Script
- Linux: Bash String Ends With
- Linux: Bash Printf Examples
- Linux: Bash First Line
- Linux: $0 in a Shell Script
- Linux: A Bash Startup Script
- Linux: Write a Shell Script
- Shell Script: Replace String in File
- A crontab service shell script
- Bash and sh: is there any difference
- A Bash Dirname Command
- Linux: A Bash Source Command
- A Bash to Loop through Lines in File
- Linux: A Bash Linter
- A Bash Nested Loop
- A Bash Test Command
- Use a Shell Script to Send an Email
- A Bash Status of Last Command
- Linux: A Bash Basename Command
- Using Bash to Write to a File
- About Bash Language
- Bash for Loop in One Line
- AWK in a Bash Script
- Learn about useful bash scripts
- Learn about systemd startup script
- About chaining bash commands
- Learn about a bash error code
- Using a bash tee command
- About a bash export command
- Learn about a bash wait command
- Arch Linux install script
- About advanced bash scripting
- To run a shell script in Dockerfile
- About a bash UNTIL loop
- Learn about C shell script
- Learn about Korn shell scripting
- Using /usr/bin/env command
- Whether bash waits for command to finish
- Using bash if 0
- Using && in an IF statement in bash
- If you want to run shell script in background
- The disk you inserted was not readable by this computer error
- Learn about SFTP in bash scripting
- Learn to run Perl script in Linux
- Examples of using the Expect
- Copy command in shell scripts
- Shell script usage
- Install Oracle Database
- Here is everything you need to know about Bash for Windows
- Guide in Pictures
- How to Use Shell Script Sleep Command
- A Bash Multiline Command
