Skip to content

Instantly share code, notes, and snippets.

@Souldiv
Last active March 4, 2024 18:29
Show Gist options
  • Save Souldiv/54f40e5d958506587b57f4f4db10eb41 to your computer and use it in GitHub Desktop.
Save Souldiv/54f40e5d958506587b57f4f4db10eb41 to your computer and use it in GitHub Desktop.
Linux and more

2. Common Linux commands

w - show who is logged and what they are doing

22:40:33 up 45 min,  2 users,  load average: 0.00, 0.03, 0.00

USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
souldiv  pts/1    -                21:55   45:11   0.00s  0.00s -bash
root     pts/3    -                22:34    6:07   0.00s  0.00s -bash

who - show who is logged on

souldiv  pts/1        2023-09-27 21:55
root     pts/3        2023-09-27 22:34

uptime - tell how long system has been running

su - run a command with sub user and group id

2.1. UFW

Uncomplicated firewall setup, works at the OS level if using with security groups which work at the instance level in aws. Requires sudo access.

2.1.1. Verify UFW Status

sudo ufw status

The output will indicate if your firewall is active or not.

2.1.1 Enable UFW

sudo ufw enable

You’ll see output like this:

Output
Firewall is active and enabled on system startup.

2.1.2. Disable UFW

sudo ufw disable

Be aware that this command will fully disable the firewall service on your system.

2.1.3. Block an IP address

sudo ufw deny from 203.0.113.100

In this example, from 203.0.113.100 specifies a source IP address of “203.0.113.100”.

If you run sudo ufw status now, you’ll see the specified IP address listed as denied:

Output
Status: active

To                         Action      From
--                         ------      ----
Anywhere                   DENY        203.0.113.100 

2.1.4. Block a subnet

If you need to block a full subnet, you may use the subnet address as from parameter on the ufw deny command. This would block all IP addresses in the example subnet 203.0.113.0/24.

sudo ufw deny from 203.0.113.0/24

2.1.5. Block Incoming Connections to a Network Interface

To block incoming connections from a specific IP address to a specific network interface, run the following command, replacing the highlighted IP address with the IP address you want to block:

sudo ufw deny in on eth0 from 203.0.113.100

The in parameter tells ufw to apply the rule only for incoming connections, and the on eth0 parameter specifies that the rule applies only for the eth0 interface. This might be useful if you have a system with several network interfaces (including virtual ones) and you need to block external access to some of these interfaces, but not all.

2.1.6. Allow an Ip address

sudo ufw allow from 203.0.113.101

2.1.7. Allow Incoming Connections to a Network Interface

sudo ufw allow in on eth0 from 203.0.113.102

The in parameter tells ufw to apply the rule only for incoming connections, and the on eth0 parameter specifies that the rule applies only for the eth0 interface.

If you run sudo ufw status now, you’ll see output similar to this:

Output
Status: active

To                         Action      From
--                         ------      ----
...         
Anywhere on eth0           ALLOW       203.0.113.102   

2.1.7. Delete UFW Rule

To delete a rule that you previously set up within UFW, use ufw delete followed by the rule (allow or deny) and the target specification. The following example would delete a rule previously set to allow all connections from an IP address of 203.0.113.101:

sudo ufw delete allow from 203.0.113.101

2.1.8. List Available Application Profiles

Upon installation, applications that rely on network communications will typically set up a UFW profile that you can use to allow connection from external addresses. This is often the same as running ufw allow from, with the advantage of providing a shortcut that abstracts the specific port numbers a service uses and provides a user-friendly nomenclature to referenced services.

To list which profiles are currently available, run the following:

sudo ufw app list

2.1.9. Enable Application Profile

To enable a UFW application profile, run ufw allow followed by the name of the application profile you want to enable, which you can obtain with a sudo ufw app list command. In the following example, we’re enabling the OpenSSH profile, which will allow all incoming SSH connections on the default SSH port.

sudo ufw allow "OpenSSH"

2.1.10. Allow Nginx HTTP / HTTPS

Upon installation, the Nginx web server sets up a few different UFW profiles within the server. Once you have Nginx installed and enabled as a service, run the following command to identify which profiles are available:

sudo ufw app list | grep Nginx

Output
  Nginx Full
  Nginx HTTP
  Nginx HTTPS

To enable both HTTP and HTTPS traffic, choose Nginx Full. Otherwise, choose either Nginx HTTP to allow only HTTP or Nginx HTTPS to allow only HTTPS.

The following command will allow both HTTP and HTTPS traffic on the server (ports 80 and 443):

sudo ufw allow "Nginx Full"

2.2. SSH

Secure Shell - used for establishing connection to a remote shell securely

2.2.1. basic information with SSH

SSH used for encrypted communication. Consists of a private key and a public key. private key needs to be secure, public key is used for sharing. public key is used for encrypting and private key is used for decrypting.public key can be generated from the private key but not the other way around. Private key is your unique identity.

2.2.2. ssh_copy_id user@remote-server-ip

copy your public key to remote server to establish communication

2.2.3. ssh-keygen

ssh-keygen -t ed25519 -C "youremail@example.com"

generate new ssh keys with your desired algorithm.

2.3. netcat

Netcat is a utility that is able to write and read data across TCP and UDP network connections. If you are responsible for network or system security it essential that you understand the capabilities of Netcat. Netcat can be used as port scanner, a backdoor, a port redirector, a port listener and lots of other cool things too.

2.3.1. port scanning with netcat

nc -v -w 2 -z <target_ip> <port/range of port>

2.3.2 connect to a port on localhost

nc localhost 30000

2.4. lsb_release

print distribution specific information

2.4.1. Check what linux version is running

lsb_release -a

2.5 nmap

2.5.1 Scan an network using nmap

Scan an entire network using nmap with verbose flags

-sn disable port scan

nmap -sn 172.19.206.239/20 -vvvv 

2.5.2 Scan a port on localhost

nmap -p 30000 localhost

2.5.3 Service scan using nmap

The -sV flag lets us do a service/version detection scan.

nmap -sV localhost -p 31000-32000

2.6 stdout, stdin, stderr

Use redirection operator > to echo into a text file

echo Hello World > test.txt

Use the redirection operator >> for not overwriting the text file

echo Hello World >> test.txt

Use < for redirecting stream into stdin

 A file descriptor is a non-negative number that is used to access a file or stream. We will go in depth about this later, but for now know that the file descriptor for stdin, stdout and stderr is 0, 1, and 2 respectively.

ls /fake/directory 2> 

2.7 find

Find a file in the current directory which is readable and size 1033 bytes and exec file command on the file

find . -type f -size 1033c ! -executable -exec file {} +

2.8 xxd creating hexdump or reversing

reverse hexdump

xxd -r <hexdump_file>

2.10 gzip bzip2 tar

you need gzip files to have .gz suffix, no such requirement for bzip2, and for tar you need files to be of .tar file

gzip -d data.gz

bunzip2 data.bin

tar -xf data.tar

3. permissions

$ ls -l

drwxr-xr-x. 4 root root    68 Jun 13 20:25 tuned
-rw-r--r--. 1 root root  4017 Feb 24  2022 vimrc

The first field of the ls -l output is a group of metadata that includes the permissions on each file. Here are the components of the vimrc listing:

  • File type: -
  • Permission settings: rw-r--r--
  • Extended attributes: dot (.)
  • User owner: root
  • Group owner: root

3.1. how to read permissions

This article is about the permission settings on a file. The interesting permissions from the vimrc listing are:

rw-r--r– This string is actually an expression of three different sets of permissions:

  • rw-
  • r--
  • r--

The first set of permissions applies to the owner of the file. The second set of permissions applies to the user group that owns the file. The third set of permissions is generally referred to as "others." All Linux files belong to an owner and a group. When permissions and users are represented by letters, that is called symbolic mode.

3.2. Octal

When Linux file permissions are represented by numbers, it's called numeric mode. In numeric mode, a three-digit value represents specific file permissions (for example, 744.) These are called octal values. The first digit is for owner permissions, the second digit is for group permissions, and the third is for other users. Each permission has a numeric value assigned to it:

r (read): 4 w (write): 2 x (execute): 1

4. Vim

4.1. configuration

create ~/.vimrc and put config for vim in there

4.2 Synchronous scroll bind

In each window that should scroll simultaneously, enter the command:

:set scrollbind

You can enter scb as an abbreviation for scrollbind, and the ! flag causes :Set to toggle a boolean option

:set scb!

5. Supervisor And Systemd

5.1. sytemd

systemd is a system and service manager for Linux operating systems. It is designed to be backward compatible with SysV init scripts, and provides a number of features such as on-demand starting of daemons, system state snapshots, process tracking, and more.

5.1.1. Pros of systemd

  • Built-in with the OS: Since systemd is built into most modern Linux distributions, there are no additional dependencies required to manage services.
  • Ease of use: systemd is straightforward to use and manage. Services can be managed just like system services, making it easy to start, stop enable or disable services.
  • No learning curve: If you’re already familiar with Linux, you’ll find managing processes with systemd to be intuitive.

5.1.2. Cons of systemd

  • Requires superuser privileges: To manage processes with systemd, you need superuser privileges. This might not be ideal in environments where you want to limit the use of superuser privileges.
  • No web interface: Unlike Supervisor, systemd does not provide a web interface for managing processes.

5.1.3 list all services

sudo systemctl --list-units

5.1.4 check status of particular service

sudo systemctl status <service-name>

5.2. supervisor

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems. It provides a flexible and robust way of managing processes.

5.2.1. Pros of supervisor

  • User-level process management: With Supervisor, any user can manage processes, eliminating the need for superuser privileges.
  • Web interface: Supervisor provides a web interface for easy process management. This can be a significant advantage if you prefer a GUI over command-line interfaces.
  • Cross-distribution compatibility: Supervisor works on any Linux distribution, providing flexibility and preventing vendor lock-in.
  • Process grouping and priority setting: Supervisor offers more flexibility in managing processes, such as grouping related processes together and setting process priorities.

5.2.2. Cons of Supervisor

  • Additional dependency: Unlike systemd, Supervisor is not built into the operating system and needs to be installed separately.
  • Learning curve: If you’re not familiar with Supervisor, there might be a learning curve to understand and use it effectively

6. Special Files In Linux

6.1. /etc/hosts

To learn more about this file click here

6.2. /etc/passwd

souldiv:x:1000:1000:,,,:/home/souldiv:/bin/bash

1:2:3:4:5:6:7
  1. Username: It is used when user logs in. It should be between 1 and 32 characters in length.
  2. Password: An x character indicates that encrypted password is stored in /etc/shadow file. Please note that you need to use the passwd command to computes the hash of a password typed at the CLI or to store/update the hash of the password in /etc/shadow file.
  3. User ID (UID): Each user must be assigned a user ID (UID). UID 0 (zero) is reserved for root and UIDs 1-99 are reserved for other predefined accounts. Further UID 100-999 are reserved by system for administrative and system accounts/groups.
  4. Group ID (GID): The primary group ID (stored in /etc/group file)
  5. User ID Info (GECOS): The comment field. It allow you to add extra information about the users such as user’s full name, phone number etc. This field use by finger command.
  6. Home directory: The absolute path to the directory the user will be in when they log in. If this directory does not exists then users directory becomes /
  7. Command/shell: The absolute path of a command or shell (/bin/bash). Typically, this is a shell. Please note that it does not have to be a shell. For example, sysadmin can use the nologin shell, which acts as a replacement shell for the user accounts. If shell set to /sbin/nologin and the user tries to log in to the Linux system directly, the /sbin/nologin shell closes the connection.

7. Ansible Commands

7.1. ansible-doc

Used for looking up information on ansible modules and roles.

ansible-doc shell

7.2. ansible-playbook

Inventory File Example: staging.yml

[vm]
node1 ansible_host=172.30.14.44

[cloud]
ec2_ubuntu ansible_host=13.239.117.98

command for running playbook for staging inventory but running for cloud group

ansible-playbook -i staging -l cloud playbooks/example.yml --key-file <private/pubkey>

-l - Flag for selecting the group (like cloud or vm) -i - Flag for selecting the inventory file

8. Poetry

8.1. Init poetry project

poetry init <name-of-project>

8.2. add dependencies to project

poetry add <name-of-library>

8.3. remove dependencies from project

poetry remove <name-of-library

8.4. list all envs in current directory

poetry env list

8.5. Start and exit python shell using poetry

Run command within directory poetry shell

Run command anywhere exit or deactivate

9. Teleport

9.1 Login to the cluster using tsh

tsh login --proxy=teleport.example.com --user=a-teleport-user

9.2 Teleport Ansible Guide

Create a file ansible.cfg:

[defaults]
host_key_checking = True
inventory=./hosts
remote_tmp=/tmp

[ssh_connection]
scp_if_ssh = True
ssh_args = -F ./ssh.cfg

create ssh config to use ansible over tsh

tsh config > ssh.cfg

lets say you have inventory called staging and you want to create a group called teleport to add a host, the host should be written in the following format.

Inside staging:

[teleport]  # group name
instance_host_name.cluster_name  # instance host name

10. AWS

10.1 STS

10.1.1 Get user identity

aws sts get-caller-identity

10.2 EC2

10.2.1 List all images owned by caller account

aws ec2 describe-images --owners self

10.2.2 Export a VM directly from AMI

VHD is for Hyper-V VMDK for Vmware

aws ec2 export-image --image-id ami-id --disk-image-format VMDK --s3-export-location S3Bucket=my-export-bucket,S3Prefix=exports/

For more information click here Required permissions for VM import/export is here

10.2.3 Check Exported Task Status

get export-image-task-ids from the previous command.

aws ec2 describe-export-image-tasks --export-image-task-ids export-ami

Or Describe all export image tasks

aws ec2 describe-export-image-tasks

10.3 S3

10.3.1 List S3 buckets available to your account

aws s3 ls

10.3.2 Create presigned link for downloading bucket object

aws s3 presign s3://conductor-vms/vms/export.vhd

11. WSL

11.1 Settings Configuration in WSL

The wsl.conf and .wslconfig files are used to configure advanced settings options, on a per-distribution basis (wsl.conf) and globally across all WSL 2 distributions (.wslconfig).

Learn more about it here here

11.2 .wslconfig

References

  • Linux Journey for linux tutorials for absolute beginners link
  • OverTheWire wargames link
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment