Tag: linux

  • Synchronize the Time & Date in Ubuntu

    Synchronize the Time & Date in Ubuntu

    Understanding Time

    Time is a critical component of every computer. When to run updates, launch scheduled tasks, or just to keep user’s informed, are all dependent on time.

    Every computer has a built in clock on the motherboard, usually powered by a battery, to keep track of the time. This hardware based clock is called the Real Time Clock (RTC) and is used to power the human readable ‘system time’, based on time zones.

    • System Time = Uses Time Zones
    • RTC = Real Time Zone = Uses Hardware clock

    Unfortunately, the hardware clock will always eventually get out of sync with real time and need to be adjusted. This occurs because of power outages, battery on the MB dies, or other reasons.

    Ubuntu Settings

    Two commands are used to control the date and time. First, the ‘timedatectl‘ command is used to set the time zone. Next, the ‘timesyncd‘ command is used start or stop the sync service and to turn on or off the network time protocol (NTP).

    An older process to manage these functions is called ntpd. While this process is still supported, it is recommended to use the newer methods.

    Check Your Time Zone

    In the United States, there are three primary time zones.

    • UTC (Universal Time Coordinated)
    • EDT (Eastern Daylight Time) = 2nd Sunday March to 1st Sunday in Nov. = 4 hr behind UTC
    • EST (Eastern Standard Time) = 1st Sunday Nov to 2nd Sunday March. = 5 hr behind UTC

    Get a list of available time zones

    Change the Time Zone

    Check the Sync & NTP Services

    Note: Typically, the Real Time Clock or ‘RTC in local TZ’ should always be set to ‘no’. This is due to that most of the US uses spring/fall time changes and the RTC does not know anything about that.

    Turn on the ‘System clock synchronized’

    Check that there is a name server or two listed in the configuration file. The entries should be space separated.

    Add the following lines under [Time]:
    NTP=ntp.myserver.com time.nist.gov

    Next, run the below command.

    Turn on the ‘NTP Service’

    Verify the Clock is Synced

    Ref: https://www.linuxfordevices.com/tutorials/ubuntu/set-up-time-synchronization-ubuntu

    Ref: https://opensource.com/article/20/6/time-date-systemd

    Ref: https://ubuntu.com/server/docs/about-time-synchronisation

  • Analyze Network Traffic using TCPDump

    Analyze Network Traffic using TCPDump

    The Basic

    TcpDump is a command line network monitoring tool for viewing packets, in real time, as they pass through the server network interface. Specifically, it can be used to view and record packets going into and out of a network interface and can be used to conduct all sort of analysis.

    I use this tool to see if logs are actually coming into a server, typically over syslog, and what port are they being received on or are going to.

    Inbound traffic from IP 10.16.28.143

    -i = listen on network card ens160
    -n = do not translate IP address to DNS names
    -nn = do not translate IP to DNS names or ports to service names
    -v = verbose

    Inbound traffic to a specific port

    Either inbound or outbound traffic to/from port 12011

    Outbound traffic to IP 10.16.1.121

    Outbound traffic on port 443

    Only capture TCP outbound traffic going to IP 10.10.192.30 on port 443.

    See if you are getting syslog data

    Only capture data coming from subnets 10.10.149.0/24 or 10.10.153.0/24 and arriving on port 514.

    Outbound traffic going to port 443

  • Customize Your VIM Settings

    Customize Your VIM Settings

    Introduction

    VIM is a popular Linux text editor program. Users can customize VIM by creating a .vimrc file in their home directory. Then, when launching VIM, it will load any variables that were preset in this file. You’ll need to set the file permissions of the .vimrc file to rw-rw—-.

    A sample .vimrc file

    In my opinion, the two most useful commands are to set line numbers on the file and set the color scheme, to make files easier to read. Here is how you set them up in the .vimrc file.

    Get a list of available color schemes

    VIM comes with several color schemes loaded by default. You can also download others. To check what defaults are available, use this command.

    References

    https://phoenixnap.com/kb/vim-color-schemes

    https://thelinuxcode.com/vimrc_tutorial

  • File, Processes, & System Details in Ubuntu

    File, Processes, & System Details in Ubuntu

    Introduction

    When working with systems, for troubleshooting, performance tuning, or regular maintenance, it’s often essential to gather key system information. This includes details such as the operating system version, hardware specifications, running processes, or file system disk usage. You might need to monitor CPU usage in real time or check how much storage space is available on your drives.

    Fortunately, there are a set of powerful command-line tools that make this process quick and efficient. Below, we’ll explore some of the most useful commands to help you monitor and manage your system effectively.

    What is OS Linux version

    Get the installed memory (RAM)

    Get a CPU count

    Get the hard drive details

    Get disk space usage by directory

    disk space used by the specified files and for each subdirectory.

    Monitor Processes using TOP

    Just run the command ‘top’ or ‘htop’ , if it is installed, to get real time Mem and CPU usage and see what process are resource hogs.

  • Understanding User Crontabs

    Understanding User Crontabs

    Introduction

    Crontab stand for CRON table and it is the primary task scheduler for Linux. The CRON daemon runs every minute and checks each user’s crontab (and the system crontab) for scheduled jobs. CRON will auto start on reboot and record to syslog natively when a job runs. CRON will send “standard out” and “errors” to the scheduled user’s mail account at /var/spool/mail/user. By default, CRON uses the bash shell to execute commands.

    There are two types of crontabs; user crontabs and system crontabs. The system wide CRONTAB jobs are stored in /etc/crontab. It is used to execute scripts that apply to the system change or all users. It requires an additional field in the crontab called a user field. This article will be focused on setting up a user’s crontab.

    If your script runs successfully from the command line, but not when it is run as a CRONTAB job, it is because crontabs are executed by the user CRON. That user has limited rights and does not have the same environmental variables as a regular user. It is recommended to always use absolute paths for shell, commands, and scripts, in your crontab.

    Understanding crontab

    Key Terms:

    • Min = 0 to 59
    • Hour = 0 to 23 (Hours are in Military time)
    • Day of Month = 1 to 31
    • Month = 1 to 12 or Jan, Feb, Mar
    • Day of Week = 0 to 6 (0 is Sunday) or mon, tue, wed
    • @reboot = run once after reboot

    Pattern Matching:

    • * = match everything
    • Range = 0-4 or jan-jun
    • List = 1,3,7,16 or mon,tue,wed
    • Step Values = 0-23/2 = run the job every two hours for 24 hours.

    File Locations:

    • System file =/etc/crontab
    • System jobs = /etc/cron.d/ (Location to store system scripts)
    • System jobs = /etc/cron.daily (Location to store system scripts)
    • System jobs = /etc/cron.weekly (Location to store system scripts)
    • System jobs = /etc/cron.hourly (Location to store system scripts)
    • User’s crontab (debian) = /var/spool/cron/crontabs/<user>. (DO NOT EDIT DIRECTLY)

    Troubleshooting:

    • Crontab Logs = /var/log/syslog (logs, i.e. did command run??)
    • Crontab Job Results (debian) = /var/spool/mail/<user> (output and errors)
    • Verify cron is running = sudo systemctl status cron (Is cron running ??)

    List the current user’s crontab

    Edit current user’s crontab

    NOTE: Some documents say, after you save and install a new CRONTAB, you need to reload the CRON service by running “service cron reload”. But, other documents say you do not to perform this action.

    Remove current user’s crontab

    List another user’s crontab

    Run a job at a specified time

    NOTE: CRON uses military time, which is using hours 0 to 23.

    Potential syntax errors

    Be careful when writing cron jobs. For the day of week and day of month fields, crontab should be interpreted as AND statements. The command will run when either field matches the current time! This example would not run a script on the first Monday of the month. Rather, this job runs on the first day of the month and every Monday.

    Skip values can only operate within the time period they´re attached to. The above will not execute every 35 minutes. Rather, it will execute at 0 minutes and 35 minutes each hour.

    Start a program on server reboot

    “/usr/bin/perl -w” mean to enable and print warning messages.

    Run a script and email the results

    Send stdout&err to syslog w tag “ossec”

    Redirect screen & error output

    Screen output and errors are recorded in the user’s mailbox at /var/spool/mail/<user>. When scripts run overnight, output to the screen (stdout) is not needed. It is common to send standard out to /dev/null and errors to a custom log file. You will need to ensure that the log file does not grow out of control.

    2>&1 means to send any errors to the same location as standard out. Order matters! you can not send errors to location that does not exist. Be sure to identify the location of the screen output first.

    References

    https://krisjordan.com/blog/2013/11/04/timesaving-crontab-tips

    https://www.generateit.net/cron-job/

  • Backup Files to S3 using Bash

    Backup Files to S3 using Bash

    Description

    A bash script will be used to copy a file from a Linux server to an S3 bucket. Next, it will run a checksum on the results to verify the upload. Finally, it will output the local file size, the local etag , the aws file size, and the aws etag value for easy comparison. This should give the end user enough confidence that the uploaded file has maintained it’s integrity.

    The script assumes you have an account in AWS with a login credentials. You have the cli AWS tools and credentials downloaded to /home/user/.aws/config and /home/user/.aws/credentials. These two files are needed to successfully authenticate to the s3 bucket.

    Amazon Web Service S3 Bucket

    AWS is a flat file system. There are no folders or directories. The “full” name of a file includes all the subdirectories as well. i.e. “/file1/file2/file3.txt” is the file name and not “file3.txt”. AWS will show all subdirectories as folders in the console, for ease of human navigate.

    Begin

    Start the script by defining that it will run as bash and add any notes to the head.

    Send any log output to a custom log file and code to exit the script if any commands in a pipeline fails.

    Get the number of processing units available and add it to a variable.

    Define the remaining local variables.

    Define the AWS variables.

    When a file is uploaded to AWS, it will calculate what is called an ETAG value. This is the checksum value of the upload file. To verify file integrity, we will compare the uploaded aws calculated ETAG against the local file’s calculated ETAG.

    The ETAG will match a true md5 hash value if the file size is < 5 GB. If the file is > 5 GB, the aws ‘cp’ command will automatically break the file into 8 MB chunks and upload 4 threads of data simultaneously, until the upload is complete. Each uploaded thread will have an md5 calculated. The resulting ETAG will be a sum of all the uploaded data chunks, rather than a true md5 hash against the completed file.

    In order to compare the ETAG’s and verify they match, we must calculate the local file’s ETAG value. Then compare that value to the value calculated by AWS. The script contains two methods to calculate the ETAG value, you will need to review and consider what is needed. In my case, I always know the value I will upload will be > 5 GB.

    To calculate the local files ETAG value, for files < 5GB. use:

    For files > 5 GB, we can use the code from https://gist.github.com/rajivnarayan/1a8e5f2b6783701e0b3717dbcfd324ba.

    Next, we will copy the files to the s3 bucket using the ‘cp’ command. We will be using the CLI copy command, rather than the s3api command, as the api can not handle file’s large then 5 GB. Copy the content to S3 and tell AWS that the data is just a plain text file.

    Get the ETAG value that AWS calculated during the upload.

    Next, we will get both the local file size and the uploaded file sizes.

    Finally, display the file sizes and the ETAG values of both the uploaded file and the local file side by side for comparison.