Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Saturday, July 3, 2021

Howto check Centos 7 and 8 common services

In Centos 7 and earlier, SysV manage Linux services. To list such services, 

config --list


Centos 7 and 8 uses Systemd to manage Linux services. The syntax for this command is

systemctl [OPTIONS...] COMMAND [UNIT...]

Centos 7 was a transition phase towards the newer Systemd, and it maintained a mix of service types.


Here are a list of common command to check Linux services.

List active systemd services

systemctl 



List all systemd services

systemctl -a

List only SysV services (centos 7 and older)

systemctl list-unit-files


List by service types

systemctl list-units --type service

systemctl list-units --type mount

Where

LOAD   = Reflects whether the unit definition was properly loaded.

ACTIVE = The high-level unit activation state, i.e. generalization of SUB.

SUB    = The low-level unit activation state, values depend on unit type.


Check status of a service

systemctl status sshd.service

sudo systemctl status nfs-server.service


To stop or start or restart a service

sudo systemctl stop nfs-server.service

sudo systemctl start nfs-server.service

sudo systemctl restart nfs-server.service


To determine service is active or enabled the following will return 0 (active) and non zero (disabled)

systemctl is-active sshd

systemctl is-enabled sshd

systemctl is-active nfs-server

systemctl is-enabled nfs-server


To list a service and its dependencies;

 systemctl list-dependencies nfs-server


List services and their number of running task, CPU percentage, memory usage and I/O continuously

systemd-cgtop


List services as above once

systemd-cgtop -n 1


To sort, use the following based on the column titles

systemd-cgtop -t 

systemd-cgtop -c

systemd-cgtop -m

systemd-cgtop -i


List services and their open ports

netstat -a

List services that use TCP and UDP and their open ports

netstat -tulpn


Display which user is used for a filename

fuser . -v


Display running service

top


Display running services in pretty formatting and enable F1-F10 commands.

htop






Wednesday, July 6, 2016

NMap, SSH and Matrix the Movie

Matrix the movie is one of the coolest movie with concepts that provoke thoughts of technology and politics.

Ever thought that open source was involved? Surprise, surprise, trinity was referring to the use of Nmap and SSH exploits.

See http://www.theregister.co.uk/2003/05/16/matrix_sequel_has_hacker_cred


Wednesday, September 9, 2015

Tutorial on Linux Shell Fundamentals

Most Linux admin and developers on Linux will benefit from knowing some shell scripting skills.

In most Linux, the shell is run on BASH. There are a few other shells out there but the default is BASH which I will introduce the basics here.

Gain access to a Linux terminal or also known as the command line terminal (CLI) and follow along these topics.

Fundamentals 1 (START)
  1. Getting started
  2. Creating a script
  3. Comments, printout and local strings
  4. Builtin variables
  5. Backup a folder
  6. Separate variable names and back slash
  7. Linux command in scripts
  8. Function basics
  9. Script arguments
  10. Accepting user input
  11. Simple if/else decision
Fundamentals 2 (START)
  1. The while loop
  2. Array variables
  3. Read from a file
  4. The for loop
  5. The until loop
  6. Find plain text files
  7. Quick menu input with select
  8. The case to create a menu
  9. Formatted printing with printf
  10. Print a line full length of screen


--

Tuesday, August 25, 2015

Access VirtualBox VM via SSH

A virtual machine provides lots of space for development, troubleshooting and learning. Sometimes its required to remotely access the virtual machine guest using SSH from the host. Virtualbox allow users to setup so many different type of virtual machines known as the guest.

The machine installed with VirtualBox is known as the host. In this example, the host is a MS Windows 7 computer with virtual machine guest OS being Centos Linux 6. SSH client such as Putty is run on the host computer and will attempt to connect to the virtual machine. This will also allow multiple guest OS to SSH between one another.

Step 1: Configure VirtualBox networking

Shutdown all guest virtual machines. Open VirtualBox Manager, click on the virtual machine to be configured and choose Settings ->Network. There should already be an existing network for access to the Internet. Nothing needs to be changed if you can already access the Internet from the guest OS.

Guest virtual machine default networking with access to Internet

Choose one of the unused Adapter (example Adapter 2) and configure the settings:
Attached to: Host-only Adapter
Name: VirtualBox Host-Only Ethernet Adapter



On the MS Windows, VirtualBox has already setup its own network 192.168.56.1 as can be seen below. The other IP is the dynamic IP used by host computer to access the Internet.



Step 2: Configure a static IP on guest OS.

Start the guest OS and login. Open a terminal as root and the new network device has been identified but we need to configure the IP to match that of the VirtualBox network 192.168.56.x.


In the case above, VirtualBox has already configured network device eth0 to access Internet. Check eth1 with

# ethtool eth1

The default eth1 if we started that network, it would use DHCP but we want a static IP so that we know the IP for SSH. We now need to create/edit eth1

vi /etc/sysconfig/network-scripts/ifcfg-eth1

Add/modify following lines;

DEVICE="eth1"
BOOTPROTO=static
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
IPADDR=192.168.56.10
NETMASK=255.255.255.0
IPV6INIT=no
NAME="System eth1"

Start the networking for eth1 and check that eth1 is using the new IP. 

Step 3: Restart the guest virtual machine.

Reboot the guest OS and test access to Internet and ability to SSH into the guest OSS from the host computer as can be seen below.

Guest VM network for Internet and SSH

The network routing is shown below.
Guest virtual machine network routing




Tuesday, December 16, 2014

Howto Linux Pretty Prompts

The linux terminal provides many useful functions to improve productivity and looks. Just imagine always having to access many remote Linux servers or creating sub shells and then forgetting which server or shell are you keying in the commands.

In the Bash shell, there as a programmable prompt (so to speak). It uses the variable named PS1 and PS2. For a beginner, its good to know how to modify the prompt displayed, change its colours and applying it to a single user or as default for all. Bash 4.2 added support for unicode which means way more fonts are available to make pretty prompts.


Basics

A simple assignment of the prompt is as follows;

export PS1='Demo$ '

The space after the '$' is commonly used to make typing more readable.

The prompt can be assigned with the Bash shell special characters. Some are visible and other not. Following list some of these characters;
\d     the date  in  "Weekday  Month  Date"  format (e.g., "Tue May 26")
\e     an ASCII escape character (033)
\h     the hostname up to the first '.'
\H    the hostname
\t      the current time in 24-hour HH:MM:SS format
\T     the current time in 12-hour HH:MM:SS format
\@     the current time in 12-hour am/pm format
\w     the current working directory
\W     the basename of current working directory


Example 1: Common prompt lets user know their login user name, server host name and working directory.
export PS1='[\u@\h \W]\$'

Example 2: Display time the prompt was executed.
export PS1='\t:\w$ '

Example 3: A two line prompt which documents the last history number.
export PS1='=\d \u@\h \!=\n\t $ '


Colours

The prompt supports colours as defined by ANSI (see linuxhowtos.org). This is in the format to display colours;

 \[\ecolourcode\]. 

Example of ANSI colours
Black      0;30m       Dark Gray    1;30m
Red        0;31m       Bold Red     1;31m
Green      0;32m       Bold Green   1;32m
Yellow     0;33m       Bold Yellow  1;33m
Blue       0;34m       Bold Blue    1;34m
Purple     0;35m       Bold Purple  1;35m
Cyan       0;36m       Bold Cyan    1;36m
Light Gray 0;37m       White        1;37m


The first digit 0=normal, 1=bold, 4=underline and each code ends with the letter 'm'. At end of prompt, return to the default colour, with \[\e[m\]


Colour 40m onwards can be used to highlight the background.

Example 1: Prompt is in blue and user types in blue.
export PS1='[\[\e[0;34m\]\u@\h \W]\$ '

Example 2:Prompt is in blue and user types using default font colours.
export PS1='[\[\e[0;34m\]\u@\h \W]\$\[\e[m\] '

Example 3: Highlight text in prompt
export PS1='\[\e[1;34m\]\u\[\e[1;33m\]@\[\e[1;32m\]\h\[\e[1;37m\]:\[\e[1;31m\]\w \[\e[1;36m\]\$ \[\e[m\]'





Example 4: Blue background with red text.
export PS1='\[\e[44m\]\[\e[1;31m\][\u@\h \W]\$\[\e[m\] '

Saving changes

This PS1 value can be set in /etc/bashrc to be used by all users. Each user can change their prompt by editing the file ~/.bashrc with this PS1 or PS2 assignment.

Example: ~/.bashrc file
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# User specific aliases and functions

export PS1='[\[\e[0;34m\]\u@\h \W]\$\[\e[m\] '


Advanced prompt


To run a command before the bash prompt is displayed, use PROMPT_COMMAND. This supports many more flexible formatting of values for advance Linux administrators.

Example:
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" ' \
export PS1='[\[\e[0;34m\]\u@\h \W]\$\[\e[m\] '

Example 2: Charles Torvalds Basic Power Prompt
PROMPT_COMMAND='history -a;echo -en "\033[m\033[38;5;2m"$(( `sed -n "s/MemFree:[\t ]\+\([0-9]\+\) kB/\1/p" /proc/meminfo`/1024))"\033[38;5;22m/"$((`sed -n "s/MemTotal:[\t ]\+\([0-9]\+\) kB/\1/Ip" /proc/meminfo`/1024 ))MB"\t\033[m\033[38;5;55m$(< /proc/loadavg)\033[m"' \
PS1='\[\e[m\n\e[1;30m\][$$:$PPID \j:\!\[\e[1;30m\]]\[\e[0;36m\] \T \d \[\e[1;30m\][\[\e[1;34m\]\u@\H\[\e[1;30m\]:\[\e[0;37m\]${SSH_TTY} \[\e[0;32m\]+${SHLVL}\[\e[1;30m\]] \[\e[1;37m\]\w\[\e[0;37m\] \n($SHLVL:\!)\$ '


Unicode

If you have Bash 4.2 onwards, then unicode characters can be added. First find a UTF 8 bit unicode you like from UTF8 or unicodelookup. Bash provide good number of symbols but does not map to 100% of the unicodes.

Ramblings...In UTF-8 unicode for U+2620, Hex is 2620 in Octal is 023040, in Decimal is 9760 and HTML is &#9760.

Example 1: Display skull and bones U+2620 or UTF-8 for URI encoding is E2 98 A0. The Octal equivalent is 342 230 240.
export PS1='[\u@\h \W]\342\230\240 '

Example 2: Display helm symbol U+2388 (See unicode) which is UTF-8 for URI encoding e2 8e 88
export PS1='[\u@\h \W]\342\216\210 '



Example 3: Multi line prompt
export PS1='\342\214\210\d \t \W\342\214\211\n\342\216\210 '

Done.

Wednesday, December 3, 2014

List files as a tree in Linux

Usually we have the directory listing as follows;

ls -l



Can we print the linux directory in a tree format? Yes we have several options, of which two is shown here.

Option 1

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'

Option 2

Install the tree package (Steve Baker, Francesc Rocher) then execute it.

tree 
This command can specify depth of folders.
tree -L 2


Monday, September 29, 2014

Serious exploits in August and Sept 2014

These recent months have shown how the open source software model could handle (or is still handling) bugs that could be turned into an exploit of Linux servers. These are;

Shellshock (Sept 2014) - remotely take over a server.
BBC http://www.bbc.com/news/technology-29361794

Heartbleed (April 2014) - OpenSSL data could be intercepted.
BBC http://www.bbc.com/news/technology-28867113

The sheer number of Linux servers affected means that it is a serious threat and is wide spread. In Heartbleed, its patched but Shellshock is yet to have a patch to fully resolve the bug.

Interesting technical discussion on Shellshock is found at stackexchange.
https://unix.stackexchange.com/questions/157329/what-does-env-x-command-bash-do-and-why-is-it-insecure

How do you know if your shell is vulnerable? Hackernews recommends to run the following command in all the shell being used;

env X="() { :;} ; echo shellshock" /bin/sh -c "echo completed"
env X="() { :;} ; echo shellshock" `which bash` -c "echo completed"

If you see the text output "shellshock", please find a patch.

Chris, a contributor at Buzzfeed News, provided a good material on how 2 persons maintained the OpenSSL package, Steve Marquess and Stephen Henson. The commercial entity for this is known as OpenSSL Software Foundation.

Now doesn't it make you wonder who is responsible of bash shell and is it the same package for every Linux distro?


Tuesday, June 11, 2013

Install Yum repo for Centos

Adding additional source of packages (software) for Centos will greatly reduce the need to recompile OSS packages. Epel provides open source software that is free for use. This includes clamav, atop, cacti,

Here are step to add a common repository.

Centos 5

Step 1: install and enable epel repo
rpm -Uvh http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm


Centos 6

Step 1: install and enable epel repo
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-5.noarch.rpm

Step 2: Enable priorities
yum install yum-priorities

Edit /etc/yum.repos.d/epel.repo
add under [epel]
priority=10


Saturday, July 21, 2012

Installing Centos 6 on HP Probook 4420s

Finally found time to install Centos 6.2 on the HP Laptop.

Laptop specs:
RAM 4Gb
CPU: i3 ...

Installed (default)
Linux kernel 2.6.32-220.el6.x86_64

Gnome 2.28.2
Mozilla Firefox 10.0.5

Add RPMFORGE software repository (libary of software)
(download the rpm)
rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
rpm -ivh ~/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
yum update
(Total 371 files, 385MB downloaded)

(reboot)

Post-Installation
Login as root at the terminal and first few software to be installed (type this out):

yum groupinstall "Desktop" "Desktop Platform" "X Window System" "Fonts"
yum install madwifi
(reboot)
yum install flash-plugin
yum install vlc
yum install libreoffice
(total 25 package, 103Mb download)
yum install libreoffice-ogltrans


Notes: wifi can only be configured easily when linux is in graphical screen/desktop.

Prepare to build from source

yum install cabextract rpm-build



Other observations
  1. The touchpad (black square at center of laptop that helps to move the mouse) is having a soft LEFT and RIGHT button (not working correctly)
    • To drag windows, press bottom half of the Touchpad until a click is felt. Then drag without leave finger from the Touchpad.
    • To RIGHT click in window, you need to press bottom half then do a second click without lifting the first finger.
    • Install from (HP) or better still to use a mouse pointer.
  2. Music
    • Default "Totem Music Player" doesn't support MP3. Need to install gstreamer-plugins-ugly
  3. Webcam
    • Runinng "Cheese" I can adjust to higher resolutions but its frames per seconds is very little. This causes the video to seem very poor quality. 
  4. Option to edit the menu bar (add new items manually) is missing. Installed alacarte package, logout then back in to enable editing of menu items.
  5. If boot was to only command line (a black screen), switch to graphical. Type init 5.

Tuesday, June 3, 2008

What AWK can do for Linux.

In Linux, there are many tools to process text and other forms of data. One tool that comes to mind is the AWK. The AWK scripting processes a file line by line and can produce a summary of its finding as header or footer.

TLDP provides a guide on AWK at http://tldp.org/LDP/abs/html/awk.html

EXAMPLE 1
Here is a demonstration of the AWK to extract the directories mounted.
STEP 1. Create an AWK script file called "myscript1.awk". This should contain two lines.

BEGIN{ printf("Display the third field\n"); }
{ print $3 }

STEP 2. Run the following command to use the AWK script.

mount -t ext2,ext3 | awk -f myscript1.awk

EXAMPLE 2
Without using an AWK script file, extract data from the file /etc/passwd and display the results to screen.

STEP 1. Run the following command

awk -F: '{ print $1 ":" $3 ":" $7 }' /etc/passwd

AWK have been used for more complex reporting but the above should give an idea of how this is done.

Blog Archive