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 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.
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.
Here’s the command to achieve this:
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.
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.