Skip to content

Instantly share code, notes, and snippets.

@amit08255
Last active November 12, 2023 22:03
Show Gist options
  • Save amit08255/d8d2d1cb0573fe2f8b5dbba08ce46d27 to your computer and use it in GitHub Desktop.
Save amit08255/d8d2d1cb0573fe2f8b5dbba08ce46d27 to your computer and use it in GitHub Desktop.
Linux Cheatsheet

Linux Cheatsheet

Switch to root user

sudo su -

User Management

  • Check if username exists: id username

  • Add new user with username: sudo adduser username

  • Check if user has expiry date: sudo chage -l username

  • Make account expire at specific date: sudo chage -E 2021-04-15 username

  • Check root login status on direct SSH: sudo cat /etc/ssh/sshd_config | grep PermitRoot

  • Disable root login on direct SSH: Edit file: sudo vi /etc/ssh/sshd_config. Replace the #PermitRootLogin yes to PermitRootLogin no. Restart SSH service: sudo systemctl restart sshd && systemctl status sshd

  • Passwordless sudo for user: Edit sudo config sudo visudo. Look for line: root ALL=(ALL) ALL and add line: username ALL=(ALL) NOPASSWD:ALL.

List Files and Folders

  • List files in current directory: ls

  • List files in current directory with permission, date and size: ll

Services

  • Enable service to start at startup: sudo systemctl enable service_name

  • Disable service to start at startup: sudo systemctl disable service_name

  • Start service: sudo systemctl start service_name

  • Stop service: sudo systemctl stop service_name

  • Status of a service: sudo systemctl status service_name -l

Database Issues

  • MariaDB log file: cat /var/log/mariadb/mariadb.log

  • MariaDB failed to start process: Stop mariadb process. Check log file. Use command: sudo chown mysql:mysql /var/run/mariadb/

DNS Issues

  • Domain name resolver file: cat /etc/resolv.conf

  • Adding new DNS server entry: Add nameserver entry in resolver file like this: nameserver 8.8.8.8

String Commands

  • Replace all occurrence of string in file: sed -i 's/original_text/new_text/g' file_path

  • Search string in file: cat file_path |grep text_to_search

File and Folders

  • Create folder and sub-folders: mkdir -p foler/subfolder/sub2

File Permissions

  • Change primary group of directory: chgrp -R group_name dir_path

  • Change directory permission to specific group: First get directory group ID by ll -lsd path_of_folder. The group ID is a number after permission. 4 drwxr-xr-x 2 root dbadmin 4096 Jun 22 17:29 /dbadmin/data/ here it is 2. chmod -R 2770 folder_path 2 - Special Permission. set group id. 7 - 4+2+1 = 7 (4= read, 2 = write, 1 = execute).User (root) Permission. 7 - 4+2+1 = 7 (4= read, 2 = write, 1 = execute). Group Permission. 0 - Other users.(zero permission).

Run Levels and GUI setup

  • Get default run level: sudo systemctl get-default

  • Set default run level GUI: sudo systemctl set-default graphical.target

  • Check status of GUI run level service: sudo systemctl status graphical.target

  • Start GUI run level service: sudo systemctl start graphical.target

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment