> For the complete documentation index, see [llms.txt](https://0un390.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://0un390.gitbook.io/notes/articles-and-write-ups/linux-system-administrator-cheat-sheet.md).

# Linux system administrator cheat sheet

| **Accessing Command Line & Managing Files from the Command Line** |                                                                                                                    |
| ----------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
| ls                                                                | lists directory contents for the specified directory or if no directory is given for the current working directory |
| Cd                                                                | Change directory                                                                                                   |
| Cd -                                                              | changes to the previous directory                                                                                  |
| Cd ..                                                             | uses the .. Hidden directory to move up one level to the parent directory                                          |
| Pwd                                                               | displays the full path name of the current working directory for that shell                                        |
| cat                                                               | change your shell’s current working directory                                                                      |
| cat file1  file2                                                  | Viewing the Contents of Files                                                                                      |
| touch                                                             | creating empty files                                                                                               |
| mkdir                                                             | creates one or more directories or subdirectories                                                                  |
| cp                                                                | Copies a file                                                                                                      |
| mv                                                                | moves files from one location to another                                                                           |
| rm                                                                | removes files                                                                                                      |
| Rm -r                                                             | remove a subdirectory and its contents                                                                             |
| rmdir                                                             | removes the directory that is empty                                                                                |
| passwd                                                            | Change password for user                                                                                           |
| wc                                                                | counts lines, words, and characters in a file                                                                      |
| history                                                           | displays a list of previously executed commands                                                                    |
| Ln                                                                | creates a new  hard link                                                                                           |
| Ln -s                                                             | creates a soft link                                                                                                |

| <p><strong>Redirecting output to a file</strong></p><p> </p> |                                                                      |
| ------------------------------------------------------------ | -------------------------------------------------------------------- |
| > file                                                       | <p>redirect stdout to overwrite a file</p><p> </p>                   |
| >> File                                                      | redirect stdout to append a file                                     |
| 2>file                                                       | redirect stderr to overwrite a file                                  |
| 2> /dev/null                                                 | redirect stderr error message by redirecting to /dev/null            |
| >file2>&1                                                    | redirect stdout and stderr to overwrite the same file                |
| &> file                                                      | <p>redirect stdout and stderr to overwrite the same file</p><p> </p> |
| >>file2>&1                                                   | redirect stdout and stderr to append to the same file                |
| &>>file                                                      | redirect stdout and stderr to append to the same file                |

&#x20;

| **Managing Local Users and Groups** |                                                                                     |
| ----------------------------------- | ----------------------------------------------------------------------------------- |
| id                                  | Information about currently logged in user                                          |
| Ls -l                               | Owner of File                                                                       |
| Ls -ld                              | Owner of directory                                                                  |
| su                                  | switch users                                                                        |
| su - user                           | switch user                                                                         |
| sudo -i                             | to get interactive root shell                                                       |
| Useradd username                    | Create a user                                                                       |
| userdel username                    | deletes the user with the specified username and removes him from /etc/passwd       |
| userdel –r username                 | deletes the user with the specified username and its home directory is also deleted |
| groupadd -r 'group\_name'           |                                                                                     |
| passwd username                     | sets initial password or changes the existing password                              |
| cat /etc/passwd                     | Show users                                                                          |
| cat /etc/group                      | Show groups                                                                         |

| **Controlling access to a file** |                                                                                                                               |
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| ls -la                           | shows detailed information about permissions and ownership                                                                    |
| ls -ld                           | shows detailed information about a directory itself and not its contents                                                      |
| chmod                            | command used to change permissions, it means “change mode”                                                                    |
| Chmod ### file                   | Change permission using numeric method                                                                                        |
| chmod WhoWhatWich file1          | Set permissions using Symbolic method                                                                                         |
| Examples:                        |                                                                                                                               |
| chmod go-rw file1                | Remove read and write permission for group and other on file1                                                                 |
| chmod a+x file2                  | Add execute permission for everyone on file2                                                                                  |
| Chmod 644 file1                  | Set read and write permissions for group and others on file1                                                                  |
| Chmod 750 file1                  | Set read, write and execute permissions for user, read and execute permissions for group and no permission for other on file1 |
| chown                            | change owner = used to change file ownership                                                                                  |
| Examples:                        |                                                                                                                               |
| chown  user1 file1               | Grant ownership of the **file1** file to the user1 user                                                                       |
| Chown -R user1 directory1        | Grant ownership of directory1 and all files and subdirectories within it to user1                                             |
| Chown :admins directory1         | change the ownership of directory1 to visitor and the group to guests                                                         |
|                                  |                                                                                                                               |

| **Controlling services and Daemons**            |                                                                                                        |
| ----------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
| systemctl                                       | used to explore the current state of the system                                                        |
| Examples:                                       |                                                                                                        |
| <p>systemctl</p><p>list-units type=services</p> | listing all currently loaded service units paginating the output using **less**                        |
| Systemctl list-units –type=service --all        | lists all service units                                                                                |
| systemctl                                       | Systemctl without any argument lists units that are both loaded and active                             |
| Systemctl list-unit-file –type=services         | To see the state of all unit files installed                                                           |
| Systemctl status service\_name                  | Viewing Services States                                                                                |
| Systemctl is-active service\_name               | Verify that a service unit is currently active                                                         |
| Systemctl is-enabled service\_name              | Verify whether a service unit is enabled to start automatically during system boot                     |
| Systemctl is-failed service\_name               | Verify whether the unit failed during startup                                                          |
| systemctl –failed –type=service                 | List all the failed units                                                                              |
| Systemctl start service\_name                   | Starting service                                                                                       |
| Systemctl stop service\_name                    | Stopping service                                                                                       |
| Systemctl restart service\_name                 | Restart a service                                                                                      |
| Systemctl reload service\_name                  | Reload a service                                                                                       |
| Systemctl reload-or-restart service\_name       | In case you are not sure if the service has the functionality to reload the configuration file changes |
| Systemctl list-dependencies Unit\_name          | displays a hierarchy mapping of dependencies to start the service unit                                 |
| <p>Systemctl mask Unit\_name</p><p> </p>        | Masking a service                                                                                      |

| **Configuring and Securing SSH**                   |                                                                                                                                             |
| -------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| ssh remotehost                                     | log you in on the remote server **remotehost** using the same user name as the current local user                                           |
| exit                                               | log out of the remote system                                                                                                                |
| ssh user1\@remotehost                              | log you in on the remote server remotehost using the user name **user1**                                                                    |
| ssh user1\@remotehost hostname                     | ssh command would run the hostname command on the remotehost remote system as the user1 user without accessing the remote interactive shell |
| w                                                  | displays a list of users currently logged into the computer                                                                                 |
| ssh-keygen                                         | To create a private key and matching public key for authentication                                                                          |
| ssh-keygen -f .ssh/key\_pass                       | The -f option with the **ssh-keygen** command determines the files where the keys are saved                                                 |
| ssh-copy-id -i .ssh/key\_pass.pub uer1\@remotehost | copies the public key of the SSH keypair to the destination system                                                                          |
| ssh  -i .ssh/key\_pass uer1\@remotehost            | **authenticate** to the remote system using the corresponding private key while logging in to the remote system over SSH                    |

| **Analyzing and storing logs**                                        |                                                                                                                                |
| --------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| tail -f /var/log/secure                                               | Returns date only from a datetime value (Only use if variable is problematic)                                                  |
| logger                                                                | sends messages to the rsyslog service                                                                                          |
| Logger -p local7.notice "Log entry created on host"                   | send a message to the rsyslog service that gets recorded in the /var/log/boot.log file                                         |
| journalctl                                                            | retrieve log messages from the journal                                                                                         |
| journalctl –n                                                         | shows the last 10 log entries                                                                                                  |
| journalctl –f                                                         | outputs the last 10 lines of the system journal and continues to output new journal entries as they get written to the journal |
| journalctl –p                                                         | takes either the name or the number of a priority level and shows the journal entries for entries at that priority an above    |
| journalctl –p -p err                                                  | list journal entries at the err priority or higher                                                                             |
| journalctl  –-since today                                             | list all journal entries from today’s records                                                                                  |
| Journalctl –since "2019-2-10 20:30:00" –-untill "2019-02-13 12:00:00" | list all journal entries ranging from 2019-02-10 20:30:00 to 2019-02-13 12:00:00                                               |
| journalctl -o verbose                                                 |                                                                                                                                |
| <p>Journalctl \_SYSTEMD\_UNIT=</p><p>sshd.service \_PID=1182</p>      | shows all journal entries related to the sshd-service systemd unit from a process with PID 1182                                |
| Journalctl \| grep -E "Runtime\|System journal'                       | output shows the journal entries that reflect the current size limits                                                          |
| journalctl -b 'number'                                                | limit the output to a specific system boot                                                                                     |
| timedatectl                                                           | shows an overview of the current time related system settings                                                                  |

| **Managing Networking**               |                                                                                                    |
| ------------------------------------- | -------------------------------------------------------------------------------------------------- |
| ip link show                          | list all network interfaces available on your system                                               |
| ping 'ip'                             | test connectivity                                                                                  |
| ip route                              | show routing information                                                                           |
| ip -6 route                           | show the IPv6 routing table                                                                        |
| traceroute 'host'                     | trace the path that network traffic takes to reach a remote host through multiple routers          |
| Traceroute6 'host'                    | For ipv6 trace the path that network traffic takes to reach a remote host through multiple routers |
| ss                                    | display socket statistics                                                                          |
| nmcli                                 | used to create and edit connection files from the command line                                     |
| nmcli dev status                      | displays the status of all network devices                                                         |
| nmcli con show                        | displays a list of all the connections                                                             |
| <p>nmcli con show</p><p> --active</p> | list only active connections                                                                       |
| nmcli con add                         | used to add new network connections                                                                |
| nmcli dev disconnect 'device'         | disconnects (deactivate) the network interface device and brings it down                           |
| nmcli con show 'name'                 | list the current settings for a connection                                                         |
| nmcli con up 'name'                   | command activates the connection name on the network interface it is bound to                      |
| <p>nmcli con down</p><p>'name'</p>    | brings down the connection                                                                         |
| nmcli con mod 'name'                  | change the settings for a connection                                                               |
| nmcli con del 'name'                  | deletes the connection identified by name from the system                                          |
| Host name                             | displays the host name                                                                             |

| **Archiving and transferring files** |                                                                                                           |
| ------------------------------------ | --------------------------------------------------------------------------------------------------------- |
| tar                                  | list the contents of archives  or extract their files to the current system                               |
| Tar -cf archive.tar file1 file2      | creates an archive named archive.tar with the contents of file1, file2, in the user’s home directory      |
| tar -cf /root/etc.tar /etc           | create the tar archive named /root/etc.tar with the /etc directory                                        |
| Tar -tf /root/etc.tar                | List an archive content                                                                                   |
| Tar -xf /root/etc.tar                | <p>restore files from the /root/etc.tar</p><p>archive</p>                                                 |
| Tar -xpf /root/myscript.tar          | an archive named /root/myscripts.tar is extracted while preserving the permissions of the extracted files |
| Tar -czf /root/backup.tar.gz /etc    | create a gzip compressed archive named /root/backup.tar.gz with the contents from the /etc directory      |
| Tar -xzf /root/backup.tar.gz         | extract the contents of a gzip compressed archive named /root/backup.tar.gz                               |
| scp 'file' 'destenation'             | copies files from a remote system to the local system or from the local system to a remote system         |
| sftp                                 | upload or download files from a SSH server                                                                |
| sftp: put 'file'                     | uploads a file to the remote system                                                                       |
| sftp: get 'file'                     | downloads a file from the remote system                                                                   |
| sftp: exit                           | exits the sftp session                                                                                    |
| rsync                                | Synchronize Files and directories                                                                         |
| rsync -av /var/log /tmp              | synchronize contents of the /tmp directory to /var/log directory                                          |

{% file src="/files/47sf8EyXmfo4Tpot0U2w" %}
