Lesson 3 of 5 · Linux for DBAs

Linux Administration Basics for DBAs

The commands DBAs reach for when something goes wrong: users and groups, file permissions, pipes and grep, running processes, disk space, and reading log files.

Beginner 8 sections 6 min read

Beyond moving around

The previous lesson covered finding your way around and working with files. This one covers the next commands every DBA uses on a server: who you're logged in as, who can read and change which files, what's running, how much disk space is left, and what the logs say.

Users, groups, and root

Every person and every program on a Linux server runs as a user. Users belong to groups, which make it easy to give several users the same access.

  • Oracle software runs as its own user, usually called oracle, not as a person's account.
  • The oracle user typically belongs to the oinstall group, which owns the Oracle software, and the dba group, which can connect to the database with full administrator rights.
  • root is the superuser: it can do anything on the system, which also makes its mistakes the most dangerous.
$ whoami                 # which user am I?
kofi

$ id oracle              # which groups does the oracle user belong to?
uid=54321(oracle) gid=54321(oinstall) groups=54321(oinstall),54322(dba)

$ sudo su - oracle       # switch to the oracle user

sudo runs a command with root's power, if your account is allowed to. It asks for your password, and every use is logged. DBAs typically use it to switch to the oracle user, or for a few approved admin tasks.

File permissions

Every file and directory has an owner, a group, and three sets of permissions. ls -l shows them as a ten-character string at the start of each line:

$ ls -l /u01/backup
-rwxr-x--- 1 oracle oinstall  2048 Oct 23 02:00 backup.sh
-rw-r----- 1 oracle oinstall 51200 Oct 23 02:14 orcl_full.bkp
- rwx r-x --- TypeOwnerGroupOthers - file, d directoryoracleoinstalleveryone else read, write, executeread, executenothing 7 5 0 = 750 Numbers: read = 4, write = 2, execute = 1. Add them up for each set: rwx = 4 + 2 + 1 = 7.
r (read)
For a file, view its contents. For a directory, list what's in it.
w (write)
For a file, change it. For a directory, create, rename, or delete files in it.
x (execute)
For a file, run it as a program or script. For a directory, enter it with cd.

chmod changes permissions, and chown changes the owner and group (it needs root, so it's usually run with sudo):

$ chmod 750 backup.sh                         # owner: everything; group: read and run; others: nothing
$ chmod 600 secrets.txt                       # only the owner can read or change it
$ sudo chown oracle:oinstall /u01/backup      # make oracle the owner, oinstall the group
NumberPermissionsTypical use
755rwxr-xr-xScripts and directories everyone can use, but only the owner can change
750rwxr-x---Scripts for the owner and their group only
644rw-r--r--Ordinary files everyone can read
640rw-r-----Files only the owner and group can read, such as backups
600rw-------Private files, such as anything holding a password

Pipes and grep

The pipe, |, sends the output of one command into the next. grep keeps only the lines that contain a word. Together, they turn a long list into the one line you need:

ps -ef hundreds of processes | grep pmon keep matching lines ora_pmon_ORCL

Processes: what's running

A process is a running program. ps -ef lists every process on the server, and piping it to grep answers the most common DBA question: is the database up?

$ ps -ef | grep pmon
oracle    2101     1  0 09:00 ?        00:00:00 ora_pmon_ORCL
kofi      4410  4380  0 10:42 pts/0    00:00:00 grep --color=auto pmon

The first line is the Oracle instance's process monitor, so the ORCL instance is running. The second line is just the grep command finding itself. The second column is each process's ID, its PID.

  • top shows a live, updating view of the processes using the most CPU and memory. Press q to quit.
  • kill followed by a PID stops a process.

Disk space

A full disk is one of the most common reasons a database stops working. df -h shows how full each file system is, in human-readable sizes:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   12G   38G  24% /
/dev/sdb1       500G  476G   24G  96% /u01

/u01, where the Oracle files live, is 96% full. du -sh shows how much space each directory uses, so you can find what's growing:

$ du -sh /u01/app/oracle/*
1.4G    /u01/app/oracle/diag
18G     /u01/app/oracle/fast_recovery_area
410G    /u01/app/oracle/oradata
9.8G    /u01/app/oracle/product

Reading logs

When something goes wrong, the answer is usually in a log file. For Oracle, that's the alert log. These commands read logs without opening them in an editor:

$ cd /u01/app/oracle/diag/rdbms/orcl/ORCL/trace

$ tail -n 50 alert_ORCL.log     # show the last 50 lines
$ tail -f alert_ORCL.log        # follow new lines as they're written; Ctrl+C to stop
$ grep "ORA-" alert_ORCL.log    # show only the lines with Oracle errors
ORA-01653: unable to extend table SALES.ORDERS by 128 in tablespace APP_DATA
$ less alert_ORCL.log           # scroll through it; /word to search, q to quit

tail -f is especially useful: run it in one window while you start the database or run a backup in another, and watch what happens in real time.

Key takeaways

  • Oracle runs as the oracle user, in the oinstall and dba groups. Use sudo only when needed, and avoid working as root.
  • Permissions are read, write, and execute, for the owner, group, and others. chmod changes them (750, 640, 600); chown changes the owner.
  • The pipe | feeds one command's output into another, and grep keeps only the matching lines.
  • ps -ef | grep pmon shows whether the database is running. Never kill database processes.
  • df -h shows free space and du -sh finds what's using it. Never delete files you don't recognize.
  • tail, tail -f, grep, and less read logs such as the alert log.