Skip to the content.

Chapter 3. Access Linux [6 Hrs]

Excellent introduction to UNIX anatomy (similar to linux): http://ibgwww.colorado.edu/~lessem/psyc5112/usail/concepts/anatomy-of-unix/anatomy.html (If it doesn’t work, make sure your browser didn’t substitute http for https. Copy pasting may work.)

Shell

Some common shells are:

Shell working loop

The loop of the shell can be summarized in following steps:

Display the prompt.
Read the command.
Analyze the syntax.
Substitute special characters
Execute the command

Display the prompt
...
and so on

Command Structure

Command may contain options and arguments.

Essential linux commands:

  1. cd: Change directory
  2. ls: List files and directories.
  3. ln: Create symbolic links.
  4. pwd: Print working directory.
  5. cp: Copy
  6. mv: Move
  7. scp: Securely copy files.
  8. chown: Change ownership of a file.
  9. chmod: Change file access permissions.
  10. lsattr: List attributes
  11. chattr: Change attributes

Man page

OS specific

Example: Output of id -u root will always be 0

Super user (root)

sudo vs su

Example:

    [ramesh@rocky-linux ~]$ su ram -c ls

The above command will ask the password of ram, and then execute the command as ram.

    [ramesh@rocky-linux ~]$ sudo -u ram ls

The above command will ask the password of ramesh, and then execute the command as ram.

sudoers file

The file /etc/sudoers is known as the sudoers file. It can be accessed by the command:

    visudo

Structure of sudoers file:

The structure of sudoers file is:

user hostlist = (userlist) commandlist

Meaning: An user user logged in from one of the host from hostlist can run any command from commandlist as any user form the userlist.

If group instead of user is to be used, preceed it with a % as in

%group hostlist = (userlist) commandlist

Examples

# Ram can execute any command as any user, but only from rocky-peeps host
ram rocky-peeps = (ALL) ALL
# Ram can execute any command from any host but only as user lisa
ram ALL = (lisa) ALL
# Ram can execute only `/usr/bin/ls` and `/usr/bin/cat` command but as any user and from any host
ram ALL = (ALL) /usr/bin/ls, /usr/bin/cat
# Ram can execute any command as any user and from any host
ram ALL = (ALL) ALL
# Ram can execute any command as any user and from any host and no password is prompted when doing so
ram ALL = (ALL) NOPASSWD: ALL

When an user uses sudo to attempt something he cannot do, it may say “This incident will be reported.” To see the report, you can simply observe the file /var/log/secure as:

    less -f /var/log/secure