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.
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
oracleuser typically belongs to theoinstallgroup, which owns the Oracle software, and thedbagroup, 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
- 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
| Number | Permissions | Typical use |
|---|---|---|
755 | rwxr-xr-x | Scripts and directories everyone can use, but only the owner can change |
750 | rwxr-x--- | Scripts for the owner and their group only |
644 | rw-r--r-- | Ordinary files everyone can read |
640 | rw-r----- | Files only the owner and group can read, such as backups |
600 | rw------- | 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:
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.
topshows a live, updating view of the processes using the most CPU and memory. Press q to quit.killfollowed 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
oracleuser, in theoinstallanddbagroups. Usesudoonly when needed, and avoid working as root. - Permissions are read, write, and execute, for the owner, group, and others.
chmodchanges them (750,640,600);chownchanges the owner. - The pipe
|feeds one command's output into another, andgrepkeeps only the matching lines. ps -ef | grep pmonshows whether the database is running. Never kill database processes.df -hshows free space anddu -shfinds what's using it. Never delete files you don't recognize.tail,tail -f,grep, andlessread logs such as the alert log.