Wednesday, 28 November 2012

Understanding basic linux system - Part 1

This post is about some of the basic unix/linux commands, there is also a sequel to it.

chapter 1 => unix runlevel [0 to 6]
A run level is a state of init and the whole system that defines what system services are operating.
prayag@prayag_f1:~$ who -r
         run-level 2  2012-12-09 17:04


run-level 2 = Local Multiuser with Networking but without network service (like NFS)

similarly my amazon elastic cloud VM was on runlevel 3, which means Multi-user mode with networking.

So, once the kernel is loaded, it finds init in sbin and executes it.
ll /sbin/init
lrwxrwxrwx 1 root root 20 Sep 28 18:40 /sbin/init -> /lib/systemd/systemd*
Then init continues to read the /etc/inittab file (/etc/init.d in ubuntu), which describes how the system should be set up in each run-level and sets the default run-level

The /etc/inittab will use the file /etc/rc<RUN_LEVEL>.d/

ll /etc/rc2.d/
total 20
drwxr-xr-x   2 root root  4096 Nov 12 12:58 ./
drwxr-xr-x 132 root root 12288 Nov 12 12:58 ../
-rw-r--r--   1 root root   677 Feb  5  2016 README
lrwxrwxrwx   1 root root    16 Nov 11 14:12 S01apport -> ../init.d/apport*
lrwxrwxrwx   1 root root    17 Nov 11 14:12 S01rsyslog -> ../init.d/rsyslog*
lrwxrwxrwx   1 root root    15 Nov 11 14:12 S01uuidd -> ../init.d/uuidd*
lrwxrwxrwx   1 root root    15 Nov 11 14:12 S02acpid -> ../init.d/acpid*
lrwxrwxrwx   1 root root    17 Nov 11 14:12 S02anacron -> ../init.d/anacron*
lrwxrwxrwx   1 root root    24 Nov 12 12:58 S02cgroupfs-mount -> ../init.d/cgroupfs-mount*
lrwxrwxrwx   1 root root    14 Nov 11 14:12 S02cron -> ../init.d/cron*
lrwxrwxrwx   1 root root    14 Nov 11 14:12 S02dbus -> ../init.d/dbus*
lrwxrwxrwx   1 root root    20 Nov 11 14:12 S02irqbalance -> ../init.d/irqbalance*
lrwxrwxrwx   1 root root    20 Nov 11 14:12 S02kerneloops -> ../init.d/kerneloops*
lrwxrwxrwx   1 root root    15 Nov 11 14:12 S02rsync -> ../init.d/rsync*
lrwxrwxrwx   1 root root    27 Nov 11 14:12 S02speech-dispatcher -> ../init.d/speech-dispatcher*
lrwxrwxrwx   1 root root    18 Nov 11 14:12 S02thermald -> ../init.d/thermald*
lrwxrwxrwx   1 root root    18 Nov 11 14:12 S02whoopsie -> ../init.d/whoopsie*
lrwxrwxrwx   1 root root    22 Nov 11 14:12 S03avahi-daemon -> ../init.d/avahi-daemon*
lrwxrwxrwx   1 root root    19 Nov 11 14:12 S03bluetooth -> ../init.d/bluetooth*
lrwxrwxrwx   1 root root    16 Nov 12 12:58 S03docker -> ../init.d/docker*
lrwxrwxrwx   1 root root    17 Nov 11 14:12 S03lightdm -> ../init.d/lightdm*
lrwxrwxrwx   1 root root    14 Nov 11 14:12 S04cups -> ../init.d/cups*
lrwxrwxrwx   1 root root    22 Nov 11 14:12 S04cups-browsed -> ../init.d/cups-browsed*
lrwxrwxrwx   1 root root    15 Nov 11 14:12 S04saned -> ../init.d/saned*
lrwxrwxrwx   1 root root    21 Nov 11 14:12 S05grub-common -> ../init.d/grub-common*
lrwxrwxrwx   1 root root    18 Nov 11 14:12 S05ondemand -> ../init.d/ondemand*
lrwxrwxrwx   1 root root    18 Nov 11 14:12 S05plymouth -> ../init.d/plymouth*
lrwxrwxrwx   1 root root    18 Nov 11 14:12 S05rc.local -> ../init.d/rc.local*


chapter 2 => linux system state/ linux services
A Linux service is an app that runs in the background waiting to be used, or carrying out essential tasks. eg. Apache and MySQL.
I will generally be unaware of services until I need them.

System state
-----------------

systemCTL is the central management tool for controlling the init system.
With systemCTL, I can start and stop, enable/disable systemd services.

* The systemctl list-units command only displays units that systemd has attempted to parse and load into memory.
* systemd will only read units that it thinks it needs, this will not necessarily include all of the available units on the system.
* To see every available unit-file within the systemd paths, including those that systemd has not attempted to load, you can use the list-unit-files command instead

systemctl list-unit-files
UNIT FILE                                  STATE
proc-sys-fs-binfmt_misc.automount          static
org.freedesktop.hostname1.busname          static
org.freedesktop.locale1.busname            static
org.freedesktop.login1.busname             static
org.freedesktop.network1.busname           static
org.freedesktop.resolve1.busname           static
org.freedesktop.systemd1.busname           static
org.freedesktop.timedate1.busname          static
dev-hugepages.mount                        static
dev-mqueue.mount                           static
proc-sys-fs-binfmt_misc.mount              static
sys-fs-fuse-connections.mount              static
sys-kernel-config.mount                    static
sys-kernel-debug.mount                     static
acpid.path                                 enabled
cups.path                                  enabled
systemd-ask-password-console.path          static
systemd-ask-password-plymouth.path         static
systemd-ask-password-wall.path             static
systemd-networkd-resolvconf-update.path    static
accounts-daemon.service                    enabled
acpid.service                              disabled
docker.service                             enabled
dbus.socket                                static  
docker.socket                              enabled

display the unit file that systemd has loaded into its system, systemctl cat docker.service and do list the dependencies of the service systemctl list-dependencies docker.service


chapter 3 => linux processes
Linux process is a "dynamic entity", constantly changing as the machine code instructions are executed by the processor. As well as the program's instructions and data, the process also includes the program counter and all of the CPU's registers as well as the process stacks containing temporary data such as routine parameters, return addresses and saved variables. The current executing program, or process, includes all of the current activity in the microprocessor.

Modern virtual memory operating systems generally have a flat memory model and not a segmented one.

List all linux processes,
$ ps -A
  PID TTY          TIME CMD
    1 ?        00:00:01 systemd
 4142 ?        00:00:02 dockerd
 4151 ?        00:00:00 docker-containerd


List all running linux processes,
ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 119836  6044 ?        Ss   12:54   0:01 /sbin/init splash
root      4142  0.0  0.9 419120 37848 ?        Ssl  12:58   0:02 /usr/bin/dockerd -H fd://
root      4151  0.0  0.2 199756 10568 ?        Ssl  12:58   0:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --shim docker-containerd-shim --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --runtime docker-runc

To kill a process,
$ pkill vim

The memory is virtualized among the processes on the system. An individual process's view of memory is as if it alone has full access to the system's physical memory.

Read more about linux process, and process virtual address space

chapter 4 => 32 or 64 bit OS
In 32-bit, a processor with 32-bit memory addresses can directly access at most (2^32) bits = 4 GiB of byte-addressable memory.
Which also means, each register stores 32 bits = 4 bytes. 32 bits (1 word) of information are processed per clock cycle.

In 64-bit computing, processors that have datapath widths, integer size, and memory-address widths of 64 bits = 8 bytes.
Which also means, each register stores 64 bits = 8 bytes.  64 bits (1 word) of information are processed per clock cycle.


prayag@prayag_f1:~$ file /sbin/init
/sbin/init: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x5f722891935c0f35fd8f318d4606b334a9b1d423, stripped

or

[ec2-user@ip-172-21-19-140 ~]$ file /lib/systemd/systemd
/lib/systemd/systemd: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=51a025073a1ab8c1a4dfa808d5af7240179400a1, stripped

ELF - https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
LSB is Linux Standard Base including fs - https://en.wikipedia.org/wiki/Linux_Standard_Base

Watch this for more info, 32 Bit vs 64 Bit - How much data the OS can handle

OS

[ec2-user@ip-172-18-21-240 ~]$ cat /proc/version
Linux version 3.10.0-514.el7.x86_64 (mockbuild@x86-039.build.eng.bos.redhat.com) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Wed Oct 19 11:24:13 EDT 2016


chapter 5 => linux access control/user groups
user groups are defined in /etc/group

$ cut -d: -f1 /etc/group
root
daemon
bin
sys
adm
lp
uucp
man
proxy
sudo
dip
sasl
plugdev
staff
games
users
nogroup
input
crontab
syslog
netdev
messagebus
ssh
pulse
pulse-access
rtkit
saned
prayagupd
sambashare
vboxsf
docker


To know which group the current user is assigned to,

groups
prayagupd adm cdrom sudo dip plugdev lpadmin sambashare vboxsf
or 
groups $USER
prayagupd : prayagupd adm cdrom sudo dip plugdev lpadmin sambashare vboxsf

To know the groups, current user assigned to with the group id/gid, which you can map in /etc/group

id $USER
uid=1000(prayagupd) gid=1000(prayagupd) groups=1000(prayagupd),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare),999(vboxsf)

Similarly, all users are defined in /etc/passwd.
cut -d: -f1 /etc/passwd

Read this for user management.

chapter 6 => package repos and PPA (package archives)

On rh,
[ec2-user@ip-172-0-0-128 ~]$ yum repolist
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
repo id                                                               repo name                                                                  status
!amzn2-core/2/x86_64                                                  Amazon Linux 2 core repository                                             16,294
amzn2extra-docker/2/x86_64                                            Amazon Extras repo for docker                                                  14
docker-ce-stable/x86_64                                               Docker CE Stable - x86_64                                                      36
repolist: 16,344

On ubuntu,

prayag@prayag_f1:~$ grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/*

/etc/apt/sources.list:# deb cdrom:[Ubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120423)]/ precise main restricted
/etc/apt/sources.list:
/etc/apt/sources.list:# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
/etc/apt/sources.list:# newer versions of the distribution.
/etc/apt/sources.list:deb http://np.archive.ubuntu.com/ubuntu/ precise main restricted
/etc/apt/sources.list:deb-src http://np.archive.ubuntu.com/ubuntu/ precise main restricted
[...]

/etc/apt/sources.list.d/dropbox.list:deb http://linux.dropbox.com/ubuntu precise main
/etc/apt/sources.list.d/dropbox.list.save:deb http://linux.dropbox.com/ubuntu precise main
/etc/apt/sources.list.d/google-chrome.list:### THIS FILE IS AUTOMATICALLY CONFIGURED ###
/etc/apt/sources.list.d/google-chrome.list:# You may comment out this entry, but any other modifications may be lost.
/etc/apt/sources.list.d/google-chrome.list:deb http://dl.google.com/linux/chrome/deb/ stable main
/etc/apt/sources.list.d/google-chrome.list.save:### THIS FILE IS AUTOMATICALLY CONFIGURED ###
/etc/apt/sources.list.d/google-chrome.list.save:# You may comment out this entry, but any other modifications may be lost.
/etc/apt/sources.list.d/google-chrome.list.save:deb http://dl.google.com/linux/chrome/deb/ stable main
/etc/apt/sources.list.d/groovy-dev-grails-precise.list:deb http://ppa.launchpad.net/groovy-dev/grails/ubuntu precise main
/etc/apt/sources.list.d/groovy-dev-grails-precise.list:deb-src http://ppa.launchpad.net/groovy-dev/grails/ubuntu precise main
/etc/apt/sources.list.d/groovy-dev-grails-precise.list.save:deb http://ppa.launchpad.net/groovy-dev/grails/ubuntu precise main
/etc/apt/sources.list.d/groovy-dev-grails-precise.list.save:deb-src http://ppa.launchpad.net/groovy-dev/grails/ubuntu precise main
/etc/apt/sources.list.d/precise-partner.list:deb http://archive.canonical.com/ubuntu precise partner #Added by software-center
/etc/apt/sources.list.d/precise-partner.list.save:deb http://archive.canonical.com/ubuntu precise partner #Added by software-center
/etc/apt/sources.list.d/ubuntu-mozilla-daily-ppa-precise.list:deb http://ppa.launchpad.net/ubuntu-mozilla-daily/ppa/ubuntu precise main
/etc/apt/sources.list.d/ubuntu-mozilla-daily-ppa-precise.list:deb-src http://ppa.launchpad.net/ubuntu-mozilla-daily/ppa/ubuntu precise main
/etc/apt/sources.list.d/ubuntu-on-rails-ppa-precise.list:deb http://ppa.launchpad.net/ubuntu-on-rails/ppa/ubuntu precise main

chapter 7 => task scheduling

When I check the /etc/cron.daily in RHEL 7 I get a following list of bash files.


[ec2-user@ip-172-21-19-140 ~]$ ll /etc/cron.daily/
total 12
-rwx------. 1 root root 219 Jul 14  2016 logrotate
-rwxr-xr-x. 1 root root 618 Mar 17  2014 man-db.cron
-rwx------. 1 root root 256 Sep 13  2016 rhsmd

And on ubuntu distro, 


prayag@prayag_f1:~$ ls -l /etc/cron.daily/
total 64
-rwxr-xr-x 1 root root   311 May 25  2012 0anacron
-rwxr-xr-x 1 root root   219 Oct  1 15:44 apport
-rwxr-xr-x 1 root root 15466 Oct 16 16:44 apt
-rwxr-xr-x 1 root root   355 Jun 19 03:42 bsdmainutils
-rwxr-xr-x 1 root root   384 May 25  2012 cracklib-runtime
-rwxr-xr-x 1 root root   256 Oct  2 01:15 dpkg
-rwxr-xr-x 1 root root   372 Oct  3 04:17 logrotate
-rwxr-xr-x 1 root root  1365 Sep 19 04:29 man-db
-rwxr-xr-x 1 root root   606 Aug 15 09:11 mlocate
-rwxr-xr-x 1 root root   249 Sep  7 00:59 passwd
-rwxr-xr-x 1 root root  2417 Jul  2  2011 popularity-contest
-rwxr-xr-x 1 root root  2947 Jun 15 00:41 standard
-rwxr-xr-x 1 root root   214 Oct 10 04:26 update-notifier-common

One of them, /etc/cron.daily/apt does variety of things based on apt configuration settings, which are maintained in /etc/apt/apt.conf and the various include files under /etc/apt/apt.conf.d/

The /etc/apt is something like this :

#!/bin/sh
[...]
check_stamp()
{
    stamp="$1"
    interval="$2"

    if [ $interval -eq 0 ]; then
    debug_echo "check_stamp: interval=0"
    # treat as no time has passed
        return 1
    fi

    if [ ! -f $stamp ]; then
    debug_echo "check_stamp: missing time stamp file: $stamp."
    # treat as enough time has passed
        return 0
    fi

    # compare midnight today to midnight the day the stamp was updated
    stamp_file="$stamp"
    stamp=$(date --date=$(date -r $stamp_file --iso-8601) +%s 2>/dev/null)
    if [ "$?" != "0" ]; then
        # Due to some timezones returning 'invalid date' for midnight on
        # certain dates (eg America/Sao_Paulo), if date returns with error
        # remove the stamp file and return 0. See coreutils bug:
        # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
        rm -f "$stamp_file"
        return 0
    fi

    now=$(date --date=$(date --iso-8601) +%s 2>/dev/null)
    if [ "$?" != "0" ]; then
        # As above, due to some timezones returning 'invalid date' for midnight
        # on certain dates (eg America/Sao_Paulo), if date returns with error
        # return 0.
        return 0
    fi

    delta=$(($now-$stamp))

    # intervall is in days, convert to sec.
    interval=$(($interval*60*60*24))
    debug_echo "check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"

    # remove timestamps a day (or more) in the future and force re-check
    if [ $stamp -gt $(($now+86400)) ]; then
         echo "WARNING: file $stamp_file has a timestamp in the future: $stamp"
         rm -f "$stamp_file"
         return 0
    fi

    if [ $delta -ge $interval ]; then
        return 0
    fi

    return 1
}

update_stamp()
{
    stamp="$1"
    touch $stamp
}
[...]


chapter 8 => network proxy
specify system-wide environment variables http_proxy and https_proxy  as below,

prayag@prayag_f1:~$ cat /etc/environment

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
http_proxy="http://10.xx.xxx.xxx:8080/"
https_proxy="https://10.xx.xxx.xxx:8080/"
ftp_proxy="ftp://10.xx.xxx.xxx:8080/"
socks_proxy="socks://10.xx.xxx.xxx:8080/"

Read about Networking and proxy server.

SOCKet Secure (SOCKS) is an Internet protocol that routes network packets between a client and server through a proxy-server.

prayag@prayag_f1:~$ env | grep -i proxy
NO_PROXY=localhost,127.0.0.0/8
http_proxy=http://10.xx.xxx.xxx:8080/
ftp_proxy=ftp://10.xx.xxx.xxx:8080/
ALL_PROXY=socks://10.xx.xxx.xxx:8080/
all_proxy=socks://10.xx.xxx.xxx:8080/
socks_proxy=socks://10.xx.xxx.xxx:8080/
UBUNTU_MENUPROXY=libappmenu.so
https_proxy=https://10.xx.xxx.xxx:8080/
no_proxy=localhost,127.0.0.0/8

I also can add env variables to /etc/profile and source it to see immediate effect.

chapter 9 => sudo version


prayag@prayag_f1:~$ sudo sudo -V
[...]
Sudoers I/O plugin version 1.8.5p2

chapter 10 => Install packages (ubuntu)
prayag@prayag:~/Downloads$ sudo dpkg -i TrackerSetup.deb

Or with gDEBi,

prayag@prayag:~/Downloads$ sudo apt-get install gdebi
prayag@prayag:~/Downloads$ sudo gdebi TrackerSetup.deb 

check about the installed package
prayag@prayag:~/Downloads$ dpkg -s gedit

http://askubuntu.com/a/17829/37643

chapter 11 => play popular non-free media formats (ubuntu)
sudo apt-get install ubuntu-restricted-extras
sudo apt-get install libavcodec-extra-53 libavcodec53 ffmpeg



References

How to install REALVNC gui in ubuntu 12.04?

How can I specify a display?

Installing & Running VNC on Redhat/RPM Linux

Starting firefox on a remote host (over ssh) opens a new window locally: what is happening?

visit url with command line

http://waxborg.servepics.com/opensuse/remote-administration/running-firefox-remotely-over-ssh

How to shut down the computer after a task has been completed?, http://askubuntu.com/a/15536

How can I install software or packages without Internet (offline)?

35 Tricky and Complex Unix Interview Questions and Commands (Part 1)

Linux Hacks gist, https://gist.github.com/prayagupd/4730009

No comments:

Post a Comment