|
583 | 583 | fi |
584 | 584 |
|
585 | 585 | # Show us what we have to work on today |
586 | | -DOCUMENTS="/Users/jmartinez/Downloads/linux-essentials-practice/text-analysis" |
| 586 | +DOCUMENTS="/Users/rkumar/Downloads/linux-essentials-practice/text-analysis" |
587 | 587 |
|
588 | 588 | for doc in "$DOCUMENTS"/*.txt |
589 | 589 | do |
@@ -638,4 +638,211 @@ cat $FILENAME |
638 | 638 | - Develop |
639 | 639 | - Deploy |
640 | 640 | - Manage |
641 | | -- Retire |
| 641 | +- Retire |
| 642 | + |
| 643 | +## <span id="hardware"></span> Understanding computer hardware |
| 644 | + |
| 645 | +| Command | Purpose | |
| 646 | +| :--- | :--- | |
| 647 | +| `cat /proc/cpuinfo` | view processor details | |
| 648 | +| `free` | view RAM stats in bytes<br><br>`-m` = show in MB<br> `-g` = show in GB | |
| 649 | +| `dmidecode` | show details about motherboard, BIOS, processor, and RAM | |
| 650 | +| `lsblk` | view all block devices (e.g., HDD) attached to system | |
| 651 | +| `df` | view free disk space on HDD<br><br> `-h` = human readable format | |
| 652 | +| `du -h $path` | disk usage; human redable, directories only <br><br>`-a` = show files | |
| 653 | +| `top` | show stats on processor, RAM, and running processes | |
| 654 | + |
| 655 | +* Hard drives tend to be named sequentially, such as `/dev/sda`, `/dev/sdb`, etc. |
| 656 | +* Partitions are named sequentially, so partitions on sda will be called `sda1`, `sda2`, etc. |
| 657 | + |
| 658 | +## <span id="data"></span>Where data is stored |
| 659 | + |
| 660 | +### <span id="kernel"></span>The kernel |
| 661 | +- Core of any Linux installation. |
| 662 | +- Responsible for managing every piece of softare on a Linux computer, interfacing with the hardware. |
| 663 | +- The kernel launches __/sbin/init__, and init in turn launches child processes. |
| 664 | +- Linux manages these processes in the processes table, which we can access via __ps__ and __top__. |
| 665 | + |
| 666 | +### <span id="processes"></span>Linux processes |
| 667 | +- Every process has a __PID__. |
| 668 | +- Every parent process has parent ID (__PPID__) |
| 669 | + + The two parent processes are 1) systemd and 2) kthreadd |
| 670 | +- The kernel supplies process information to the __`/proc`__ directory so it can be available to the `ps`, `top`, and `free` commands. |
| 671 | +- We can use `ps` to identify running processes. Note that this command provides a static snapshot. |
| 672 | + + `-u $username` shows processes for that username |
| 673 | + + `-e` shows every process running from all users |
| 674 | + + `-H` show hierarchy of processes via indented output. E.g., `ps -eH` |
| 675 | + + `--forest` also shows process hierarchy. E.g., `ps -e --forest` |
| 676 | + + `-f` shows full format listing (all arguments a command is using while running). E.g., `ps -ef --forest` |
| 677 | + + `ps -u josue --forest` shows parent/child relationships for processes. |
| 678 | + + `ps u U josue` gives CPU and memory %. |
| 679 | + + `ps aux` the `u` adds the username column. There's so much output it's typically more practical to grep. |
| 680 | + + `kill -9 $PID` will kill a process |
| 681 | +- `top` is dynamic, as opposed to `ps`, which provides a static snapshot. |
| 682 | + + `-h` or `?` will display CLI usage info and exit |
| 683 | + + After running `top`... |
| 684 | + * `k` will prompt for the PID of the process to kill. |
| 685 | + * `M` sort by memory usage |
| 686 | + * `P` sort by CPU usage (default) |
| 687 | +- `free` generates a report on the system's memory status using __KB__ |
| 688 | + + The __Mem:__ line shows total RAM stats |
| 689 | + + The __-/+ buffers__ line shows the total memory used by the programs |
| 690 | + + __Swap:__ is hard disk space used as a adjunct to RAM. |
| 691 | + + The `-h` flag shows the information in human-readable measurements (MB, GB) |
| 692 | + |
| 693 | +### <span id="syslog"></span>syslog, klog, dmesg |
| 694 | +- Most system logs are stored in __/var/log/__ |
| 695 | +- Logs are closed daily and retained for several days |
| 696 | +- Reading most system log files requires root privileges |
| 697 | +- __boot.log__ records events from when the system boots |
| 698 | +- __messages__ is the main log file |
| 699 | +- __secure__ is the file that logs when users elevate their privileges or attempt/fail to log in |
| 700 | +- `grep sshd /var/log/*` |
| 701 | +- __klogd__ manages messages from the kernel separate from other programs. |
| 702 | +- `dmesg` will display messages from the kernel. This helps with tshoot of hardware or driver issues. |
| 703 | + |
| 704 | + |
| 705 | +## <span id="networking"></span>Networking |
| 706 | + |
| 707 | +### <span id="basic-networking"></span>Basic networking |
| 708 | + |
| 709 | +#### Important network tools |
| 710 | + |
| 711 | +| Tool | Purpose | |
| 712 | +| --- | --- | |
| 713 | +| `ping -c $num ` | testing connectivity | |
| 714 | +| `dig` | `dig www.pluralsight.com -t A` | |
| 715 | +| `nslookup` | `nslookup -query=A www.pluralsight.com` | |
| 716 | +| `netstat` | list network connections | |
| 717 | +| `route` | current route/netwk settings | |
| 718 | +| `host $fqdn` | test DNS resolution | |
| 719 | +| `traceroute` | trace packet route | |
| 720 | +| `ifconfig` | current network settings | |
| 721 | +| `ip addr [show]` | current IP addr and network settings | |
| 722 | + |
| 723 | +- You set up DNS information in __/etc/resolv.conf__, but in some distros you're not supposed to edit this file. |
| 724 | +- You can see CentOS network config in __/etc/sysconfig/network-scripts/ifcfg-ens33__ or some other __ifcfg...__ file. |
| 725 | + |
| 726 | +#### Static IP address |
| 727 | +- Edit __/etc/sysconfig/network-scripts/ifcfg-ens33__ |
| 728 | + + BOOTPROTO="static" |
| 729 | + + `IPADDR="$addr"`` |
| 730 | + + `NETMASK="$mask"`` |
| 731 | + + `NETWORK="$subnet_id"`` |
| 732 | + + You can use CIDR notation on the IP address and omit the `NETMASK` |
| 733 | + + Remember to set DNS information in __/etc/resolv.conf__ |
| 734 | + + You add routes another way |
| 735 | + |
| 736 | +### <span id="routes"></span>Routes |
| 737 | +- `ip route show` shows the routes |
| 738 | +- `route` older method of showing routes |
| 739 | +- `netstat -r` same output as the `route` command, including routes to leave the LAN |
| 740 | +- Always set your destination gateways as IP addresses, not FQDNs. |
| 741 | +- Add routes via `route add -net $ntwk_id netmask $mask gw $rtr_addr` |
| 742 | +- Remove routes via `route del -net $ntwk_id netmask $mask gw $rtr_addr` |
| 743 | +- `Route add default gw $ip_addr` |
| 744 | +- The DNS server used is indicated in __/etc/resolv.conf__ |
| 745 | + |
| 746 | +### <span id="other"></span>Other commands |
| 747 | + |
| 748 | +| Command | Description | |
| 749 | +| --- | --- | |
| 750 | +| `netstat -a` | Lists listening & non-listening sockets | |
| 751 | +| `netstat -i` | Stats about the network interfaces | |
| 752 | +| `netstat -l` | Lists listening sockets | |
| 753 | +| `netstat -s` | Summary for each protocol | |
| 754 | +| `netstat -r` | Equivalent to `route` | |
| 755 | + |
| 756 | + |
| 757 | +## <span id="security"></span>Basic security and user types |
| 758 | + |
| 759 | +### <span id="root-std-users"></span>Root and standard users |
| 760 | +- Only the user and root can access the user's files. |
| 761 | +- `finger $username` gives info on a user (login, directory, name, and shell) |
| 762 | +- `id $username` gives user ID, group ID, group memberships |
| 763 | +- __/etc/passwd__ has list of users who can authenticate locally. Each line indicates the user, the user's pw (legacy field), UID, GID for default group, full name or comment, home dir, and default shell |
| 764 | +- __/etc/shadow__ has list of user passwords. Each line has the username, hashed pw, last modified field in Unix epoch, max days before a password must be changed, days ahead of max when the user will be prompted to change the password, the days to wait to disable the account if the password remains expired, and the expired field. |
| 765 | +- __/etc/sudoers__ has a list of sudoers |
| 766 | +- __/etc/group__ shows the group, password for the group, GID, and list of users who are members |
| 767 | +- `pwck` checks whether passwd and shadow are in sync. |
| 768 | +- `pwconv` adds any missing users from etc to shadow. |
| 769 | +- Root exists to perform administrative tasks and can therefore access all files. |
| 770 | +- `su` or `su -` let’s you become Root. `su - username` gives us a shell as that user, with their PATH var. |
| 771 | +- `sudo $cmd` is a per-command way to elevate privileges. |
| 772 | +- `who` = who is logged in |
| 773 | +- `W` shows logged in users and their processes. |
| 774 | +- `who -b` last boot time |
| 775 | +- `who -m` whostname and user associated with it |
| 776 | +- `who -r` our current run level |
| 777 | +- `who -q` number of users logged in |
| 778 | +- `who -a` all of the above |
| 779 | +- `last [$username]` who logged in, when, and how, in reverse chronological order |
| 780 | + |
| 781 | + |
| 782 | +## <span id="users-groups"></span>Creating users and groups |
| 783 | +- Every user acct has a UID and a textual username. |
| 784 | +- Different users could have the same UID and therefore identical rights to the same files. You should never do this. |
| 785 | +- `id` will show the current user’s UID and GID. You can also type `id $username` |
| 786 | +- `groups $username` shows the group memberships. |
| 787 | +- `groupadd <grp-name>` = add a new group |
| 788 | +- `useradd [-G $GID] -m -c "John Doe" jdoe` = add a new user. This command pulls defaults from __/etc/default/useradd__ |
| 789 | + + `-m` = create home dir |
| 790 | + + `-c` = comment; usually the user's full name |
| 791 | +- `userdel -r jdoe` = delete user and home folder |
| 792 | +- `sudo passwd $username` = change user's password. |
| 793 | + |
| 794 | + |
| 795 | +## <span id="permissions-ownership"></span> Managing file permissions and ownership |
| 796 | + |
| 797 | +### <span id="permissions"></span> File and directory permissions |
| 798 | + |
| 799 | +```bash |
| 800 | +☁ shell-scripting ll |
| 801 | +total 24 |
| 802 | +drwxr-xr-x 5 rkumar staff 160B May 21 16:22 ./ |
| 803 | +drwxr-xr-x 5 rkumar staff 160B May 21 08:48 ../ |
| 804 | +-rwxr-xr-x 1 rkumar staff 546B May 21 16:17 daily.sh* |
| 805 | +-rwxr-xr-x 1 rkumar staff 516B May 21 16:22 indexer.sh* |
| 806 | +-rw-r--r-- 1 rkumar staff 1.8K May 21 16:22 test1.txt |
| 807 | +``` |
| 808 | + |
| 809 | +- In the output above, the columns on the left indicate the user, group, and global permissions. |
| 810 | +- Permissions can be shown via symbolic (the letters above) or octal notation. |
| 811 | + + `r` = 4 |
| 812 | + + `w` = 2 |
| 813 | + + `x` = 1 |
| 814 | + + e.g., daily.sh has octal values 755 |
| 815 | + |
| 816 | +### <span id="modifying-permissions"></span> Modifying permissions |
| 817 | +- `chmod` = change mode of a file or directory, affecting permissions |
| 818 | + + `chmod u=rwx,g=rw,o=r $file_name` |
| 819 | + + `chmod o-rx daily.sh` = remove read and execute permissions from others |
| 820 | + + `chmod -R o-rx shell-scripting/*` = recursively alter permissions for files in a directory, but not the directory itself |
| 821 | + * Applying the command to the directory instead of including `/*` also alters the directory. |
| 822 | + + `chmod 600 test1.txt` = modify permissions on the file with rw permissions for the user and no permissions for the group or others |
| 823 | +- `chown $file_or_dir` = change ownership of a file/directory |
| 824 | + + `chown $username:$group $file` |
| 825 | + * You can omit the colon and the group if you're only changing the user. `chown $username $file` |
| 826 | + * You can omit the user if you're changing the group membership: `chown :$group $file` |
| 827 | + * Only root can change the user who owns a file |
| 828 | +- `chgrp` = change group ownership of a file/directory |
| 829 | + |
| 830 | + |
| 831 | +## <span id="special"></span>Special directories and files |
| 832 | + |
| 833 | +### <span id="symlinks"></span> Symbolic links |
| 834 | +- Symlinks are similar to windows shortcuts. They reference the path to a file, not the file itself. |
| 835 | +- If the original file/dir is moved, the symlink breaks. |
| 836 | +- `ln -s $src_name $link_name` one convention is to append `.lnk` to the end of the symlink name |
| 837 | +- `unlink $link_name` removes the symlink |
| 838 | +- Symlinks display an __l__ in the file descriptor column of the `ls -l` output. |
| 839 | +- Hard links are another pointer to the exact data on the hard disk. Deleting only one doesn't delete the file. |
| 840 | + + `ln $src_file $link_name` |
| 841 | + |
| 842 | +### <span id="special-files-dirs"></span> Special files and directories, and the sticky bit |
| 843 | +- __/var/tmp:__ Has temp files that do __not__ get deleted on reboot |
| 844 | +- __/tmp:__ Has files that get deleted upon reboot |
| 845 | + + Files in this directory have the sticky bit set, meaning that only users who created a file can delete that file even if everything has rwx permissions for this directory. This cam be seen via `ls -ld /tmp`, which gives `drwxrwxrwt. 8 root root 211 May 23 18:22 /tmp` |
| 846 | +- There are two ways to apply the sticky bit to a directory: |
| 847 | + + `chmod o+t $dir_name` |
| 848 | + + `chmod 1777 $dir_name` the `1` denotes the sticky bit. To remove it, use `chmod 777 $dir_name`, where the absence of the `1` implies a zero (`chmod 0777 $dir_name`) |
0 commit comments