Linux Basics
Commands and programs that all Linux users need to know (but many don't!)
TODO: Add screenshots/code examples for each command; put commands in tables; clean and organize all (issue #7)
Command-line Basics
Command | Description |
---|---|
| Get help with a command |
| View history of commands that have been typed into the terminal |
| Repeat a specific command from command history |
| Search through command history: then cycle with Up or Down arrows. (Do not need to type |
| Cycle through previously used command arguments |
| Move between "words" on a command line |
| Clear all text off the terminal window |
| Print string to terminal.
|
| Copy selected text |
| Paste clipboard contents |
| Print from file to printer |
| Change directories |
| Move up one directory |
| Change directory to current user's home directory |
| Return to previous directory |
| Exit terminal session |
Special Symbols
| Purpose |
| Send the output of one command to another. |
| Redirect output to a file. |
| Redirect input from a file. |
| Append output to an existing file. |
| Separator used in path names. |
| Used to escape characters and to send multi-line commands. |
| Current directory. |
| Parent directory. |
| displays the process ID of the current shell instance. |
| Process command in the background (and give control of the terminal back). |
| Run the next command only if the previous completed successfully. |
| Match any number of characters in file name. |
| Match any single character in file name. |
| Match any one of the enclosed characters in file name. |
| Run commands in sequence, regardless if the previous succeeded. |
| Group commands. |
| Used to feed multiple parameters to a single command. Separate parameters by |
| Followed by a digit will repeat the command from the history file that corresponds. |
| Repeat the previous command. |
| Shortcut that stands for Standard Input (STDIN) |
| Shortcut that stands for Standard Output (STDOUT) |
| Shortcut that stands for Standard Error (STDERR) |
Filesystem Basics
Everything in Linux is a file, even directories and devices. Directories have some special restrictions, but for the most part can be treated like files.
Listing and viewing Directories and Files
Command | Description |
---|---|
| List files in a folder, to include hidden files:
|
| List files with attributes (filesize, permissions, etc.) |
| List files, sorted by Size |
| List files in current folder and all subfolders (Recursive) |
| Locate all files that symlink to a file |
| Searches for files in a |
| Uses a database to search for files. Update the database with |
| List the size, used space, and available space on the mounted filesystems of your computer |
| Print the contents of a file to the command line |
| Combine the contents of two text files |
| Compare two files and show differences (Only for text-based files) |
| Search for string inside a file |
| Displays the first 10 lines of a file. Specify the number of lines with |
| Displays the last 10 lines of a file. Specify the number of lines with
|
| Displays the filetype of a file, determined by the hexadecimal " magic bytes". |
File and directory creation and deletion
Command | Description |
---|---|
| Create a new blank file with this name |
| Copy file from one location to another. If no location is specified, creates the copy in the same directory. [Path optional] |
| Move file from one location to another. If no location is specified, renames the file in same directory (removes the old file). |
| Removes (deletes) a file. |
| Removes (deletes) all files in the directory. |
| Recursively deletes all files in the directory and all subdirectories and files. Will not prompt for approval with |
| Makes a new empty directory |
| The |
| Deletes an (empty) directory |
| Removes all instances of a specified filename. Only searches PATH directories. You could also use |
File & text manipulation
Command | Description |
---|---|
| Concatenates the contents of two files |
| Counts the lines, words, and bytes in a file. |
| A programming language for text processing. Can do many many things. |
| Performs text editing on a stream of text. Useful for replacing text in a file and much more. Example: Replace all occurrences of 1001 with 0 in /etc/passwd.
|
| Extract a section of text. |
| |
| |
| Compare two files and show differences. Output is in three columns:
|
| Compare two files and show differences. Has two modes:
|
| Opens two files in Vim side-by-side and highlight the differences. Some shortcuts:
|
File Permissions
The permissions for a file (for example, viewed with the ls -l
command) are typically written as:
r
= read w
= write x
= execute
Breaking down this format gives us four parts:
The first character tells if it is a file or a directory. if it is a
-
(hyphen) then it is a file. However if the first character is ad
, then the file is a directory. (Remember, technically everything in Linux is a file, even directories).The next three characters specify the permissions of the owner of the file.
The following three characters specify the permissions of the group that owns the file.
The final three characters specify the permissions of all other users.
The permissions -rwxrwxrwx
mean that the anyone can read, write and execute the file.
In the above example, the owner, group, and everyone permissions are all rwx
; hence anyone can read, write, and execute this file.
The chmod command
The chmod
command is used to set the permissions on a file. This is usually expressed in one of two different formats, ugoa+rwx and octal notation. The command is used as follows:
Octal notation
In octal notation, the permissions are assigned using triple octal (base8) digits. The first digit is the cumulative permissions for the owner, the second for the group, and the third for everyone else.
Permissions | Binary notation | Octal notation | Description |
---|---|---|---|
| 000 | 0 | No permissions |
| 001 | 1 | Execute permission only |
| 010 | 2 | Write permission only |
| 011 | 3 | Write and execute |
| 100 | 4 | Read permission only |
| 101 | 5 | Read and execute permission |
| 110 | 6 | Read and write permission |
| 111 | 7 | Read, write and execute |
From the above table we can easily derive :
Therefore, if you want to give only the owner read and write permissions, they would be assigned 600
(4+2=6).
Taking the same example from above, to assign the permissions -rwxrwxrwx
the command would be:
That is: read (4), write (2), and execute (1) permissions for the owner, group, and all others.
ugoa+rwx notation
In this notation format, there are three main components:
Who. The users to modify permissions for:
u
= user (owner),g
= group,o
= others, and finallya
= u+g+o (all).What. The modifier:
=
to set permissions,+
for adding permissions,-
for removing permissions.Which. The permissions to set, add, or remove: one or more of
rwx
as above.
As you can see, this notations allows for easier and more explicit control over exactly which permissions are given to whom.
Examples:
To give all users the write permission:
To remove write and execute permissions from the 'other' group:
These permission changes can also be chained by adding a comma between the permission changes.
To add read/write permissions for the file owner and group, while making it read only for everyone else:
Advanced permissions (TODO: Finish cleaning this up. Add descriptions of SUID/GUID)
Other than just read and write, you can also set some other permissions like SUID and GUID.
chmod 4000 file
chmod +s file
Both the above examples would add the setuid
bit to the file.
chmod 2000 file
chmod +g file
Both the above examples would add the getuid
bit to the file.
The sticky bit
https://en.wikipedia.org/wiki/Sticky_bit <- pull more information from here and add
The "sticky bit" is added to folders in order to prevent anyone else from deleting the folder or any of its contents. It is represented by a t
at the end of the permissions d--r--r--rt
. When a sticky bit is set, nobody other than the owner or the root can delete the folder or the file.
chmod 1000 folder
chmod +t folder
Both the above examples set the sticky bit to the folders
Examples: chmod 1744 file
This would set the sticky bit, give all permissions to the owner and only read permission to the group and others
chmod 0600 file
This would only give the owner read and write permission, but not execute permission.
The chown command
The chown
command can be used to change the owner of a file or a directory.
The above command would change the owner of the file to $user
and also the group to $group
.
File Attributes
Read attributes of files on Linux with lsattr
lsattr
lists the file attributes on a second extended file system. See chattr
below for a description of each attribute.
Useful options:
Argument | Description |
---|---|
| Recursively list attributes of directories and their contents. |
| List all files in directories, including files that start with |
| List directories like other files, rather than listing their contents. |
| Print the options using long names instead of single character abbreviations. |
You can chain together these options to recursively list the attributes of all files and folders in a directory with long names:
Change attributes of files on Linux with chattr
chattr
changes the file attributes on a Linux file system.
The format of a symbolic mode is
+-=[aAcCdDeFijmPsStTux]
.
Symbol | Meaning |
---|---|
| Add the following attributes the to specified file |
| Remove the following attributes from the specified file |
| Set the attributes of the specified file to be the following |
The letters aAcCdDeFijmPsStTux
select the new attributes for the specified files:
Attribute | Description |
---|---|
| append only |
| no atime updates |
| compressed |
| no copy on write |
| no dump |
| synchronous directory updates |
| extent format |
| case-insensitive directory lookups |
| immutable |
| data journaling |
| don't compress |
| project hierarchy |
| secure deletion |
| synchronous updates |
| tail-merging |
| top of directory hierarchy |
| undeletable |
| direct access for files |
The following attributes are read-only and may be listed by lsattr
but not modified by chattr
:
Attribute | Description |
---|---|
| encrypted |
| indexed directory |
| inline data |
| verity |
See the chattr manpage for more detailed descriptions of each attribute.
File compression and encryption (TODO:)
Command | Description |
---|---|
unzip | |
gunzip | |
tar |
System Information
Command | Description |
---|---|
| List OS, hostname, kernel build number, CPU architecture |
| List running processes (current user) |
| List running processes for all users (if permitted) |
| Similar to Windows Task Manager, lists running processes with details of hardware usage |
| Show list of all services installed with status |
Networking
Command | Description |
---|---|
| Get networking information (IP, Subnet mask, MAC, etc.); On some systems may require |
| Get networking information (IP, Subnet mask, MAC, etc.); No |
| Set IP address for an interface |
| Change MTU size for an interface |
| Change MAC address (or use |
Managing connections
TODO: add more information about Managing connections in Linux (Issue #9)
Add commands such as telnet, SSH, nc, curl, wget
Add commands for listing information about open network connections: lsof -i, ss, netstat
include description and examples
Command | Description |
---|---|
| |
| |
| |
| Transfer data to or from a server using a variety of protocols including IMAP/S, POP3/S, SCP, SFTP, SMB/S, SMTP/S, TELNET, TFTP, and others. |
| Downloads files using the HTTP,HTTPS, or FTP protocols. |
| Download files using concurrent connections
|
nc listener: nc -lvnp <port>
list open network connections
Command | Description |
---|---|
| |
| Shows State, data sent/recieved, local process:port, remote address:port |
| Get all connections that are listening, do not resolve names, show process information |
|
Shared folders
Command | Description |
---|---|
| Show available shares to mount |
| Connect to Windows SMB share folder |
TODO: add more information on mounting and using network shares (issue #10)
Add information on creating, mounting, and connecting to network shares (Samba, SMB, etc.)
Add commands such as
smbclient
,smbpass
,showmount
,mount
, Downloading Files, Detecting ADS,smbmap
,
DNS
Command | Description |
---|---|
| Look up DNS information for a site |
| Reverse look up a domain from an IP |
| Look up the IP address for a host- or domain-name. |
Installing and Managing Programs
Command | Description |
---|---|
| Update repository database |
| Update installed programs and packages (must update repository database first). Adding |
| |
| |
| Search for packages (unknown name) to install from repositories |
| Search for package in repositories |
| Show details about the specified package |
| Installs the specified package (and any dependencies). |
| Uninstalls the specified package |
| Installs the specified |
| Convert rpm to Debian packages |
Users and Groups
TODO: Add information about Linux Users and Groups (issue #11)
Add information about creating, modifying, and deleting users and passwords
Add information about use, creation, modification, and deletion of groups
Add commands such as
adduser
,groups
,passwd
, etc.Add commands related to listing users, seeing who is logged in, etc. (
id
,w
,last -a
)add descriptions and examples
Command | Description |
---|---|
| Displays all environment variables for the current context. |
groups
Add a new user: adduser
addgroup
id
w
last -a
User Privileges
Command | Description |
---|---|
| Execute commands with elevated privileges |
| Execute |
| List |
| Stop remembering credentials and re-prompt for password |
| Configuration file for |
Environment Variables
Command | Action |
---|---|
| List all current environment variables and their values. |
| Define the value of an environment variable. Can be a new or existing variable. Exported variables only work in the context of the current shell. |
| List the values in the PATH environment variable. |
| Show the current username. |
| Show the current working directory. |
| Show the current user's home directory |
| Show the process ID of the current shell. |
| Show number of rows and columns in the current shell. |
$PATH
To make
$PWD
part of path so you don't need./
when running commands/scripts: (NOT RECOMMENDED for home/production use!)export PATH='pwd':$PATH
Add new $PATHs to the
.profile
file rather than.bashrc.
Then, use the commandsource ~/.profile
to use the newly added PATHs.The best way to add a path (e.g., ~/opt/bin) to the PATH environment variable is:
(from https://unix.stackexchange.com/questions/26047/how-to-correctly-add-a-path-to-path)
$HISTCONTROL
The HISTCONTROL environment variable can be used to whether the bash history removes duplicate commands, commands that start with a space, or both. The default behavior is to remove both.
ignoredups
- Ignore Duplicates
$HISTIGNORE
The HISTIGNORE environment variable can be used to filter commands so they do not appear in the history.
This example causes the history command to not log common commands such as ls
,bg
,fg
,exit
,and history
. Uses standard bash text shortcuts such as [ ] to indicate options.
$HISTTIMEFORMAT
The HISTTIMEFORMAT environment variable controls date/time stamps in the output of the history command.
Startup Scripts
Add script to run at startup: update-rc.d </path/to/the/script> defaults
(needs 755 permissions)
Delete script from default autorun: update-rc.d -f </path/to/the/script> remove
Make a Linux live boot USB
On Windows (easiest way!):
Download and run Rufus.
Select the USB device and ISO you want to use, giving the volume a name if you wish.
If you want to use persistence,
Click "Show advanced drive options".
Select the amount of storage to use for persistence.
Click "Start" and wait for it to finish.
For Kali live persistent boot USBs you will need the additional step of adding a persistence.conf
by following the instructions below.
Verify your USB devices persistent storage partition with the command
fdisk -l
.After locating your partition (in this example it is
/dev/sdb3
), label itpersistence
.Create a mount point, mount the new partition there, and then create the configuration file to enable persistence. Finally, unmount the partition.
Recover an unresponsive terminal
Press the RETURN/ENTER key.
You may have typed a command but forgotten to press RETURN to tell the shell that you’re done typing and it should now interpret the command.
If you can type commands, but nothing happens when you press RETURN, try typing CTRL-J. If this works, your terminal needs to be reset to fix the RETURN key. Some systems have a reset command that you can run by typing CTRL-J reset CTRL-J. If this doesn’t work, you may need to log out and log back in or turn your terminal off and on again.
If your shell has job control type CTRL-Z.
This suspends a program that may be running and gives you another shell prompt. Now you can enter the jobs command to find the program’s name, then restart the program with fg or terminate it with kill.
Use your interrupt key (typically DELETE or CTRL-C).
This interrupts a program that may be running. (Unless a program is run in the background as the shell will wait for it to finish before giving a new prompt. A long-running program may thus appear to hang the terminal.) If this doesn’t work the first time, try it once more, though doing it more than twice usually won’t help.
Type CTRL-Q.
If output has been stopped with CTRL-S, this will restart it. (Note that some systems will automatically issue CTRL-S if they need to pause output; this character may not have been typed by the user from the keyboard.)
Check that the SCROLL LOCK key is not toggled on.
This key stops the screen display from scrolling upward. If pressing it once does not work, make sure you’ve pressed it an even number of times as this leaves the key in the same state it was when you started.
Type CTRL-D at the beginning of a new line.
Some programs (like mail) expect text from the user. A program may be waiting for an end-of-input character from you to tell it that you’ve finished entering text. However, typing CTRL-D may cause you to log out, so you should only try this as a last resort.
If you’re using a windowing system, close (or terminate) the terminal window and open a new one.
Fixing command-not-found
errors
command-not-found
errorsIf you encounter errors on your system when you mistype a command or try to run a program that is not installed try these steps to fix the command-not-found
command.
TODO: screenshot or type out example so people know what I am referring to...
https://bugs.launchpad.net/command-not-found/+bug/1824000
Fork Bomb
A fork bomb is a type of denial-of-service attack against Unix-based systems, which makes use of the fork operation (or equivalent functionality) whereby a running process spawns another running process indefinitely. This attack works by creating a large number of processes very quickly in order to saturate the available resources of the operating system.
Once this code is executed, within seconds the target system will freeze and will have to hard rebooted.
A common succinct bash fork bomb looks like:
Which can be explained as:
Function | Description | |
---|---|---|
| define a function named | |
`: | :` | load a copy of the |
| Disowns the other functions. If the first | |
| Ends the function definition and tells the interpreter to run what is next as a command | |
| Call function |
It can also be written as:
References
If you like this content and would like to see more, please consider buying me a coffee!
Last updated