How best to learn proper usage of Terminal commands

I am very interested in finding solutions to various problems I am having with Mac OS (presently still on High Sierra). Many postings I find for problem solving involve terminal commands.
I already get stuck when using „sudo“ : “Username is not in the sudoers file. This incident will be reported”.
When do I use „quit“, „exit“, should I always use terminal commands as Admin user etc.? I am afraid to do harm to my data/ running system.
I have been using personal computers since 1984, and in 2009 I finally got sick and tired of Windows and moved to a better world called Mac OS X. I have pretty basic programming skills going back to Cobol and Basic.

What do y‘all think is the best approach to learning how to properly use terminal commands? How did you master that learning curve? Is there the one „bible“ for it in the form of a book or a website?

Many thanks for your comments and suggestions.

https://www.takecontrolbooks.com/command-line/

I think it is smart to start out with https://www.takecontrolbooks.com/command-line/ After that I would have a look at this book. It is my bible. Shell-Programming-Unix-Linux-Developers I have the third edition Read the reviews there.

I also recommend Joe Kissell’s Take Control book.

Here are two courses I’ve come across recently:

Bart Busschotts’ Taming the Terminal is based on a series of podcast segments he did with Allison Sheridan. I’ve found it helpful to read an entry and then go back and follow along with the corresponding podcast segment.

Another one that you may find helpful, but more fast-paced and with a wider scope, is MIT’s Missing Semester site and videos — a series of lectures for beginning computer science students about using the command line.

1 Like

I would find things you normally do with the Finder and slowly learn how to do them with the Terminal. Learn how to navigate the file system (cd), look for files (ls, find), view them (cat), edit them (nano or download the micro text editor), move them (mv), copy them (cp), delete them (rm), change permissions (chmod and chown), and check to see where you are (pwd or cwd). If you have servers do this over ssh. Obviously rm is where you can do the most damage. But you can also over write files with mv and cp. If you are going to do system admin type stuff you’ll need to learn how to use vi a little (how to enter edit mode, exit edit mode, quit without saving, and saving and quitting).

Learn how relative vs absolute paths work ("/" vs everything else). Learn how to transfer files back and forth between servers with scp. If you can learn this you will be surprised how much you can do.

If you see the “Username is not in the sudoers file" message it means you are a non-admin user, which is usually good. This is how I use my computers. In Terminal (or iTerm2), you just need to “su adminusername” before you use “sudo”. Yes, you can destroy your computer with sudo. I just saw this today.

I wonder if he was still employed the next day?

neil

If you can use su, you don’t need sudo.

In traditional Unix, each user has a digital user ID. User 0 is usually known as root or the superuser. Basically, root can do anything on the system.

The su command logs you in as the superuser. (Well it spawns a shell making you superuser)
Sometimes, certain users need to do something they’re not normally allowed to do. Imagine if Bob was given the task to make backups. He wouldn’t normally be given read access to other user’s files, and maybe also excluded from reading certain administrator files (like /etc/shadow that contains the passwords). You could give Bob the root password, but you don’t want him being able to do everything, just run the backup.

Enter the sudo command. This command gives people like Bob certain super user access for particular commands. This was set in a file called /etc/sudoers In this file you can specify which users and groups have superuser (or su) access for which commands, and if they need to enter their personal password to run these commands. Bob can be given superuser status to run backups, but not to reformat the hard drive.

On MacOS, the root user has no password. No one can log in as root. Instead, certain users are given sudo access. These are users who you specify as administrators in the System Preference. You are an administrator, you can use sudo. If not, you can’t.

Normally, the way you use sudo is to use sudo followed by the command you want to run. For example:

$ sudo passwd carol #set Carol’s password

All sudo commands are normally logged (this is configurable) and if someone tries something they’re not supposed to do, you get that “Username is not in the sudoers file. This incident will be reported” message. The report is a log file on your computer. It’s not like Tim Cook got an email. Think of it as that permanent record you’re principal talked about in junior high school.

I don’t remember the MacOS default, but I believe you need your password and you must enter a command you’re sudoing on the same line. A blank sudo command logs you in as superuser — something you don’t usually want. However, you can change this by editing the /etc/sudoers file. Long time ago, I gave myself the ability to run sudo without the password and run sudo without another command.

If your new to the command line, do not use sudo unless you know exactly what you’re doing.

Most Unix and Linux command lines use the BASH shell. Due to copyright issues with GNU, MacOS users zsh. Both are 99% the same. However, there are differences in configurations commands. If you get a Linux book on BASH, it’ll teach you quite a bit about MacOS and zsh programming. You just have to be aware there are some differences. Linux books and BASH books are quite common. MacOS and zsh are harder to find.

Also note the man command. It’ll tell you in excruciating detail all about the command. Most of it won’t make sense until you get more familiar with Unix, but it’s always helpful. It shows similar commands.

1 Like

Unless you enable the root account: How to enable the root user on your Mac or change your root password – Apple Support

This will let you log in as root from the GUI login window.

It will also let you use the su command to launch a shell as the root user, but only if you are switching from an administrator account. (This can be disabled by commenting off a line in a system configuration file, but I wouldn’t recommend it since there are really no occasions where it is necessary.)

Similarly, even with root enabled, you can’t perform an SSH login to the root account without updating a system configuration file to permit it. Again, there really shouldn’t be a need to do this, since it is a potential security hole and you can always log in to an administrator account and use su or sudo from there.

On most Unix systems, the root account receives an e-mail about the failed sudo attempt (and system administrators typically configure root’s mailbox to auto-forward it to the administrator account(s)). This doesn’t happen on macOS, probably because the root account is disabled by default so there would never be anybody to see or delete such a message.

It shouldn’t be doing that. But if you want to get a root shell, it’s easy enough to type sudo sh or even sudo su (if the root account is enabled). Unless, of course, /etc/sudoers is configured to not let you do that.

Or emacs. And some of the basic emacs key bindings can be used in the shell and in many text entry areas of macOS.

1 Like

Yes, you can configure the system to do whatever you want. But I was just trying to offer some quick starter tips that the OP could use now. I think it’s asking too much of a new Terminal user to configure sudo.

I think Mac OS X 10.5 and 10.6 was the closest Mac got to “traditional” Unix. Ever since then it’s moved away.

sudo alone prints usage. Use sudo -s or sudo zsh to get a root shell.

Another few tips with Terminal involve moving between Finder and Terminal. Drag an item into the Terminal to get it’s path. So a lot of time I’ll find something in Finder, open Terminal, type "nano " (with a space), then drag the item into terminal.

To open something in Finder with Terminal just type “open " followed by the thing to open (a folder, an application, or a file”.

Another way to get yourself to learn the Terminal is to install some really cool Terminal only applications that don’t come with macOS. Homebrew is the easiest way to do that. The install instructions are on the front webpage.

I don’t know what you do with your computer, but some examples of things everyone might want to download are as follows:

  • micro (easy to use terminal editor)
  • youtube-dl (download YouTube videos)
  • wget (download websites)
  • tldr (this is similar to man but really easy to read)
  • imagemagick (edit images from the command line)
  • gnu-typist (learn to type)
  • wumpus (classic game)

If you program.

  • python3
  • zsh
  • php

etc

If you run servers.

  • apache
  • node
  • mariadb

etc

Brew will also install GUI applications (called casks). For example, brew can install the following.

  • firefox
  • chrome
  • bbedit
  • docker
  • dropbox
  • go2shell
  • grandperspective
  • iterm2
  • virtualbox
  • xquartz
  • zoom

Brew can also install Mac App Store apps if you install “mas”. It’s a bit tricky though because you have to know the MAS id. For example:

mas “Numbers”, id: 409203825
mas “Slack”, id: 803453959
mas “Xcode”, id: 497799835

Much as I love emacs…

On my MacBook Pro running 11.2.3:

mbp13r$ which vi

/usr/bin/vi

mbp13r$ which emacs

i.e. emacs doesn’t come with Big Sur, while vi does. Better to learn vi.

Oh yeah, I forgot Apple did that. I think Catalina also didn’t include emacs. I ended up installing it using Homebrew or MacPorts.

I have been Sysadmin’ing for ~30 years plus.

The only emacs command I know is how to exit emacs. Every machine I have worked on has either ci or vim ( vi with macros ), it will always be there, emacs - well it might be there but not actually configured in a way that you can use.

Having said that I have friends, family and colleagues who live inside emacs - I am very happy for them :slight_smile:

I just want to know something that will be available on every machine I use.

FWIW bbedit with ssh access is what I use for doing serious editing, it just works as they say. For everything else there is vi/vim.

However emacs/vi is a religious war so I will not comment further. My favourite all time .sig can be seen here in the tee shirt: Emacs versus vi | Jeffrey's notepad

Cheers

f

Exactly. Anybody who’s familiar with emacs is used to using things like ctrl-a, ctrl-e, ctrl-k. The great thing is that those work on most Mac apps too. I wouldn’t know what to do without them. Had I learned vi back in the day and looked down on emacs, I would have never learned those. And nowadays I’d be be frustrated that macOS apps don’t know what you mean when you hit :q. :wink:

I downloaded and installed the Emacs for OSX build.

Then, in order to allow access from the command line, I created a shell script, /usr/local/bin/emacs containing:

#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs $*

There are, of course, many other ways to install Emacs, but this is one of the easiest I’ve run across over the years.

Easier way to install (vanilla) Emacs: get the free Aquamacs app, from the menu select “Install command line tools”, et voila!
You get for free also an (optional) mac-friendly GUI for emacs.

–e.

Clix is a free program that not only teaches you how to use the command line, and shows you various useful commands, it will allow you to use the command line without accidentally damaging anything (sort of like the command line with training wheels):

Clix (free)
https://rixstep.com/4/0/clix/

Thanks for your help and wealth of information, guys. My learning curve has started with Joe Kissel’s Take Control book. And when done I will revisit this thread to steepen the curve… :nerd_face:

And what I was writing while this conversation was going on. :slight_smile: