Skip to content

Linux

The open-source OS.

Linux Monitoring Tools

See Linux Monitoring Tools

SELinux

See SELinux

RSysLog

See Rsyslog

User Management

See Linux User Management.

Package Installer

See the apt-get manual.

Favourite Commands

find and grep — Search for a String

Recursively searches / for string and prints the results to the terminal. The -i flag ignores case sensitivity.

sudo find / | grep -i 'string'

tail — Live View of a File

Gives you a scrolling view of the log file. As new lines are added, they appear in the console.

tail -f 'path/to/file'

ls — List Directory Contents

Use the long format to show owner and permission details:

ls -l

Include hidden files and directories as well:

ls -al

ps — Report a Snapshot of Current Processes

Show processes for the current user only:

ps -u

Show processes for all users:

ps -au

Show all processes, including those without an associated terminal:

ps -aux

See Linux Monitoring Tools for details and analysis of using ps.

rm — Remove Files or Directories

Remove a single file:

rm 'file'

Remove a directory and all its contents (use caution):

rm -r 'directory'

Force removal of unwritable files (use extreme caution):

rm -rf 'directory'

chown — Change File or Directory Ownership

Change the file owner:

chown 'Owner' 'file'

Change the file owner and group:

chown 'Owner':'Group' 'file'

Change a folder and its contents (use caution):

chown -R 'Owner':'Group' 'directory'

chmod — Change Access Permissions

Make a file readable, writable, and executable by the owner, group, and others:

chmod 777 'file'

Make a file executable:

chmod +x 'file'

Change a folder and recursively apply the permissions to its contents (use caution):

chmod -R 744 'directory'

How to Determine chmod Codes

chmod is a tricky function because it uses numbers to alter file and folder permissions. These numbers are based on a recognizable pattern that produces the required permissions.

chmod uses three octal numbers (for example, 777). Each number maps to read, write, and execute permissions for one of three user groups:

  • The first number sets permissions for the owner or creator of the file or folder. This may be root, but it is typically the user who created it.
  • The second number sets permissions for the group that the file or folder belongs to. For example, files created in /etc/apache or /var/www are typically part of the www-data or apache user group.
  • The third number sets permissions for all other users. For user-created files, this is often the field you will edit because other programs or users may need access.

Each number is an octal value from 0 to 7. It can be represented in binary using three bits:

  • The first bit is read permission. 1 means enabled and 0 means disabled.
  • The second bit is write permission. 1 means enabled and 0 means disabled.
  • The third bit is execute permission. 1 means enabled and 0 means disabled.

Examples

To give a file read, write, and execute permission for itself; read and execute permission for the group; and read permission for everyone else:

  • Read/write/execute = 111 = 7. The first number is 7.
  • Read/execute for the group = 101 = 5. The second number is 5.
  • Read for everyone else = 100 = 4. The third number is 4.

To set these permissions, run:

chmod 754 <name-of-file>