/42_born2beroot

Project part of 42 school curriculum

Primary LanguageShell

42 Born2beRoot

Goals:

Project aimed to learn more about system administration, and Linux OS The main topics on this project covers:

  1. Booting VMs
  2. LVM partitions | Filesytem Hierarchy Standard
  3. SSH services
  4. Firewall settings
  5. Sudo configuration
  6. User settings | Groups configuration | password policies
  7. CRON jobs

1. Setting Virtual Machine ISO and boot on VM

1.1.Download Oracle Virtual Box


- Creating a new VM: - 2048MB RAM (The default is 1024MB, but if your computer has enough RAM you can dedicate more so it can be faster) - Set the HD to 30.8G (Implementing the Bonus, otherwise it would be set 8GB) - Storage on physical disk can be: - Dynamically allocated - Pros - it can grow in size - Cons - the pyshical disk can become highly fragmented, the performance may degrade - Fixed size - Pros - it can be faster to run https://techgenix.com/fixedsizevs-dynamicallyexpandingvirtualharddisks/ - Before booting - On disk -> Controller: IDE -> set Debian iso file ![Disk settings](./img/disk_settings.PNG) - On Network - set to "Bridged Adapter", so it can have internet access https://linuxhint.com/use-virtualbox-bridged-adapter/

1.2. Boot setup

1.2.1. Set hostname and domain name

2. Partition Disk - understand LVM use, and File System Hierarchy


- __Linux Filesytem Hierarchy Standard__ - Common partitions when setting Linux ![file sytem hierarchy](./img/file_system_hierarchy.PNG)
- Linux File System/Structure Explained! - https://www.youtube.com/watch?v=HbgzrKJvDRw&

- __`lsblk`__ - Linux command to display information about disk devices in a system. - https://linuxhint.com/linux-lsblk-command-tutorial-for-beginners/ - what is the MAJ:MIN column - https://www.oreilly.com/library/view/linux-device-drivers/0596000081/ch03s02.html
- Miscelaneous information: - Difference between Primary and Logical Partition - It's more a legacy from how old OS were set (Mainly DOS and Windows) where it could have only 4 partitions to load an operational system. Where on Linux it's possible to have multiple partitions one for each File system (reducing potential data loss) - sda 1 to 4 it could be considered primary partitions, and sda 5 and higher on are extended partitions. - https://www.wikiwand.com/en/Disk_partitioning - https://askubuntu.com/a/1207738 - Difference creating the disk at beggining or end of avaiable space - Didn't find any explanation on where it could be better to create at the end, but emperical tests suggests it might have a poor performance on the disk. - https://askubuntu.com/questions/56883/is-having-the-swap-partition-at-the-beginning-better-than-at-the-end - Difference on ext4, ext3 and ext2: - types of filesystems created for Linux. - ext3 and ext4 enables journaling (able to log changes in the machine) - https://www.learnitguide.net/2016/08/difference-between-ext2-ext3-and-ext4.html
- __Expected result__ - After the manual setup the inital configuration of the VM, the hard disk should have the following structure [tip - take a snapshot of the state of the machine, or a copy of the VM to not need to go over this step again in case you end up doing something that doesn't how to fix or think in restarting the VM] ![Bonus](./img/bonus_partitions.PNG)

3. Setting SSH (Secure Socket Shell)


4. Setting Firewall


5. sudo

  • Sudo is a program to allow a sysadmin to give limited access to users and log root activity.
  • Till now all the project was using the root user, after this setup is possible to assign an user to sudo group can, and run commands by using the prefix sudo, and it's possible to finish the configuration of a machine in a remote local.

- https://www.sudo.ws/about/intro/ - https://man7.org/linux/man-pages/man8/sudo.8.html
- __Configure policies:__ - Create `/var/log/sudo` folder to keep sudo log when enabling on the policies - To set sudo policies and following best practices. Instead of editing the /etc/sudoers. The local changes were in the `/etc/sudoers/.d` using visudo (it checks for any misspelling before saving) - the path `/etc/sudoers/.d` is already included in the `/etc/sudoers` as default - requiretty means it can only runs sudo commands on logged-in terminal session. - https://stackoverflow.com/a/68008345/16518944 - secure_path - meant to resctrict which paths and in which order when running a executable with sudo - https://askubuntu.com/a/924048
- https://linux.die.net/man/5/sudoers

6. User settings

6.1. Creating new groups and assign users

  • List all users in machine cat /etc/passwd
    • the columns are
      • User name
      • Encrypted password (x means the password is stored in the /etc/shadow file)
      • User ID number
      • User's group ID number
      • Full Name
      • user home directory
      • Login shell
  • I didn't understand if the users needs to have the primary group as user42, but this is how it's possible to change without messing up https://www.smarthomebeginner.com/safely-change-primary-group-group-in-linux

  • List all groups in machine cat /etc/groups
    • columns are
      • group name
      • password
      • group id (gid)
      • group list


6.2. Password policies

6.2.1. Time related settings

  • To apply these rules we use the command chage for existing users and editing /etc/login.defs to apply for new users. • Your password has to expire every 30 days. • The minimum number of days allowed before the modification of a password will be set to 2. • The user has to receive a warning message 7 days before their password expires.

6.2.2. Password Strengh

- It should follow these policies. For this I optted to use 
  - Your password must be at least 10 characters long. It must contain an uppercase letter, a lowercase letter, and a number. Also, it must not contain more than 3 consecutive identical characters.
  - The password must not include the name of the user.
  - The password must have at least 7 characters that are not part of the former password.

7. Monitor / CRON jobs

7.1. Creating shell script that will need to run every 10 minutes

It should have these following information
  - The architecture of your operating system and its kernel version.
    - https://www.technologyuk.net/computing/computer-software/operating-systems/operating-system-architecture.shtml
  - The number of physical processors.
  - The number of virtual processors.
  - The current available RAM on your server and its utilization rate as a percentage.
  - The current available memory on your server and its utilization rate as a percentage.
  - The current utilization rate of your processors as a percentage.
  - The date and time of the last reboot.
  - Whether LVM is active or not.
  - The number of active connections.
  - The number of users using the server.
  - The IPv4 address of your server and its MAC (Media Access Control) address.
  - The number of commands executed with the sudo program.
All the commands for this are on monitoring.sh file
For displaying the information `wall` was used, it works as broadcast system to all users logged in the server.

7.2. Manage with crontab

- Variables on cron
   add cron-variables.PNG
- To manage cron service:
    - sudo systemctl enable cron.service
    - sudo systemctl start cron.service
    - sudo systemctl stop cron.service
    - sudo systemctl restart cron.service
    - sudo systemctl status cron.service
- https://www.cyberciti.biz/faq/linux-execute-cron-job-after-system-reboot/
- https://linux.die.net/man/5/crontab

8. BONUS - Setting wordpress

9. Miscelanous information learned throught the project

9.1. Difference between Linux distros (Debian vc CentOS)

  • Debian is a open source
  • CentOS is managed by

9.2. Difference between aptitude and apt

9.3. Difference between SELinux and AppArmor

  • AppArmor is a Mandatory Access Control(MAC) sytem which is a kernel Linux Security Model (LSM), limitating what each program can do or consume resources.
  • Differences - AppArmor is a bit more simple than SElinux, such as not being possible to have a Multi-Level-Security (MLS) and Multi-Category-Security (MCS), not being possible to keep separation between containers.
  • https://wiki.ubuntu.com/AppArmor
  • https://www.redhat.com/sysadmin/apparmor-selinux-isolation

9.4 What is TCP, UDP and sockets

9.5 What is tty (Teletypewirter)

Coding editor on command line

  • Since on this project all setup needs to be done on command line. I opted for vim as text editor
  • Ubuntu already comes with vi already installed, but doesn't have many features that has on vim. (highlight text colors!)
  • Cheatsheet - https://vimsheet.com/

- Sources -

Extra material

Course - for linux https://www.redhat.com/en/services/training/rh024-red-hat-linux-technical-overview?section=Outline

Top 10 Linux Job Interview Questions https://www.youtube.com/watch?v=l0QGLMwR-lY

Debian handbook - https://debian-handbook.info/browse/stable/