Pages

Friday, January 22, 2016

dig Examples


Below are a couple of examples on how you can use dig. These are common examples and include queries such as mail exchange lookup, address record lookup, reverse dns lookup, etc.
you can always add the  '+short' option at the end of any dig command to keep the output manageable:

eg dig www.example.com +short

Perform a Basic DNS Dig
This is simply going to tell us what the IP address is for the domain www.example.com. The output will be an IP address which will be something like 1.2.3.4.

dig www.example.com

Reverse DNS Dig
Perform a reverse DNS dig lookup. This will translate an IP address into its domain name. The output will be a domain name such as www.example.com.

dig -x 1.2.3.4

Dig MX Record Lookup
An MX (Mail Exchange) record tells us that the record is a domain name that is responsible for accepting email. The below command will tell us what domain name the mail record below is pointing as well as the priority of the mail records. . The output will be the domain name the record points to such as mail.example.com, which tells us that the mx record example.com resolves to mail.example.com.

dig -t MX example.com

Show the TXT or SPF records

dig example.com txt

Directly Query A DNS Server
By default the dns query will resolve off of your own dns server (which is usually the dns server of your ISP or local server), however we can manually specify the dns server we want to query by adding @ns1.example.com to the dns dig command.


dig @ns1.example.com www.somedomain.com

Directly Query the TXT records of a  specific name server
dig @ns1.example.com www.somedomain.com txt

Show the Name servers of a domain
Here's how to query for a list of nameservers for a given domain,

dig example.com ns

dig traceroute information
If you like the traceroute command, you can do something similar with dig to follow DNS nameservers, like this, using the

dig example.com  +trace

 or to keep the output manageable:

dig example.com  +trace +short







Saturday, December 12, 2015

Block IP using blackole route

A null, also known as Blackhole, is a routing table entry that discards a matching packet without sending it further. It is mainly used for preventing/ blocking an unwanted IP to attack your system. It drops the IP using IPtables. for instance, if you want to block an IP address 192.168.0.1, you can write the command:
# ip route add blackhole 192.168.0.1/32

your system will now drop all packets coming from 192.168.0.1

Teaming 2 Network cards

If we have two network interfaces, eth0 and eth1 and we want it to be shown in a single bond as bond0, we need to first create a config file with the name bond0. The network configuration in Linux stores files in etc/sysconfig/network-scripts/ directory.
The work parameters must be set as follows:

#vi /etc/sysconfig/network-scripts/bond0

DEVICE=bond0
IPADDR=192.168.1.20
NETWORK=192.168.1.0
NETMASK=255.255.255.0
USERCTL=no
BOOTPROTO=none
ONBOOT=yes
The IP address must be replaced with your actual setup.
Open eth0 and eth1 configuration files
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
# vi /etc/sysconfig/network-scripts/ifcfg-eth1
Modify the parameters as follows:
DEVICE=eth0
USERCTL=no
ONBOOT=yes
MASTER=bond0
SLAVE=yes
BOOTPROTO=none
DEVICE=eth1
USERCTL=no
ONBOOT=yes
MASTER=bond0
SLAVE=yes
BOOTPROTO=none
Save and close the file.
 Now load the bond driver/module when the channel bonding interface is brought up. The kernel configuration files are also modified using # vi /etc/modprobe.conf
Add these lines :
alias bond0 bonding
options bond0 mode=balance-alb miimon=100
 Test configuration using these two steps:
Firstly, load the bonding module:
# modprobe bonding
Restart the network to see bond0 interface:
# service network restart

Both Nic's should now be working as one

Wednesday, September 30, 2015

Add a windows domain user to a computers local administrator group remotely




To Add a windows domain user to a computers local administrator group remotely

first download and install PsExec from

https://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

open command prompt

use psexec to connect to computer

psexec \\computer_name -u \domain\administrator -p password cmd

a command prompt of the remote computer will open on your computer

then type in the command prompt

net localgroup administrators domain\user /add

should return command completed succesfully

Done.


Monday, August 3, 2015

Ethtool Examples


Ethtool is the defacto tool for interacting with your network adapters.

ethtool ethn0   (or whichever eth device you want information on)
gives you detailed information about your network adapter.

ethtool -i eth0  Will display the driver settings, driver version,firmware version and bus details

ethtool -a eth0  will show you the auto negotiation details of your adapter.

ethtool -S eth0 will display network statistics IE how many bytes have been sent and how many bytes have been received by the adapter.

And then my favourite

ethtool -p eth0   will physically identify your network card by causing the LED's on the network card to flash on and off.  this is very useful when you have multiple network interfaces and need to know which interface is bound to which eth device on your system.

ethtool -t eth0 offline     will bring the adapter offline to perform diagnostics and testing
ethtool -t eth0 online     will perform testing whilst the adapter is online and available to your system)

provided your Nic supports this feature ethertool will perform diagnostics  on your network adapter



Thursday, July 30, 2015

Output from rpm -qa, how to extract only the name of the package?

If you need to migrate to a new computer and want to install the same packages that were on your old computer on the new one, the following pipes all the packages into a list that you can then use to install on the new computer.

rpm -qa --qf "%{NAME}\n" > filelist.txt

Then to install the packages

yum -y install  `cat filelist.txt`
or
yum -y install $(cat filelist.txt)

Wednesday, July 29, 2015

How to show only settings and not the comments in a file

In Bash , ZSH or most shells. Comments are added in files using #   or sometinmes ;

Sometimes files are full of comments and finding the settings that are set amongst all the comments can be confusing.

The following will show you the contents of a file excluding lines begining with #

substitute the #   with a ;  if your program uses ;  to comment


grep -v '^#'  filename

Sunday, February 8, 2015

nmcli basics and examples

nmcli  is the  cli for network manager which is the new way to configure network and network adaptors in Redhat/Centos 7 and Fedora 21

Here are some basic nmcli commands to get you going.

The nice thing with nmcli is that you can tab through all options ie you don't need to remember all the commands, tab completion will bring them up.

nmcli hitting tab here brings up the following options
con  -- NetworkManager connections
dev  -- devices managed by NetworkManager
nm   -- NetworkManager status

nmcli dev  hitting tab here will bring up the following options
disconnect  -- disconnect device and prevent it from automatically activating
list        -- get detailed information about devices
status      -- print status of devices
wifi        -- list available WiFi access points

similarly
nmcli con  hit tab will bring up the following
delete  -- delete a connection
down    -- deactivate a connection
list down   -- list configured connections
status  -- print status of active connections
up      -- activate a connection

nmcli nm
enable  -- get status or enable/disable networking
sleep   -- get sleep status or put to sleep/awake NetworkManager
status  -- show overall status of NetworkManager
wifi    -- inquire or set status of WiFi in NetworkManager
wwan    -- inquire or set status of WWAN in NetworkManager

To show all configured connections
nmcli con 


To connect to a wifi network

first nmcli dev wifi
to see list of available networks then

nmcli dev wifi connect networkname password inputpassword

switch wifi off
nmcli nm wifi off

Bring eth0 adaptor up

nmcli -p con up id "My wired connection" iface eth0

activates the connection with name "My wired connection" on interface eth0. The -p option makes nmcli show progress of the activation.

Get list
nmcli con

Stop interface
nmcli con down id 'Connection'

Start interface
nmcli con up id 'Connection'

Friday, February 6, 2015

Booting into single user mode and changing root password Centos / Redhat 7


since among other things this process is also now different in redhat 7 / CentOS 7
here is how you do it in redhat 7

During boot, press "e" at the grub loader.
Scroll down using the arrow keys to the line starting with "linux". It would look like this.
linux16 /vmlinuz-3.10.0......
Remove the following from that line. "rhgb" and "quiet".
Add the following to the end of the line. "init=/bin/sh".
Press ctrl+x to continue the boot process.
Once the system is booted, you will be at the root user in single user mode. But this is in a read only file system.
You need to mount the / filesystem.
mount -o remount, rw /
Test that you can write to /, following command should work without error.
touch /tmp/test
Now change your password for root
passwd
Touch the following file to make sure things are ok on SELinux, this is a fix file process.
touch /.autorelabel
Finally start the normal boot process.
exec /sbin/init

You're Done.

Saturday, January 10, 2015

Upgrade from Fedora 20 to Fedora 21

make sure your fedora 20 is up to date

yum update

reboot
then

sudo fedup --network 21 --product=nonproduct

Once Fedup completed

Reboot the system
Once the system reboots, there should be a new entry in the GRUB menu titled System Upgrade.
Select the System Upgrade option from the GRUB menu
Remark: If the System Upgrade item is not shown in the grublist at boot, it is most often caused by having a different grub, most often installed by another Linux distribution you may have in multiboot. To correct this quickly: reinstall grub:
grub2-mkconfig -o /boot/grub2/grub.cfg
grub2-install /dev/sda (replace /dev/sda by any other device you prefer to boot from)
The system should boot into the upgrade process and a plymouth boot screen should be displayed
There is a root shell on VT2 so you can tinker with the system if something goes wrong. (To disable this, boot with rd.upgrade.noshell)
Press 'esc' to see a more detailed log. If you switch back to the graphical progress indicator, it may show 0% for the remainder of the upgrade but that does not mean the upgrade has stopped.
Once the upgrade process has completed, the system will reboot and an option to boot Fedora 21 will be on the grub menu.


Cleaning Up Post Upgrade

It is worth rebuilding the RPM DB to prevent RPMDB checksum error when doing a distribution sync:

sudo rpm --rebuilddb

There are a collection of post-upgrade things to do. Some of which are fixed by doing a distro sync:

sudo yum distro-sync --setopt=deltarpm=0

This tool search for .rpmnew, .rpmsave and .rpmorig files and ask you what to do with them: Keep current version, place back old version, watch the diff or merge.

sudo yum install rpmconf

sudo rpmconf -a

If you are using google-chrome from the Google repository, you must re-install google-chrome due to a packaging bug on the Google side of things. Make sure to adjust the command to the build type you would like to install:


sudo yum remove google-chrome-\* && sudo yum install google-chrome-stable

Enjoy Fedora 21

Thursday, October 30, 2014

Configure YUM repository using DVD or CD ROM


Necessary when for example you are configuring a Redhat Server without a paid for subscription
at lease this gives you access to all the programs on the CD / DVD

Mount your CD Rom. in this example we mount cd rom to  to /mnt/cdrom

mount /dev/cdrom /mnt/cdrom

Create the new repo file called cdrom.repo under /etc/repos.d directory.

vi /etc/repos.d/cdrom.repo

Add the following details.

[cdrom]
name=CDROM Repo
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release


cntrl -  x to save


1. [cdrom]  -  Name of the Section.
2. name =  Name of the repository
3. baseurl = Location of the package
4. Enabled = Enable repository
5 gpgcheck= Enable secure installation
6. gpgkey= Location of the key

To test
Install the package using the yum command, let’s install the MySQL package using  YUM.

yum install mysql-server

Using systemctl to Manage Services

Using systemctl to Manage Services

Note: For in all examples below, the format of may also be used instead - though it is not required in recent versions of systemd, and is therefore not shown.

Several examples of widely used services are: httpd (Apache Web server), sshd (SSH server), nfs (NFS server), autofs (AutoFS service), vsftpd (the "Very Secure FTP" server), and many more...


Start a Service

# systemctl start


Stop a Service

# systemctl stop


Restart a Service

# systemctl restart


A Conditional Restart of a Service (Restarts a service only if it's already running)

# systemctl try-restart


Reload a Service

# systemctl reload


Check whether a Service is Running

# systemctl status  

( Or:  # systemctl is-active )


List all Available Services and Show their Running Status

Note: may be "piped" through grep, to find a specific service:  | grep  

# systemctl list-units --type service --all
or systemctl list-units -t service


Enable a Service (this makes it start automatically at start up)
does what chkconfig on used to do.

# systemctl enable


Disable a Service  (this stops the service from starting at startup)
does what chkconfig off used to do.

# systemctl disable


Check whether a Service is Enabled

# systemctl is-enabled  

( Also mentioned in: # systemctl status , under "Loaded:" )


List all Available Services, and check whether they are Enabled

Note: may be piped through grep, to find a specific service:  | grep

# systemctl list-unit-files --type service 


Kill all Running Processes Related to a Service

# systemctl kill


Available unit types.
Service unit          .service         A system service.
Target unit          .target         A group of systemd units.
Automount unit .automount A file system automount point.
Device unit        .device A device file recognized by the kernel.
Mount unit        .mount A file system mount point.
Path unit                .path         A file or directory in a file system.
Scope unit        .scope An externally created process.
Slice unit                .slice         A group of organized units that manage system processes.
Snapshot unit        .snapshot         A saved state of the systemd manager.
Socket unit        .socket An inter-process communication socket.
Swap unit        .swap A swap device or a swap file.
Timer unit        .timer A systemd timer.

Boot process

Systemd primary task is to manage the boot process and provides information about it.
To get the boot process duration, type:

# systemd-analyze

To get the time spent by each task during the boot process, type:

# systemd-analyze blame

To get the list of the dependencies, type:

# systemctl list-dependencies

More examples

To move to single user mode, type:

# systemctl rescue
To move to the level 3 (equivalent to the previous level 3), type:

# systemctl isolate runlevel3.target
Or:

# systemctl isolate multi-user.target
To move to the graphical level (equivalent to the previous level 5), type:

# systemctl isolate graphical.target
To set the default run level to non-graphical mode, type:

# systemctl set-default multi-user.target
To set the default run level to graphical mode, type:

# systemctl set-default graphical.target
To get the current default run level, type:

# systemctl get-default



To stop a server, type:

# systemctl poweroff
Note: You can still use the poweroff command, a link to the systemctl command has been created (the same thing is true for the halt and reboot commands).

To reboot , suspend it or put your machine into hibernation, type:

# systemctl reboot
# systemctl suspend
# systemctl hibernate


Journal analysis

In addition, Systemd handles the system event log, a syslog daemon is not mandatory any more.
To get the content of the Systemd journal, type:

# journalctl
To get all the events related to the crond process in the journal, type:

# journalctl /sbin/crond
Note: You can replace /sbin/crond by `which crond`.

To get all the events since the last boot, type:

# journalctl -b
To get all the events that appeared today in the journal, type:

# journalctl --since=today
To get all the events with a syslog priority of err, type:

# journalctl -p err
To get the 10 last events and wait for any new one (like “tail -f /var/log/messages“), type:

# journalctl -f




Tuesday, June 24, 2014

How to Convert GPT partition to MBR (without loosing Data) Windows 7



I know this is Windows related,  but the solution involves using gdisk which is a Linux tool.

For this you will need a Fedora Live CD and a Windows 7 system repair disk

boot with Fedora live CD

open Terminal

yum -y install gdisk

gdisk /dev/sda

will find the GPT partition table


b     (this will back it up)  (OPTIONAL)

give backup a name (optional)
sda-preconvert.gpt

type the following
r     (starts transformation / recovery)

g      (to convert GPT to MBR

p  (to preview the MBR converted partition table)

w  ( to save your changes)


Now boot with Windows 7 System repair Disk


choose option to go into command prompt

type
DISKPART and press Enter.
LIST DISK and press Enter.
SELECT DISK N and press Enter (N represents the disk you want).
LIST PARTITION and press Enter.
SELECT PARTITION N and press Enter (N represents the partition you want).
ACTIVE and press Enter.
EXIT and press Enter.

type the following

BOOTREC /SCANOS and press Enter.
BOOTREC /REBUILDBCD and press Enter.
BOOTREC /FIXMBR and press Enter
BOOTREC /FIXBOOT and press Enter.

reboot

if you recieve following error
File: \boot\bcd
Status: 0xc000000f
Info: An error occurred while attempting to read the boot configuration data.

don't sweat, boot back up using system repair disk and let the automatic system repair run
this time it will take longer and after a final reboot, you should have a working system. with a MBR partition and all your files and config exactly as they were.

if you continue to receive a Error 0xc0000225 on windows boot
boot with Gparted and remove all EFI  partitions.

reboot normally and you should be in good shape.

Sunday, April 13, 2014

Sunday, March 30, 2014

Command line shortcuts


Clear screen
cntrl l 

ssh connection to unreachable host through a reachable host
ssh -t reachable_host ssh unreachable_host 

set an audible alarm when machine comes online
ping -i 60 -a IP_address

Display top 10 running processes sorted by memory usage
ps aux | sort -nk +4 | tail

save your previous command as a script
echo "!!" > foo.sh

what is my my public ipadress ?
curl ifconfig.me


Sunday, March 16, 2014

Enable Logging for SFTP sessions




To enable logging of your sftp sessions

Replace the susbsystem line in your /etc/ssh/sshd_config with
Subsystem    sftp    /usr/libexec/openssh/sftp-server -f LOCAL5 -l INFO
Add the following to /etc/syslog.conf or  /etc/rsyslog.conf
#sftp logging
local5.*                        /var/log/sftpd.log
Restart the sshd and syslog/rsylog services,
sftp sessions should now be logged to /var/log/sftpd.log

Wednesday, March 12, 2014

setting persistant system wide environment variables


The folder /etc/profile.d/ is the recommended place to add customizations to the system profile.
do not edit /etc/profile  rather add files in the /etc/profile.d folder

For example, when installing the oracle JDK, you might need to set the JAVA_HOME and JRE_HOME environment variables.

Create a new file called java.sh
vim /etc/profile.d/java.sh

Within this file, initialize the necessary environment variables
export JRE_HOME=/usr/java/jdk1.7/jre
export PATH=$PATH:$JRE_HOME/bin

export JAVA_HOME=/usr/java/jdk1.7
export JAVA_PATH=$JAVA_HOME

export PATH=$PATH:$JAVA_HOME/bin

save the file.

every time you reboot the environment variable will be loaded system wide..

Sunday, March 2, 2014

Undeleting Files that were accidentallly deleted


How to undelete files from ext3/ext4 partition
When you accidentally delete a file or files or an entire directory extundelete can recover them for you.

yum install extundelete

The first step should be to  unmount the partition that your lost files are on, as soon as possible.
If you know the path and the name of the file or directory  (let's assume it's /home/cgerada/Music/ and you accidentally deleted all your music files .
sudo to root
sudo -i or su - root and go to a partition with enough free space to store the deleted files. Then:
type

extundelete --restore-files /home/cgerada/Music/ /dev/sda3

you should get the following
NOTICE: Extended attributes are not restored.
WARNING: EXT3_FEATURE_INCOMPAT_RECOVER is set.
The partition should be unmounted to undelete any files without further data loss.
If the partition is not currently mounted, this message indicates 
it was improperly unmounted, and you should run fsck before continuing.
If you decide to continue, extundelete may overwrite some of the deleted
files and make recovering those files impossible.  You should unmount the
file system and check it with fsck before using extundelete.
Would you like to continue? (y/n) 
type y
y
Loading filesystem metadata ... 3679 groups loaded.
Loading journal descriptors ... 31276 descriptors loaded.


As soon as extundelete is  finished, you will  find the recovered files  in the folder you were in when you ran the command  /RECOVERED_FILES/

If you deleted  a  directory itself, you can use --restore-directory


There are some other  useful options such as  --restore-all , --restore-file, --after 'dtime' or --before 'dtime'

type extundelete --help  to see exactly what the other options do.


Thursday, February 6, 2014

Installing with yum from a text file.




To install a list of specific  packages that are installed on one server to another server .

rpm -qa > installed.txt

will create a text file with a list of all installed packages

copy installed.txt from server1 using rsync to server2, like this:

rsync installed.txt  server2:

then on server2  type

 yum -y install $(cat installed.txt)

This will now install all the packages listed in installed.txt on server2.

Tuesday, February 4, 2014

Disallowing programs through Sudo

You want to grant user cgerada root privelages to all programs except one, lets use tcpdump in this example.
ie we want to prevent cgerada from running  tcpdump,  but he must still be able to run all other commands as root using sudo. Further ,you do not want cgerada to have the ability to sudo -i
which effectively changes cgerada to the root user.

Normally with sudo you list the programs that the user is allowed to run with root privelages.
in this example you want to list and implement a program that is not allowed..

To edit the sudoers config file
type visudo  [enter]
which will bring up the sudoers file in vi ready to edit..

add the following line

under the section were the Cmnd_Alias is commented out
add the following alias

Cmnd_Alias DISALLOWED = /user/tcpdump, /bin/bash

You can separate with commas all the commands that you want to disallow.

Im also disallowing /bin/bash simply because when a user types sudo -i a new bash session is started as root. by disallowing this my user will not be able to sudo -i.

Further down in the sudoers file were you see
root   ALL=(ALL)    ALL

add the following underneath

cgerada ALL=ALL, !DISALLOWED

The !(Bang) means the opposite  ie without the !(bang) the user will have access to those programs.
by putting in a !(bang) in front the opposite is true.

save the file and exit by typing :x [enter]

Now look what happens if the user cgerada tries to run tcpdump


sudo tcpdump -n port 25
[sudo] password for cgerada: 
Sorry, user cgerada is not allowed to execute '/usr/sbin/tcpdump -n port 25' as root on server.



now look what happens if the user tries to sudo -i

sudo -i
[sudo] password for cgerada: 
Sorry, user cgerada is not allowed to execute '/bin/bash' as root on server