Book Image

Bash Cookbook

By : Ron Brash, Ganesh Sanjiv Naik
Book Image

Bash Cookbook

By: Ron Brash, Ganesh Sanjiv Naik

Overview of this book

In Linux, one of the most commonly used and most powerful tools is the Bash shell. With its collection of engaging recipes, Bash Cookbook takes you through a series of exercises designed to teach you how to effectively use the Bash shell in order to create and execute your own scripts. The book starts by introducing you to the basics of using the Bash shell, also teaching you the fundamentals of generating any input from a command. With the help of a number of exercises, you will get to grips with the automation of daily tasks for sysadmins and power users. Once you have a hands-on understanding of the subject, you will move on to exploring more advanced projects that can solve real-world problems comprehensively on a Linux system. In addition to this, you will discover projects such as creating an application with a menu, beginning scripts on startup, parsing and displaying human-readable information, and executing remote commands with authentication using self-generated Secure Shell (SSH) keys. By the end of this book, you will have gained significant experience of solving real-world problems, from automating routine tasks to managing your systems and creating your own scripts.
Table of Contents (15 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Using chroot and restricted Bash shells to secure scripts


In this recipe, we will learn about chroot and restricted bash(rbash). The chroot command is used to change the root directory. Using rbash, we can restrict some features of the bash shell for some security purposes.

Getting ready

Besides having a Terminal open, you need to ensure that rbash is installed in the system.

How to do it…

  1. Now, we will look at the command to start rbash. Run the following command:
$ bash -r
or
$ rbash
  1. Now we will test some restrictions. First, we will try to change the directory. Run the following command:
$ cd work/

Next, we will try to write some contents to the file. Run the given command to write some contents into the file:

ls > log.txt

How it works…

After using rbash, access to the system will be limited. In the preceding examples, we started the restricted shell by typing bash -r or rbash.

Next, we tried to change the directory, but we got the rbash: cd: restricted message, so we cannot change the directory...