Introduction to the shell

The shell is for most Unix users the main interface to an Unix-based system like Linux or Mac OS X. The shell is a command line interface to the system and comes in different flavors like bash, tcsh, ksh, zsh, etc. While multiple shells exist, the most popular shells used are bash for interactive use and sh for system scripting.

A lot can be configured in the shell, for example the prompt. The prompt is the string that is displayed before the command line and can be changed by modifying the PS1 variable. The prompt normally also shows the current working directory and the current user, but this is a convention. A second convention is that if the prompt ends with a #, then your in a shell with root privileges and when it ends with a $, then you are in a shell with normal user privileges.

The prompt shows the # when the shell is in a root user context
# whoami
The prompt shows the $ when the shell is in a normal user context
$ whoami

This convention is used in all examples on this site together with the assumption that the user is logged in as user01 and is by default located in the /home/user01 directory.

Using su

Every shell is running under the account that was used to login with. This account also refered to as an user is member of a primary group that is required, and can also have up to 16 or 32 secondary groups depending is Network File System (NFS) is used or not.

With the command id you can get the primary group and the secondary groups of the user that is currently logged in.

The command id shows the primary group and the secondary groups of the user that is logged in
$ id
uid=1000(user01) gid=1000(user01) groups=1000(user01)

The command can also be extended to get the groups of other users defined on the system as is show below for the user root.

The command id can also show the primary and secondary group for other users
$ id root
uid=0(root) gid=0(root) groups=0(root)

A shell runs as the user that is logged in. This is the user that is used to run the command line and to see the effective user that is used to run the command whoami.

Show the effective user for the shell
# whoami

The effective user can be changed by using the command su. This command changes the user that the shell runs as, but to accomplish this the password of the target user is required.

Change the effective user to the user root
$ su root

By default only the effective is changed by the su command, but the environment is not changed. To change the environment as well, use the - option and the environment is also changed when the user changes.

$ su - root
$ su - root -c "env"
$ su root -c "env"

Using sudo

$ sudo -l
$ sudo su -
$ sudo cat /var/log/messages
$ sudoedit /etc/hosts

Monitoring the system and processes

Process state:

  • D - Uninterruptable Sleep

  • R - Running or Runnable

  • S - Sleeping

  • T - Stopped or Traced

  • Z - Zombie

$ uptime
$ w

Showing who is logged on

$ who

cat /proc/cpuinfo

$ lscpu
Architecture:            x86_64
  CPU op-mode(s):        32-bit, 64-bit
  Address sizes:         39 bits physical, 48 bits virtual
  Byte Order:            Little Endian
CPU(s):                  4
  On-line CPU(s) list:   0-3
Vendor ID:               GenuineIntel
  Model name:            Intel(R) Core(TM) i5-6260U CPU @ 1.80GHz

Showing processes

$ ps -ef
$ pstree
$ pgrep <process name>

Process priority

Default value is 0

  • 0-19: can be set by the user

  • -20- -1: can only be set by root

$ nice <command>
$ nice -n 15 <command>
$ renice -n 7 <PID>
$ renice -n 7 $(pgrep <process name>)

Working with files

Archiving and compression

# tar cf archive.tar file1 file2 file3
# tar cf /root/etcbackup.tar /etc
# tar tf /root/etcbackup.tar

Compression methods:

  • z - gzip

  • j - bzip

  • J - xz

# tar czf /root/etcbackup.tar.gz /etc
# tar cjf /root/logbackup.tar.bz2 /var/log
# tar cJf /root/sshbackup.tar.xz /etc/ssh
# mkdir /root/logbackup
# cd /root/logbackup
# tar xjf /root/logbackup.tar.bz2
# mkdir /root/etcbackup
# cd /root/etcbackup
# tar xf /root/etcbackup.tar


Copy a file to a remote host:

$ scp /etc/sudo.conf /etc/sudoers server02:/tmp

Copy a file to a remote host as root:

$ scp /etc/sudo.conf root@server02:/tmp

Copy a file from a remote host as root to the current directory:

$ scp root@server02:/tmp/sudo.conf .

indien je hele directories wilt overzetten moet je de -r vlag meegeven:

With the -r flag scp will copy the contents of the directory:

$ scp -r /etc user01@server02:~


$ sftp user01@localhost
user01@localhost's password:
Connected to localhost.

sftp> ls
bin output
sftp> mkdir test
sftp> cd test
sftp> put /etc/hosts
Uploading /etc/hosts to /home/user/test/hosts
/etc/hosts 100% 510 0.5KB/s 00:00


Rsync is a fast, reliable, efficient, secure, and portable file transfer. The example below will copy the contents of the directory /etc to the directory /srv/rsync/etc/:

$ rsync -av /etc /srv/rsync

The example below will copy the contents of the directory /etc/ to the directory /srv/rsync/:

$ rsync -av /etc/ /srv/rsync

Copy log files from server01 to /tmp on the current host:

$ rsync -av server01:/var/log /tmp

Remote login with SSH keys

$ ssh-keygen
$ scp ~/.ssh/ user@remotehost:~/.ssh/
$ cd .ssh
$ cat >> authorized_keys
$ ssh-copy-id [user@]remotehost