Using Python Virtual Environment

Advantages for Virtual Environment:

  • separates python used for development from system python and other projects
  • avoids issues with versions between projects
  • allows ability to populate package list for projects for portablility between systems

What is Virtual Environment

Python virutal environment is essentially changing the path of your current terminal session. By appending to the front of the path environment variable it will then use the virutal environment that is setup with its own bin and lib files for python verses the system’s python bin and lib. This allows us to separate the system python packages away from project packages. So the project must be ran while virtual environment is active. You should see (venv) at the start of the command line prompter when the activate is sourced.

Path before activate:

Post activate:


Have virtualenv installed
pip install virtualenv

Setup Guide:

This is the basic workflow of starting a new project using virtual environment.

# Creates and goes to project directory
mkdir proj && cd proj  

# Makes virtual environment folder name venv  
python -m venv venv 

# To activate virtual environment  - sourcing the activate file 
. venv/bin/activate

At this time install any package dependencies for your python project with pip.

#Create list of python packages for project. 
pip freeze > requirements.txt

# To exit/deactivate vitual environment

Setting up PIA VPN – CentOS7


yum install -y epel-release openvpn curl unzip wget

Verify starting public IP:

wget -qO -

Configure OpenVPN

cd /etc/openvpn
# download the PIA .ovpn files
# unzip file
# Create cred file by inserting username on line one and password on line two:  
vi /etc/openvpn/cred.conf

# Change permissions on credential file
chmod 400 /etc/openvpn/cred.conf
# sym link desire locationConfig.ovpn to server.conf
ln -s /etc/openvpn/[locationConfig].ovpn /etc/openvpn/server.conf
# Modify server.conf file
vi server.conf
# modify the config to reflect the following
     auth-user-pass cred.conf
# add PIA DNS servers
nameserver >> /etc/resolv.conf
nameserver >> /etc/resolv.conf
# Restore selinux context 
restorecon -Rv /etc/openvpn/
# start and enable at start VPN service 
# if you wish to stop VPN service use - systemctl stop openvpn@server.service
systemctl enable openvpn@server.service
systemctl start openvpn@server.service

verify that public IP has changed

wget -qO -

reboot to make sure changes are persistent and VPN is active.

KVM and Vagrant Setup on RHEL/CentOS 7

Step 1 – KVM and Vagrant Installation

Installs needed packages for KVM and Vagrant
yum -y install virt-manager qemu qemu-kvm qemu-img libvirt libvirt-python libvirt-devel python-virtinst libvirt-client ruby-devel gcc

Auto-starts libvirt on system boot
systemctl enable libvirtd

Starts the libvirt service immediately
systemctl start libvirtd

Obtaining Vagrant RPM

Go to to ensure it is the most current version and modify wget command, if needed.

Install the rpm with yum yum -y localinstall vagrant_2.1.2_x86_64.rpm

Step 2 – Vagrant Plugins Installation

Installs vagrant plugin for kvm. Make sure to install by the desired user of Vagrant.
vagrant plugin install vagrant-libvirt
vagrant plugin install vagrant-mutate

Step 3 – Adding your User to the Group

usermod -aG libvirt <insertUserNameHere>

Enabling Execution from Remote Shell

Optional – To enable execution from remote shell

echo 'polkit.addRule(function(action, subject) {
if ( == "org.libvirt.unix.manage"
&& subject.local
&& subject.isInGroup("libvirt")) {
return polkit.Result.YES;
});' > /etc/polkit-1/rules.d/10.virt.rules

systemctl restart polkit

Vagrant Quick Reference

I will be using centos box for this guide but you can download your desired distro here.

download vagrant template (box)
vagrant box add centos/7 --provider=libvirt

Setting Up VM

create project directory
mkdir ~/VM && cd ~/VM

initialize – This creates Vagrantfile
vagrant init centos/7

build / starts VM
vagrant up --provider=libvirt

to ssh – if you do not have a name set in Vagrantfile it will be default vagrant ssh default

Further VM Controls

current status
vagrant status

saves content in RAM
vagrant suspend

clean shutdown
vagrant halt

unclean shutdown
vagrant halt --force

destroys the vms
vagrant destroy

Plex media server setup on CentOS7 via repo

This is the bash script I user to install plex on Centos 7 System. With this system you can update plex with yum update vs having to manually download the rpm.

# setups plex on CentOS7 
# run the commands as root or make script to run with sudo

# Adds repo to yum.repos.d
echo "Creating repo file for plex"
echo """[PlexRepo]
gpgcheck=1""" > /etc/yum.repos.d/plex.repo

# installs and enbles plex media server
echo "Installing plex and enabling it at startup"
yum -y install plexmediaserver

# Setting up firewall rules
echo "Adding firewall rules to firewalld"
echo """<?xml version="1.0" encoding="utf-8"?>
  <description>Ports required by plexmediaserver.</description>
  <port protocol="tcp" port="32400"></port>
  <port protocol="udp" port="1900"></port>
  <port protocol="tcp" port="3005"></port>
  <port protocol="udp" port="5353"></port>
  <port protocol="tcp" port="8324"></port>
  <port protocol="udp" port="32410"></port>
  <port protocol="udp" port="32412"></port>
  <port protocol="udp" port="32413"></port>
  <port protocol="udp" port="32414"></port>
  <port protocol="tcp" port="32469"></port>
</service>""" > /etc/firewalld/services/plexmediaserver.xml

firewall-cmd --add-service=plexmediaserver --permanent
firewall-cmd --reload

echo "Complete the setup in your browser using -- localhost:32400 or plexServerIp:32400"

Substituting text with sed – Quick Reference

sed the stream editor

  • good for scripting configuration
  • uses regular expressions

Common regular expressions:

^ — line start with — ^start .*
[^] — line does not start With — [^]start .*
$ — End of line or string — *? end$
\b — Word Boundary – start or end of word — ing\b

Word Substituting

Substituting Word – first instance only
sed s/oldWord/newWord/ /file/path

Substituting Word – all instances
sed s/oldWord/newWord/g /file/path

Substituting Word for nth instances
sed s/oldWord/newWord/2 /file/path

  • in this case the second instance
  • use p after 2 to print the line with the changes – helpful when scripting to ensure you are targeting the correct instance.

sed s/oldWord/newWord/ /file/path

remove comments/ white space from config files
sed -e '/^#/ d' -e '/^$/ d' < FileToRemoveCommits > OutputFile

to auto backup the file before editing
sed -i.orig -e 's/danger/safety/g' testfile.txt

additional tip
another useful feature when using sed in scripts is testing by not redirecting the output to a file.
sed s/oldWord/newWorld/ filename – to test output. the original file stays the same.

Change UUID of disk – RHEL/CentOS

Use the following to check for Duplicate UUID (This can be caused if you dd a drive to another drive). This command list UUID / dev names of all the systems disk.


To generate a new UUID


To change the UUID

tune2fs /dev/<insertDevNameHere> -U <insertGeneratedUUID>

You can verify that the changes with blkid.

VIM Quick Reference

To Enter vim Editor

note: if you get an error try vi filename

vim filename or vim /path/to/filename

To Enter Input Mode

i – to insert at current cursor
A – to append to end of line

To Exit vi

:q! – exit without saving
:wq – exit with saving
:x – exit with saving

Command Mode

Movement Within File

# moves cursor
             ^                    The k key moves up 
             k                    The h key moves left
       <  h     l  >              The l key moves right
             j                    The j key moves down

G – to move to the bottom of the file
gg – to move to the start of the file
0 – zero – to move to beginning of line
2w – to move the cursor two words forward
3e – to move the cursor to the end of the third word forward

To Delete

d2w – deletes the word at current cursor and the next
dd – deletes the whole line

To Undo

u – to undo the last change
U – to undo all changes on a whole line
:e! – to undo all changes since last write

To Replace

place cursor on letter to replace
press r then the letter you wish to be there, for example:
re – will replace the selected character with e
ce – will remove all the letters of the word and then enter insert mode

To Search

In command mode type:
Use n to go forward one entry. Use N to go backward one entry.

To Substitute Old with New, Use the Following


Text Editing

x – remove the character under the cursor
dw – until the start of the next word, EXCLUDING its first character.
de – to the end of the current word, INCLUDING the last character.
d$ – to the end of the line, INCLUDING the last character.

Cut / Copy and Pasting

dd – Cut a line
yy – Copy a line
yw – Copy a word
p – to paste

To Issue Commands Inside Editor Using :!

:! ls -al

Commenting Out Multiple Lines

To Show Line Numbers
:set number

To Comment Out Lines 1-3

To Uncomment Lines 1-3

To Hide Line Numbers
:set nonumber

Tutorial for vim


Recover Root Password RHEL/CentOS 7

  1. Boot up server
  2. At boot menu, press any key to stop auto selection at grub menu
  3. Highlight the kernel you wish to boot to and press e
  4. Move cursor to linux16 line and hit end key on keyboard to go to the end of the line
  5. Append rd.break to the end of the line
  6. Ctrl + x – to boot with your modifications

The system will enter emergency mode with /sysroot directory mounted as read only. The sysroot directory is the root of your system drive.

  1. mount -o remount,rw /sysroot – to remount the root as rw
  2. chroot /sysroot – to chroot jail sysroot as the root file system
  3. passwd root – to reset root password
  4. touch /.autorelabel – this ensures that SELinux will add a label to any unlabeled files – This allows the password to be updated
  5. exit – exit chroot jail
  6. exit – exit debug shell

Yum Basics – CentOS/RHEL7

Basic yum commands

Install a package – add a ‘-y’ behind yum to install with yes provided to any prompt

yum install packageName

Update a package

yum update <packageName>

Update all packages from enabled repos that are installed on the system ‘-y’ can be used on this as well

yum update

Remove a package

yum remove packageName

Enabling and Disabling yum repos

Shows a list of enabled repos

yum repolist
# or 
yum repolist enabled

Shows a list of disabled repos

yum repolist disabled 

shows a list of all repos (enabled/disabled)

yum repolist all

The repo name can be found in /etc/yum.repos.d/.repo

It is common that multi repos will be in one file

To disable repo for one running instance

yum -y --disablerepo="epel" update

To disabled repo – perminate

yum-congfig-manager --disable "epel"

To enabled repo for one running instance

yum -y --enablerepo="epel" update

To enabled repo – perminate

yum-congfig-manager --enable "epel"

misc features

The following will provide the pacakge that made the specified conf file

# If you have apache installed you can see if you use /etc/httpd to test
yum provides /Path/to/config/file

paths to know:

/etc/pki/rpm-gpg/  # location of gpg keys of repos
/etc/yum.repos.d/  # location of yum repos files 
/var/log/yum.log   # location of yum logs