10 Unix commands every Mac and Linux user should know

Get started with Bash, Unix pipes, directory navigation, sudo, Nano, and other command-line essentials

1 2 Page 2
Page 2 of 2

Naturally, adding a user to the sudoers file requires root privileges. I’ll need to log out of my nunez account and log into my administrator account. Then if I run sudo nano /etc/sudoers at the command line, Nano will open the sudoers file in the terminal window, ready for editing:

15 bash nano sudoers1 IDG

We can navigate a file in Nano using the arrow keys. To make nunez a sudoer, we need to scroll down to the “User privilege specification” section and enter the line nunez ALL=(ALL) ALL, as you see for the root and %admin users:

16 bash nano sudoers2 IDG

With the line entered, we type Ctrl+x and enter Yes at the prompt to save the changes and exit Nano. After I log back in as nunez, we can see an example of sudo in action:

17 bash touch IDG

As you can see, the first attempt at adding a file to the / directory (a privileged directory owned by root) failed, but when I used the sudo command it worked. Some folks advocate using sudo for all commands requiring root privileges, but I think it's tedious. It's fine to work from a root shell when you need to—but remember to revert to a normal user when you're done with root tasks.

This incantation will almost always work to get you a root shell:

18 bash sudo bin bash IDG

The - at the end may not make sense right now, but use it anyway. Remember those environmental variables? The hyphen will ensure that your root user environment is set up properly and your root session doesn’t inadvertently alter the environment you switched from.

Related:
1 2 Page 2
Page 2 of 2