Skip to content

Instantly share code, notes, and snippets.

@mutuadavid93
Last active August 15, 2023 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mutuadavid93/4786aac1caaab4861206c3467967e89d to your computer and use it in GitHub Desktop.
Save mutuadavid93/4786aac1caaab4861206c3467967e89d to your computer and use it in GitHub Desktop.
Linux Commands

######################################################

Linux Commands

######################################################

rsync

Transfer files between servers.

  • a - archive mode i.e. keep files metadata same e.g. dates
  • r - recursive
  • v - verbose
  • z - zip files while transferring

$ rsync -ravz --remove-source-files --delete root@143.198.172.183:<source_path> <destination_folder_path>

e.g. $ rsync -ravz --remove-source-files --delete root@143.198.172.183:/root/notes .

SSH

cut

Gets content from a file e.g. words or characters

Options

  • b - get by byte
  • c - by character
  • f - field. Meaning words
  • d - delimiter. Default is Tab. Can be changed to " " or ":" i.e. Space or Full-colon

Start from 7th character, grab single character at 56th position and select 57th to 61st characters. $ cut -b 7-11,56,57-61

Get first and second words. $ cut -d " " -f 1,2

Get first word while using full-colon as delimiter $ cut -d ":" -f 1 /etc/passwd

NOTE: used where a file has multiple lines

tr

Translates characters or text content.

Options

  • s - squeeze, removes duplicate specified characters
  • d - removes characters

Convert all the lowercase characters in the text file into uppercase. The ones which were uppercase remain the same. $ tr [a-z] [A-Z] <

Below command has same effect as the one above $ less | tr [a-z] [A-Z]

Similar to below one $ cat | tr [:lower:] [:upper:]

Delete all alphabets $ tr -d [:alpha:] <

Replace a character with another e.g. replace all ocurrences of "!" with "." $ tr "!" "." <

Systemd

PID 1 Manages other processes called units e.g. Services and Timers

Start a service $ systemctl start <service_name>

Check status of a service $ systemctl status <service_name>

Restart a service $ systemctl restart <service_name>

Enable a service on bootup $ systemctl enable <service_name>

Disable a service not start on bootup $ systemctl disable <service_name>

Reload the service by reloading it's configuration files. Users won't be disconnected from the service $ systemctl reload <service_name>

System Unit Directories

  • /etc/systemd/system

    • highest priority directory
  • /run/systemd/system

    • runtime system units
  • /lib/systemd/system

    • after installing packages, the package's service files are store here

System Unit File types *.service *.timer *.mount *.wants *.target

Due to priority and precedence any personal service files or files you need to run first. Must be inside /etc/systemd/system

Example of a service file;


[Unit] ; generate service descriptions and dependencies imports ; e.g. Before, After e.t.c Description=The Apache HTTP Server After=network.target remote-fs.target nss-lookup.target Documentation=https://httpd.apache.org/docs/2.4/

[Service] ; forking, starts by being started by a parent process which starts and terminates ; leaving current service(s) started Type=forking Environment=APACHE_STARTED_BY_SYSTEMD=true

; systemctl start <service_name> ExecStart=/usr/sbin/apachectl start

; systemctl stop <service_name> ExecStop=/usr/sbin/apachectl graceful-stop

; systemctl reload <service_name> ExecReload=/usr/sbin/apachectl graceful KillMode=mixed PrivateTmp=true Restart=on-abort

[Install] ; This service will be started only when e.g. multi-user.target unit has started, ; i.e. starts when the stage where multiple users can login is reached. WantedBy=multi-user.target

Editing System unit files $ sudo systemctl edit <service_name>

NOTE:

  • above command opens *.conf file which created by the edit command.
  • created inside /etc/systemd/system/ directory since MUST take priority to do the override
  • edits must be above "Edits below this comment will be discarded" disclaimer line.

Example of a service file editted;


[Unit] Description=The Apache HTTP Server and is for Simps. Use Nginx instead

NOTE:

  • to undo the change, remove the override file
  • $ sudo rm /path/to/override/file/name e.g. /etc/systemd/system/apache2.service.d/override.conf

To instead start editing with all the file's changes present instead of creating your an override file;

  • $ sudo systemctl edit --full <service_name> Above command saves the exact file with same name but saves the copy inside a higher priority directory, instead of /lib/systemd/system

NOTE:

  • Edit in vim rather than nano $ sudo -E systemctl edit <service_name>

FINALLY for edit changes to take effect, the systemd must be reloaded to include them $ sudo systemctl daemon-reload

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