Author: mark_user

  • Backup to a Remote Server using Bash

    Backup to a Remote Server using Bash

    Introduction

    A great way to backup your Linux files is through automation. Linux contains many built in commands that can be used to automate this process. In this article, we will write a simple bash script to backup critical files to a remote host.

    For this to work, a user account is needed on both hosts to transfer the files. The sending server will need the private SSH key and the receiving server will need the public SSH key. These keys are used to automatically authenticate to the remote server. Creating and deploying a SSH key pair for authentication is beyond the scope of this article.

    The script will use the scp command as it uses SSH underneath the hood. This means it will natively automatically check for an identity file stored under the user’s account at ~/.ssh/config. When creating the SSH credentials, do not add a passphrase to the private key.

    Secure copy (scp) command is good if you just want to copy a single file. You can use the -R switch to copy all files in a directory. If you need to backup multiple files, you can modify the script and just add additional variables, update checks, and then add more scp commands.

    If you need to transfer entire directories consider using the rsync command. Rsync like scp should automatically use the ~/.ssh/conf file by default. However, rsync sometime has issues using an identity file, so do proper testing.

    Finally, Consider setting up a CRON job to run the script nightly.

    Procedure

    Lets build a script on server1 and we will be connecting and coping our file to server2. First, set up the head of the script and call bash and add any comments.

    Next, let’s set up the script variables.

    Next, lets run a check on the remote server and test if the file path exists!

    Finally, copy the file to the remote server.

    Lastly

    Create a cronjob to run nightly at 9:00 PM.

    References

    https://unix.stackexchange.com/questions/127352/specify-identity-file-id-rsa-with-rsync

  • Manage Users & Groups

    Manage Users & Groups

    Introduction

    Managing user and groups in Linux is an essential administrative task. We will cover the manual method to create a new user and add that user to a group. Also, remove that user in a multi step process. Finally, we will cover selective tasks concerning Groups.

    There is a separate article concerning adding the same user to multiple servers using Ansible. We will not duplicate that information here.

    Create a User Account

    First, get a list of all users on the host.

    Now, create a new user called mark.

    Remove a User Account

    If we want to fully remove a user we will need to follow several steps.  Also, the user’s files on other remote systems will need to be manually searched for and removed or have the ownership changed.

    You can not remove an account if the user is currently logged in. This is because the user will have existing running processes.

    Lock the user’s account, so they can not login.

    Backup the user’s account data.

    See if the user has any running processes and kill them.

    Remove the user’s crontab jobs.

    If necessary, cancel any running print jobs. (Linux print remove).

    Assign Mark’s files to another user named Tom.

    -exec = execute script.
    chown tom:tom = Change ownership to tom.
    {} = for each file that is found
    \ = Terminate script when done.

    Since we will be deleting the home and mail spool directories, and we have already made a backup, we do not need or want to search those directories by changing the file permissions right now. This will prevent us from deleting them and lead to orphan files. So we will modify the above command to exclude those. We only want to change ownership for files outside of those two directories.

    -p = path to exclude
    -prune = Do not search specified path.
    -o = OR

    Finally, remove the user’s account. Some users like to use the deluser command and some like to use the older userdel command. They do essentially the same thing. I am using deluser, as it is a higher level command and also deletes the user’s /home directory and mail spool.

    –remove-home = removes /home and /var/spool/mail.
    –remove-all-files = removes /home,/var/spool/mail, and attempts removal of all other files.

    Lastly, check to verify there are no remaining files assigned to the user.

    Create a Group

    Lets create a group called analyst.

    Add a User to a Group

    Let’s add the new user mark to the analyst group. NOTE: You must be a member of a group before you can add others to the same group.

    -a = Append
    -G = Group

    NOTE: For centOS systems, need to run the command ‘$ usermod -aG wheel <user>’

    Review Group Memberships

    Lets see who is in the group analyst and see what groups the user ‘mark’ is in.

    Remove a User from Group

    We can remove the user mark from the analyst group. The command is not as clean as adding a user. It is not obvious that these are group commands. You can use either of the two below commands.

    -d = delete user from group.

    Change a File’s Group Permissions

    Next, lets change ownership of a file to the group ‘analyst’. Although there are other methods, I prefer the one shown below, as it is more granular. After you change a file’s group permissions, users will not be able to access the file until they log off and back on again.

    References

    https://linuxize.com/post/how-to-add-and-delete-users-on-ubuntu-18-04/

    https://www.digitalocean.com/community/tutorials/how-to-add-and-delete-users-on-an-ubuntu-14-04-vps

  • Tracking Communications with Netstat & TCPView

    Tracking Communications with Netstat & TCPView

    Netstat on the Command Line

    Netstat is a built in utility typically used to troubleshoot remote connection issues. It is also be used to see what external IPs and URLs your computer is actively communicating with and to what ports are passively open waiting for a connection. To run netstat, launch the Command Prompt in administrator mode.

    It is important to remember that running the command is a one and done. It does not auto refresh unless you tell it to using a switch. To get a list of switches, run “netstat /?“. The most important for netstat switches are:

    -a = Display all connections and listening ports
    -b = Display the executable involved.
    -f = Display FQDN.
    -n = Display IP and ports in number format.
    -o = Display process ID associated with the connection.
    -r = Display routing table.

    Let’s go over some commands. First, ‘netstat -ab’ is unusually slow to provide results and the results are not in a easy to read format. For this reason many users do not like it. But it will provide the process name that opened or established the connection.

    Next is ‘netstat -ano’. It provides very fast results, but does not provide the name of the running service. However, you can get the process ID’s and then lookup the running process in task manager to find the service or executable. Finally, you can lookup up the foreign addresses in an online URL lookup to find the ‘whois’ or the IP registration.

    Display the TCP active connections with the FQDN.

    You can pipe the results from netstat to include only specific outputs.

    You can pipe to exclude results by using the /V switch.

    Using TCPView

    Think of TCPView (or the older CURRPorts) as a netstat GUI interface. TCPView is produced by Microsoft System Internals and is actively supported. It is easier to read then the command line, auto refreshed every 2 seconds, and provides both fast results as well as the process name.

    You can quickly track down what service is attempting to make an external connection, what remote IP or URL it is communicating with, and over what port. The results can be combined with some google searches to get a complete picture. I find it easier than trying to interpret the netstat command line results.

    The color scheme is tricky. Red means a connection is about to close, green means a connection was just opened, and yellow means a connection has just refreshed. Notice the green bar in the picture below.

  • Thanksgiving Cranberries

    Thanksgiving Cranberries

    Ingredients

    1 cup sugar
    1 cup water
    4 cups fresh cranberry (12 oz package)
    Optional: pecans, orange zest, raisins, currants, blueberries, cinnamon, nutmeg, allspice

    Place cranberries in colander and rise them. Pick out and discard any damages cranberries. Put the water and sugar in a medium saucepan on high heat and bring to boil. Stir to dissolve the sugar. Add cranberries to the pot and return to boil. Lower the heat and simmer for 10 minutes or until most of the cranberries have burst.

  • Practical Communication Tips for Men

    Effective Speaking for Men in Relationships

    If a woman is speaking in a calm manner, and detailing some minor issue, they are not looking for you to “solve” their problem. They do not want logic based advice or tips on “how to fix” the issue. They just want: reassurance that they are not wrong; confirmation they are not a bad person; and reinforcement that (gosh darn it) people like them. A women craves constant affirmation.

    Your only job is to remain calm, listen, and provide them with positive feedback. Here are some comments I use on a regular basis to provide my wife with assurance.

    • You deserve better …
    • I can not believe they treated you that way …
    • What you are asking is not unreasonable …
    • You are so brave …

    These comments are particularly well suited for conversations that I am not paying attention too. But, they can also be dropped into any conversation.

    If a women is yelling, deflecting responsibility, talking in circles, or explaining in mind numbing details, then it is a more serious matter. Under these terms, what a women wants is for the man to assume full responsibility for the situation and fix the problem. However, she still wants to retain the right to veto any potential actions. She reserves this right in case the solution makes her look bad. In other words, you have to fix the issue, while pretending you are not actually fixing the issue (and also, maybe giving her the credit).

    You can make comments, along the lines of:

    • Don’t worry WE will figure this out together …
    • WE will ask [ third party ], if they have an answer …
    • WE will solve this tomorrow …

    I hope this article provides readers with a keen insight into how to effectively communicate with a woman. By listening, providing positive affirmation, and taking over responsibility when necessary (to bail her out of what ever situation she got herself in). You will have a long and happy relationship.

    Cheers!

  • TCP/IP Stack Using the OSI Model

    TCP/IP Stack Using the OSI Model

    The OSI model of the TCP/IP stack has become the standard model for understanding how a packet flows into or out of a system. Here is my understanding.

    7. Application Layer – User can interact. (GUI).

    6. Presentation Layer – Encryption (SSL, SSH, IMAP, etc).

    5. Sessions Layer – Manages sessions – (API’s, sockets).

    4. Transport Layer – Assemble bits into packets. End to end. (TCP, UDP).

    3. Network Layer – Transfers the data packets. (IP, ICMP).

    2. Data Link Layer – Addressing & Physical Transmission. (Ethernet, switch, Bridge).

    1. Physical Layer – Wires, hubs.