# Linux Administration notes

### User management:

* Switch user: $su - &lt;username&gt;
    
* If the username is not given then it will be treated as root.
    
    $su - is equivalent to $su - root
    
* After executing the switch command you need to enter the password.
    
* If you are switching from root user to any user password is not required. Note from the user is important here i.e. current user, not the user to which you are switching.
    
* Let's say you are logged into a machine with ec2-user, then switch to the root using **su -** then you need to enter the password of the root user.
    
* After some time if want to switch back to ec2-user then use **su - ec2-user**. Here password is not required to enter because your current user was root before switching.
    
* If you want to make a user superuser then add him to the sudoer\`s group(wheel in redhat distors and sudo in debian distros)
    
    **sudo usermod -aG sudo username**
    
* Another way is to update /etc/sudoers file, by executing visudo command
    
    \# User privilege specification
    
    **username ALL=(ALL:ALL) ALL**
    
* If you want to get rid of password prompts for a user everytime you switch back then use
    
    **username ALL=(ALL:ALL) NOPASSWD:ALL**
    
    ```c
    # User privilege specification
    root    ALL=(ALL:ALL) ALL
    
    # Members of the admin group may gain root privileges
    %admin ALL=(ALL) ALL
    k8    ALL=(ALL:ALL) ALL
    # Allow members of group sudo to execute any command
    %sudo   ALL=(ALL:ALL) ALL
    
    bmaddi  ALL=(ALL:ALL) NOPASSWD:ALL
    k8    ALL=(ALL:ALL) NOPASSWD:ALL
    ```
    
    ### AWK vs CUT:
    
* Both `awk` and `cut` can be used to extract fields and substrings from text, but they have some key differences:
    
    **Delimiter**
    
    `cut` uses a single character as the field delimiter by default (`\t`). You can specify a custom delimiter using `-d`:
    
    ```c
      cut -d ',' -f1  # For comma-separated file
    ```
    
    `awk` uses whitespace as the default delimiter, which includes spaces, tabs, and newlines. You can specify a custom delimiter using `-F`:
    
    ```c
      awk -F'|' '{print $1}' # For pipe-delimited file
    ```
    
    **Empty Fields**
    
    `cut` will print empty fields if they exist, while `awk` will suppress leading whitespace and empty fields by default.
    
    ```c
      echo "abc   def" | cut -f2    # Prints empty field
      echo "abc   def" | awk '{print $2}' # Prints def
    ```
    
    Flexibility
    
    `awk` is more flexible as it allows you to use regular expressions as delimiters. You can also loop through columns using a `for` loop.
    
    ```c
      awk -F'[ ]+' '{for (i=1;i<=NF;i++) print $i}'
    ```
    
    You can use functions like `substr()` to extract substrings:
    
    ```c
      awk '{print substr($1,1,6),$2}'
    ```
    
    **Performance**
    
    `cut` is typically faster than `awk` since it is a simpler tool designed for a single purpose.
