Linux Daily Uses & Advance Commands – Part 1

 





1. How to remove files older than 7 days by creating a cron job to run every night?
find /var/log/security -type f -mtime +7 -exec rm -rf {} \;
Cron – /etc/crontab (show the cron format)


To add the command in crontab file
crontab -e
0 0 * * * /bin/find /var/log/security -type f -mtime +7 -exec rm -rf {} \;


2. Run a command that shows all the line except any lines starting with a character # in the file?
    cat filename | grep -v ^#  (character name you want to omit)


    3. Which 2 files contain default values when creating a user with useradd command? 


    First File: /etc/default/useradd

    Second File: /etc/login.defs
    cat /etc/default/useradd

    grep -v ^# /etc/login.defs | grep -v ^$ [first # symbol for omit and second $ for omit space)
    Password max, min, warn days | password length | UID and GID min, max | umask | encryption method



    4. What is the command to create user with a pre defined uid, shell and home directory?
      useradd -m -d /home/username -s /bin/bash -u 9000 username
      grep username /etc/passwd


      5. How to delete a user with his home directory?

      userdel -r username


      6. How to create a user specifying a primary/secondary group?
      useradd -g test -G wheel username [small g for primary group and capital G for secondary group)
      id username [to check user information]


      7. How to change primary group of any user?
        usermod -g primarygroupname username


        8. How can you give a normal user all the root level privileges? 

        The file path is /etc/sudoers (But we never change the sudoer file directly, we edit the file with visudo command)
        visudo
        Change the file line # # Allow root to run any command anywhere
        root  ALL=(ALL) ALL
        gautam ALL=(ALL) ALL (Add this line below the root line)


        9. How can you give sudo access to any user without asking him to provide password every time he runs a command?
        visudo
        Add the line below – # # Same thing without a password [we need to add user in wheel group)

        # %wheel ALL(ALL) NOPASSWD: ALL
        linux ALL(ALL) NOPASSWD: ALL


        10. How to view the user’s login and logout details?
          last 
          last username (particular user detail)

          Popular posts from this blog

          How to Convert PEM KeyFile into PPK KeyFile and vice versa?

          How to transfer Google Cloud Storage One account data to Google Cloud another account?

          MySQL Commands, Administration, Backup, Restore