Thursday, December 31, 2015

password and chown related notes

shadow passwords and pasword policy:::
password stored in /etc/shadow
to see that be root and cat /etc/shadow
[root@new-host-4 ~]# chage -l ashok
Last password change                                    : never
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7

[root@new-host-4 ~]# chage -d 0 ashok      === will force to update password on the very next login attempt for the user.
chage -E yyyy-mm-dd   = set an expiry date for the password

ashok@192.168.10.9's password:
Last login: Wed Dec 30 22:41:11 2015 from 192.168.10.8
[ashok@new-host-4 ~]$ /etc/shadow
-bash: /etc/shadow: Permission denied
[ashok@new-host-4 ~]$ sudo /etc/shadow
[sudo] password for ashok:
sudo: /etc/shadow: command not found
[ashok@new-host-4 ~]$ /etc/shadow
-bash: /etc/shadow: Permission denied
[ashok@new-host-4 ~]$ su -
Password:
Last login: Wed Dec 30 22:46:39 EST 2015 on pts/0
[root@new-host-4 ~]# cat /etc/shadow
root:$6$1ZLgIZk9$b356o6XdyONjLO6gM73ZiUJ9PMca28vWSHfOMTCLPYsJC9PZEXarB01OVT2VgDyK9DHriCveHWTCL3XlIXo7.1:16799:0:99999:7:::
bin:*:16659:0:99999:7:::
daemon:*:16659:0:99999:7:::
adm:*:16659:0:99999:7:::
lp:*:16659:0:99999:7:::
sync:*:16659:0:99999:7:::
shutdown:*:16659:0:99999:7:::
halt:*:16659:0:99999:7:::
mail:*:16659:0:99999:7:::
operator:*:16659:0:99999:7:::
games:*:16659:0:99999:7:::
ftp:*:16659:0:99999:7:::
nobody:*:16659:0:99999:7:::
avahi-autoipd:!!:16796::::::
systemd-bus-proxy:!!:16796::::::
systemd-network:!!:16796::::::
dbus:!!:16796::::::
polkitd:!!:16796::::::
tss:!!:16796::::::
postfix:!!:16796::::::
sshd:!!:16796::::::
ashok:$6$PbsfPTSy00gDE9.i$vyENBhp7zuQbrA820n0WRNrtpnCUEOTNl9sxrkVZcdVGDCDpaiD0puWUcYatHP8R3dqxaeUADJZHzqcdg.s441::0:99999:7:::
devi:$6$zMZ78S9l$8FVniuXvTtkpPTl8Me6VVUEXdnkdqABIh2FdVB5I/ZuUtrXkrKgdNgJf5s6LGVv4kA.9dm4.NvRz//zUCd3Wd/:16800:0:99999:7:::
geoclue:!!:16797::::::
usbmuxd:!!:16797::::::
libstoragemgmt:!!:16797::::::
rpc:!!:16797:0:99999:7:::
unbound:!!:16797::::::
rtkit:!!:16797::::::
chrony:!!:16797::::::
colord:!!:16797::::::
abrt:!!:16797::::::
saslauth:!!:16797::::::
setroubleshoot:!!:16797::::::
qemu:!!:16797::::::
radvd:!!:16797::::::
pulse:!!:16797::::::
rpcuser:!!:16797::::::
nfsnobody:!!:16797::::::
gdm:!!:16797::::::
gnome-initial-setup:!!:16797::::::
avahi:!!:16797::::::
ntp:!!:16797::::::
tcpdump:!!:16797::::::
kamal:$6$FyCVh1Kq$6u5H8sG6F7P8v.vruuES2up8l.behwzseIWwdmD1nqy8A.WA7wT0gay/X5VMyGqIAaATtXn4VXA4Usy6C9SP4/:16798:0:99999:7:::
sachin:$6$guWE/XTA$xOrtQofhj/5mCBWH025bdmLhNRyumrui4Kr5T.x3cxqL0/rT53U0GZu6x7I4ajrpeDlTMVVfSHuGZxjG2v.hm.:16799:0:99999:7:::
hari:!!:16799:0:99999:7:::
jullet:$6$PEFa96HD$Bm91uVhEtutf4KexUxqbsp8IFiahpSjdApYFyi2wRFkZwlA5HNognyD.cOjb02gebaYfJUPeOVyqkXKxSzMUK/:16799:0:99999:7:::
romeo:$6$D1.VS6ys$/O7buiTC7O0NWEBd.ozT5lIcYByJ9ICFK1eZq7dWF0JUhpp6jC9m4VjHVL9lxpUUFan1w16dtzds2ONEmZEL6.:16799:0:99999:7:::
hamlet:$6$zz7JYbWS$Ga.a9eLlOE9hAybd.p2slgBFH0PtDY64UDb5tC6YNE1M10PLRn338bjgwPkC5juSBPHHaph/W0peBOQQtq3Ww/:16799:0:99999:7:::
dolly:$6$y.9RDJ6j$k9zR8XeZyi8.3CLLTkklD1gnEJDaY1Mf.RtgHO4yUE2IrH9R2jwLqRfnxwrcPh/QpWw/vreYWZdWsGucSnzwN/:16799:0:99999:7:::
elvis:$6$FTyDcxr9$19dToz5zVGCBIPm7BlFsRwEUeOgnBMMXKIq/rKPnnROB4uIFN9rPIE29RwMgQ41NDa9irQEdUqokErl7UvgAc1:16799:0:99999:7:::
reba:$6$Rs9g8bWS$v6cGYROGlSxbBVvHhtsqC0A2IWGmYJEdIOMPQ2WrmcW2.7WsaR.kdVh.D1ys42vp2XjTW5PBKTSjf8QLGoSEK0:16799:0:99999:7:::
amrit:!!:16799:0:99999:7:::
apache:!!:16800::::::
[root@new-host-4 ~]# chage -l ashok
Last password change                                    : never
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7

[root@new-host-4 ~]# chage -d 0 ashok
[root@new-host-4 ~]# useradd hari
useradd: user 'hari' already exists
[root@new-host-4 ~]# useradd sita
[root@new-host-4 ~]# usermod -L sita
[root@new-host-4 ~]# su sita
[sita@new-host-4 root]$ man usermod
[sita@new-host-4 root]$
[sita@new-host-4 root]$ usermod -L -e 1 sita
bash: /sbin/usermod: Permission denied
[sita@new-host-4 root]$ su root
Password:
[root@new-host-4 ~]# usermod -L -e 1 sita
[root@new-host-4 ~]# su - sita
Last login: Thu Dec 31 00:34:29 EST 2015 on pts/0
[sita@new-host-4 ~]$ id sita
uid=1235(sita) gid=1235(sita) groups=1235(sita)
[sita@new-host-4 ~]$ usermod -U sita
-bash: /usr/sbin/usermod: Permission denied
[sita@new-host-4 ~]$ su root
Password:
[root@new-host-4 sita]# useradd harry


[root@new-host-4 sita]# usermod -s /sbin/nologin harry
[root@new-host-4 sita]# su harry
This account is currently not available.

to require a password  change every 90 days

[ashok@new-host-4 ~]$ sudo chage -M 90 romeo
[sudo] password for ashok:
[ashok@new-host-4 ~]$ man chage
[ashok@new-host-4 ~]$ sudo chage -l romeo
Last password change                                    : Dec 30, 2015
Password expires                                        : Mar 29, 2016
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 90
Number of days of warning before password expires       : 7

force the password change on the first login for the any account
sudo chage -d 0 romeo
to set a expiry date on future:::
  first of all get what date is that::
  date -d "+180 days" then
  sudo chage -E 2016-08-03 expires on this date on future
   sudo vi /etc/login.defs(password stored in /etc/login.defs
   kamal@192.168.10.9's password:
Last login: Thu Dec 31 09:43:32 2015
[kamal@new-host-4 ~]$ passwd ashok
passwd: Only root can specify a user name.
[kamal@new-host-4 ~]$ root
bash: root: command not found...
[kamal@new-host-4 ~]$ su -
Password:
Last login: Thu Dec 31 00:45:01 EST 2015 on pts/0
Last failed login: Thu Dec 31 01:02:53 EST 2015 on pts/1
There were 2 failed login attempts since the last successful login.
[root@new-host-4 ~]# passwd
Changing password for user root.
New password:
Password change aborted.
New password:
Password change aborted.
New password:
[root@new-host-4 ~]# passwd ashok
Changing password for user ashok.
New password:
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
[root@new-host-4 ~]# su ashok
[ashok@new-host-4 root]$ cd
[ashok@new-host-4 ~]$ sudo chage -M 90 romeo
[sudo] password for ashok:
[ashok@new-host-4 ~]$ man chage
[ashok@new-host-4 ~]$ sudo chage -l romeo
Last password change                                    : Dec 30, 2015
Password expires                                        : Mar 29, 2016
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 90
Number of days of warning before password expires       : 7
[
[ashok@new-host-4 ~]$ sudo vi /etc/login.defs( passwd login details of all users paswd change and many more)

using chown to change the ownership of the file for eg here a file called foofiles is created by root user and using chown changed to kamaluser
[root@new-host-4 ~]# touch foofiles
[root@new-host-4 ~]# ls -l foofiles
-rw-r--r--. 1 root root 0 Dec 31 10:29 foofiles
[root@new-host-4 ~]# chown kamal foofiles
[root@new-host-4 ~]# ls -l foofiles
-rw-r--r--. 1 kamal root 0 Dec 31 10:29 foofiles
chown -R performs recusrsive change of ownership includiong directories and  its conteent



[sudo] password for ashok:
sudo: vim/etc/login.defs: command not found
[ashok@new-host-4 ~]$ which etc
/usr/bin/which: no etc in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[ashok@new-host-4 ~]$ man which
[ashok@new-host-4 ~]$ sudo vim/etc/login.defs
sudo: vim/etc/login.defs: command not found
[ashok@new-host-4 ~]$ sudo vi /etc/login.defs
[ashok@new-host-4 ~]$ ^C
[ashok@new-host-4 ~]$ groupadd -g
bash: /sbin/groupadd: Permission denied
[ashok@new-host-4 ~]$ sudo groupadd -g 4000 consultants
[ashok@new-host-4 ~]$ tail -f /etc/group
apache:x:48:
abc:x:1451:ashok
abcd:x:5001:ashok
wheel:x:10:ashok,kamal
addd:x:5002:
shakespeare:x:3000:jullet,romeo,hamlet
artists:x:5003:reba,dolly,elvis
sita:x:1235:
harry:x:1236:
consultants:x:4000:
^C
[ashok@new-host-4 ~]$ sudo useradd -G consulatants sspade
useradd: group 'consulatants' does not exist
[ashok@new-host-4 ~]$ sudo useradd -G consultants sspade
[ashok@new-host-4 ~]$ sudo useradd -G consultants bboop
[ashok@new-host-4 ~]$ sudo useradd -G consultants dtracy
[ashok@new-host-4 ~]$ tail -5 /etc/group
harry:x:1236:
consultants:x:4000:sspade,bboop,dtracy
sspade:x:1237:
bboop:x:1238:
dtracy:x:1239:
[ashok@new-host-4 ~]$ passwd sspade
passwd: Only root can specify a user name.
[ashok@new-host-4 ~]$ sudo passwd sspade

passwd: Unknown user name 'dtrac'.
[ashok@new-host-4 ~]$ sudo passwd dtracy
Changing password for user dtracy.
New password:
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password:
Sorry, passwords do not match.
New password:
Retype new password:
Sorry, passwords do not match.
New password:
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password:
Sorry, passwords do not match.
passwd: Have exhausted maximum number of retries for service
[ashok@new-host-4 ~]$ passwd dtracy
passwd: Only root can specify a user name.
[ashok@new-host-4 ~]$ sudo passwd dtracy
Changing password for user dtracy.
New password:
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
[ashok@new-host-4 ~]$ sudo chage -d 0 dtracy
[ashok@new-host-4 ~]$ lab localusers grade
bash: lab: command not found...
[ashok@new-host-4 ~]$



Wednesday, December 30, 2015

useradd usermod groupadd groupmod basics

groupadd groupmod and useradd and usermod
groupmod: change the group name to a GID mapping
groupmod -n  newgroupname oldgroupname ( this command gives a new name to old group)
similiarly -g aoptions help to specify a new GID(it provides anew GID whereas can also replace the old one with the new one

[root@new-host-4 ~]# id ashok
uid=1000(ashok) gid=1000(abc) groups=1000(abc),10(wheel)
[root@new-host-4 ~]# groupmod -n ashok wheel
[root@new-host-4 ~]# id ashok
uid=1000(ashok) gid=1000(abc) groups=1000(abc),10(ashok)
[root@new-host-4 ~]# groupmod -g 1451 abc
[root@new-host-4 ~]# id ashok
uid=1000(ashok) gid=1451(abc) groups=1451(abc),10(ashok)
[root@new-host-4 ~]# groupadd -g 1345 abcd
[root@new-host-4 ~]# groupadd abcd


bash: q: command not found...
[root@new-host-4 ~]# clear
[root@new-host-4 ~]# groupadd -g 3000 shakespeare
[root@new-host-4 ~]# groupadd artists
[root@new-host-4 ~]# tail -5 etc/group
tail: cannot open âetc/groupâ for reading: No such file or directory
[root@new-host-4 ~]# tail -5 /etc/group
abcd:x:5001:ashok
wheel:x:10:ashok,kamal
addd:x:5002:
shakespeare:x:3000:
artists:x:5003:
[root@new-host-4 ~]# usermod -G shakespeare juliet
usermod: user 'juliet' does not exist
[root@new-host-4 ~]# useradd jullet | usermod --G shakespeare jullet
usermod: unrecognized option '--G'
Usage: usermod [options] LOGIN

Options:
  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group
  -G, --groups GROUPS           new list of supplementary GROUPS
  -a, --append                  append the user to the supplemental GROUPS
                                mentioned by the -G option without removing
                                him/her from other groups
  -h, --help                    display this help message and exit
  -l, --login NEW_LOGIN         new value of the login name
  -L, --lock                    lock the user account
  -m, --move-home               move contents of the home directory to the
                                new location (use only with -d)
  -o, --non-unique              allow using duplicate (non-unique) UID
  -p, --password PASSWORD       use encrypted password for the new password
  -R, --root CHROOT_DIR         directory to chroot into
  -s, --shell SHELL             new login shell for the user account
  -u, --uid UID                 new UID for the user account
  -U, --unlock                  unlock the user account
  -Z, --selinux-user SEUSER     new SELinux user mapping for the user account
 
 the shakespeare group consists of jullet romeo and hamlet and artists group consists of reba dolly and elvis:::
useradd: user 'jullet' already exists
[root@new-host-4 ~]# useradd jullet | usermod -G shakespeare jullet
useradd: user 'jullet' already exists
[root@new-host-4 ~]# id jullet
uid=1005(jullet) gid=1005(jullet) groups=1005(jullet),3000(shakespeare)
[root@new-host-4 ~]# usermod -g shakespeare jullet
[root@new-host-4 ~]# id jullet
uid=1005(jullet) gid=3000(shakespeare) groups=3000(shakespeare)
[root@new-host-4 ~]# usermod -G shakespeare jullet
[root@new-host-4 ~]# id jullet
uid=1005(jullet) gid=3000(shakespeare) groups=3000(shakespeare)
[root@new-host-4 ~]# usermod -G shakespeare romeo | useradd romeo
useradd: user 'romeo' already exists
[root@new-host-4 ~]# id romeo
uid=1006(romeo) gid=1006(romeo) groups=1006(romeo),3000(shakespeare)
[root@new-host-4 ~]# usermod -G shakespeare hamlet | useradd hamlet
useradd: user 'hamlet' already exists
[root@new-host-4 ~]# id hamlet
uid=1007(hamlet) gid=1007(hamlet) groups=1007(hamlet),3000(shakespeare)
[root@new-host-4 ~]# usermod -G artists reba || useradd reba
[root@new-host-4 ~]# id reba
uid=1010(reba) gid=1010(reba) groups=1010(reba),5003(artists)
[root@new-host-4 ~]# usermod -G artists dolly
[root@new-host-4 ~]# id dolly
uid=1008(dolly) gid=1008(dolly) groups=1008(dolly),5003(artists)
[root@new-host-4 ~]# usermod -G artists elvis
[root@new-host-4 ~]# id elvis
uid=1009(elvis) gid=1009(elvis) groups=1009(elvis),5003(artists)
[root@new-host-4 ~]# tail -5 /etc/group
abcd:x:5001:ashok
wheel:x:10:ashok,kamal
addd:x:5002:
shakespeare:x:3000:jullet,romeo,hamlet
artists:x:5003:reba,dolly,elvis
[root@new-host-4 ~]#

[root@new-host-4 ~]# usermod -g abcd ashok
[root@new-host-4 ~]# id ashok
uid=1000(ashok) gid=5001(abcd) groups=5001(abcd),1451(abc),10(ashok)



to remove a group:
groupdel
usermod:: we can change users primary as well as secondary groups using usermod
only root access or sudo command can carry out all these tasks.
usermod -g groupname user changes the current group of user if assigned new group on groupname.
(primary group)
usermod -aG groupname username   does the same a helps to append and prevent the user to be deleted from other supplemental groups (secondary group)

managing local linux users and groups

display command output to terminal ignore all errors:::   2>/dev/null
send command output to files ;errors to different file::   >file 2>file2
send output and errors to the same new,empty files::: &>file
send output and errors to the same file but preserve existing file content::: >>file 2>&1
run a command but throw away all possible terminal displays:: &>/dev/null
send command output to both the screen and a file at the same time::; |tee file
run command save output in a file discard error messages ::: >file 2>/dev/null



managinf local linux users and groups:::

 id command gives the information about the current logged in user
 ps = to see the processes on the current shell
 ps -a to see all the processes
 example of some use of sudo is given below:::
 [ashok@new-host-4 ~]$ tail /var/log/secure
tail: cannot open â/var/log/secureâ for reading: Permission denied
[ashok@new-host-4 ~]$ sudo tail -n3 et

[sudo] password for ashok:
Sorry, try again.
[sudo] password for ashok:
tail: cannot open âetâ for reading: No such file or directory
[ashok@new-host-4 ~]$ logout
bash: logout: not login shell: use `exit'
[ashok@new-host-4 ~]$ exit
exit
[kamal@new-host-4 root]$ su ashok
Password:
[ashok@new-host-4 root]$ su
Password:
[root@new-host-4 ~]# su ashok
bash: [ashok@new-host-4: command not found...
[ashok@new-host-4 ~]$ cat: /var/log/secure: Permission denied
bash: cat:: command not found...
Similar command is: 'cat'
[ashok@new-host-4 ~]$ [ashok@new-host-4 root]$ cd
bash: [ashok@new-host-4: command not found...
[ashok@new-host-4 ~]$ [ashok@new-host-4 ~]$ sudo tail /var.log.secure
bash: [ashok@new-host-4: command not found...
[ashok@new-host-4 ~]$ tail: cannot open â/var.log.secureâ for reading: No such file or directory
bash: tail:: command not found...
Similar command is: 'tail'
[ashok@new-host-4 ~]$ [ashok@new-host-4 ~]$ sudo tail /var/log/secure
bash: [ashok@new-host-4: command not found...
[ashok@new-host-4 ~]$ Dec 30 13:15:45 new-host-4 gdm-launch-environment]: pam_unix(gdm-launch-environm                                                                             ent:session): session opened for user gdm by (uid=0)
bash: syntax error near unexpected token `('
[ashok@new-host-4 ~]$ Dec 30 13:16:09 new-host-4 polkitd[763]: Registered Authentication Agent for uni                                                                             x-session:c1 (system bus name :1.30 [gnome-shell --mode=gdm], object path /org/f                                                                             reedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8)
bash: syntax error near unexpected token `('
[ashok@new-host-4 ~]$ Dec 30 13:16:47 new-host-4 sshd[2968]: Accepted password for ashok from 192.168.                                                                             1.7 port 49493 ssh2
bash: Dec: command not found...
[ashok@new-host-4 ~]$ Dec 30 13:16:48 new-host-4 sshd[2968]: pam_unix(sshd:session): session opened fo                                                                             r user ashok by (uid=0)
bash: syntax error near unexpected token `('
[ashok@new-host-4 ~]$ Dec 30 13:17:34 new-host-4 su: pam_unix(su-l:session): session opened for user r                                                                             oot by ashok(uid=1000)
bash: syntax error near unexpected token `('
[ashok@new-host-4 ~]$ Dec 30 13:17:39 new-host-4 su: pam_unix(su:session): session opened for user kam                                                                             al by ashok(uid=0)
bash: syntax error near unexpected token `('
[ashok@new-host-4 ~]$ Dec 30 13:17:49 new-host-4 su: pam_unix(su:session): session opened for user ash                                                                             ok by ashok(uid=1002)
bash: syntax error near unexpected token `('
[ashok@new-host-4 ~]$ Dec 30 13:20:58 new-host-4 sudo:   ashok : TTY=pts/0 ; PWD=/root ; USER=root ; C                                                                             OMMAND=/sbin/usermod ashok
bash: Dec: command not found...
bash: C: command not found...
[ashok@new-host-4 root]$ Dec 30 13:22:02 new-host-4 sudo:   ashok : TTY=pts/0 ; PWD=/home/ashok ; USER=ro                                                                             ot ; COMMAND=/bin/tail /var.log.secure
bash: Dec: command not found...
bash: ot: command not found...
bash: /var.log.secure: No such file or directory
[ashok@new-host-4 ~]$ Dec 30 13:22:34 new-host-4 sudo:   ashok : TTY=pts/0 ; PWD=/home/ashok ; USER=ro                                                                             ot ; COMMAND=/bin/tail /var/log/secure
bash: Dec: command not found...
bash: ot: command not found...
bash: /var/log/secure: Permission denied
[ashok@new-host-4 ~]$ [ashok@new-host-4 ~]$ tail /var/log/secure
bash: [ashok@new-host-4: command not found...
[ashok@new-host-4 ~]$ tail: cannot open â/var/log/secureâ for reading: Permission denied
bash: tail:: command not found...
Similar command is: 'tail'
[ashok@new-host-4 ~]$ [ashok@new-host-4 ~]$ sudo tail -n3 et
bash: [ashok@new-host-4: command not found...
[ashok@new-host-4 ~]$
[ashok@new-host-4 ~]$ [sudo] password for ashok:
bash: [sudo]: command not found...
[ashok@new-host-4 ~]$ Sorry, try again.
bash: Sorry,: command not found...
[ashok@new-host-4 ~]$ [sudo] password for ashok:
bash: [sudo]: command not found...
[ashok@new-host-4 ~]$ tail: cannot open âetâ for reading: No such file or directory
bash: tail:: command not found...
Similar command is: 'tail'
[ashok@new-host-4 ~]$ [ashok@new-host-4 ~]$ logout
bash: [ashok@new-host-4: command not found...
[ashok@new-host-4 ~]$ bash: logout: not login shell: use `exit'
> [ashok@new-host-4 ~]$ exit
> exit
> [kamal@new-host-4 root]$ su ashok
> Password:
> [ashok@new-host-4 root]$ su
> Password:
> [root@new-host-4 ~]# su ashok

> here is another example of excessig file using sudo that could be only accessed by root user...
[ashok@new-host-4 ~]$ sudo vi etc/motd
groupadd helps to add group::: syntax   groupadd -r ashok adds automatically the group id number from the valid one

[sudo] password for ashok: we need to have our user password not the root one..
after that the file opens and we can make our own edit according to our choice using insert mode.

 usermod::
 userdel::
 useradd::

 [root@new-host-4 ~]# id sachin
uid=1003(sachin) gid=1003(sachin) groups=1003(sachin)
[root@new-host-4 ~]# id ramesh
uid=1004(ramesh) gid=1004(ramesh) groups=1004(ramesh)
[root@new-host-4 ~]# userdel ramesh
[root@new-host-4 ~]# useradd hari
[root@new-host-4 ~]# id hari
uid=1004(hari) gid=1004(hari) groups=1004(hari)

a newly assigned user always takes the id that is recently available which may be the id of recently deleted one.so if we just delete the user not its home directory with options -r , the new user can get access to that content..

Getting Help in RED HAT enterprise Linux :chapter 2:

::
navigating man pages:::
spacebar = scroll forward (down) one screen
Pagedown = scroll forward (down) ine screen
d = scroll forward down one screen
u = scroll forward (up) one half screen
/string = search forward (down) for string in the main page
n = repeat previous search forward (down) in the man page
se4arching for man pages for keyword::
man -k keyword
man -t = to prepare a man page for printing
lp passwd . ps -P 2-3. -P option specifies specific pages  ( to print any pages)

gedit + filename = research the gedit option used to begin an editing session with the cursor at the end of the file

basics file management linux

hey this is the first file and yes i am exciterd[ashok@new-host-4 ~]$ cd aaa
[ashok@new-host-4 aaa]$ ls -l
total 0
drwxrwxr-x. 3 ashok ashok 16 Dec 28 23:20 bbb
[ashok@new-host-4 aaa]$ ls -a
.  ..  bbb
[ashok@new-host-4 aaa]$ touch ashok
[ashok@new-host-4 aaa]$ cat ashok
[ashok@new-host-4 aaa]$ cat>> ashok
hey this is the first file and yes i am exciterd^C
[ashok@new-host-4 aaa]$



mkdir -p command creates a parent directory if not existed already
 for eg: here it creates thesis as a parent directory along with [ashok@new-host-4 ~]$ mkdir -p thesis/chapter1 thesis/chapter2
l[ashok@new-host-4 ~]$ mkdir -p thesis/chapter1 thesis/chapter2
[ashok@new-host-4 ~]$ ls - R thesis
thesis:
chapter1  chapter2
 ls -R = listing recursive directories along with the files
 cp -R = to copy non empty directories ( - r especially used for doing anything with the  non empty touch song{directories
 ~+ = current working directory
 brace expansion: [ashok@new-host-4 globe]$ echo file{a,b}{1,2}
filea1 filea2 fileb1 fileb2
 command subsitution:
 `(backsticks not single inverted commas ) are quite old ..
 now used $() instead of backsticks ..$() can form even nested loops but backsticks cannot 
 single inverted commas '' cannot redirect command substituition but "" does
 processes::
 ps command =listing current processes
 ps -aux = all processes with columns and includes processes without a controlling terminal
User       PID  %CPU  %mem    VSZ  RSS TTY STAT     START TIME  COMMAND
 root        39  0.0  0.0      0     0 ?        S    01:35   0:00 [rcuob/30]
root        40  0.0  0.0      0     0 ?        S    01:35   0:00 [rcuob/31]
root        41  0.0  0.0      0     0 ?        R    01:35   0:23 [rcu_sched]
root        42  0.0  0.0      0     0 ?        S    01:35   0:20 [rcuos/0]
root        43  0.0  0.0      0     0 ?        S    01:35   0:00 [rcuos/1]
also ps-lax = long listing option but avoids username lookup so is faster comparativelyps - 
ps -ef also is alos a similiar unix command

Running jobs in the background::
& sign runs th job in background
(%job number )  reconnect to the controlling terminal bringing back to the foreground



basic red hat linux training

rhel 123 notes ashok


to power off the system ctrl+Alt+DEL


note: yelp command on gnome used as help


to change password we use passwd command


head and tail command : to see beginning and end of the file respectively  ( head -n1 first 1 line from the above and tail -n1 last line from the end )
uparrrow gives previous history command , down arrow gives the very next history command left and right move along the character where cursor is placed
..
man -t : to prepare a man page for printing using PostScript


tar -cvzf destination filename (compresses file and z compresses)


if used j instead of z ,compresses more


in gnome shell ctrl+ alt + L to lock the user  if graphical user is idle for few minutes


command history  ; history command   !1 showa the first command in the history
!a = shows any command on history having a as string
esc + .  = appends the very previous word on the current cursor


editing the command line::
ctrl + a = come to the start of the command line
ctrl + e = go to the end of the command line
ctrl + u = clear the cursor from the beginning of the command line
CTRL +  k = clear from the cursor  to the end of the command line

~ = home directory
. = current directory
.. = parent directory
cd - = previous directory
.files = hidden files


formation of triangle

# to find out if given sides form a triangle
a=input("enter first side")
b=input("enter second side")
c=input("enter third side")
d=int(a)
e=int(b)
f=int(c)
if d+e>f and e+f>d and f+d>e:
    print("it is atriangle")
else:
    print("not triangle")
 

#to check odd or even
a=input("enter a number")
b=int(a)
if (b%2==0):
    print("it is even")

else:
    print("it is odd")
 
# Having no programming background it is slow but a great achievement for me, i am slowly learning to code.
Python 3.4.2 (default, Jun 19 2015, 11:34:49)
[GCC 4.9.1] on linux
Type "copyright", "credits" or "license()" for more information.
>>> import math
>>> math.ceil(math.sqrt(9))
3
>>> math.ceil(math.sqrt(9.5))
4
>>> ================================ RESTART ================================
>>>
37.69911184307752
113.09733552923255
12
31.41592653589793
78.53981633974483
10
>>> ================================ RESTART ================================
>>>
area and perimeter
30
22
>>> ================================ RESTART ================================
>>>
113.09733552923255
78.53981633974483
>>> ================================ RESTART ================================
>>>
>>> =====

python programming

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> print(7*2+4)
18
>>> ================================ RESTART ================================
>>> width=7
>>> height=4
>>> print(height*2+width*2)#perimeter
22
>>> print(length*width)#area
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    print(length*width)#area
NameError: name 'length' is not defined
>>> print(Width*height)#aea
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    print(Width*height)#aea
NameError: name 'Width' is not defined
>>>  print(width*height)

SyntaxError: unexpected indent
>>> print(width*height)#area
28
>>> width
7
>>> 5%2
1
>>> g=float(pi)+pi
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    g=float(pi)+pi
NameError: name 'pi' is not defined
>>> pi=3.5
>>> g=float(pi)
>>> g
3.5
>>> g=int(pi)
>>> g
3
>>> 11//2
5
>>> 11.0//2.0
5.0
>>> 11/2.0
5.5
>>> 11//2.0
5.0
>>> g=8
>>> g+=2
>>> g=
SyntaxError: invalid syntax
>>> g
10
>>> x=40
>>> x==40
True
>>> 40=x
SyntaxError: can't assign to literal
>>> x==45
False
>>> 3**2
9
>>> x=20
>>> x*=3
>>> x
60
>>> x==59
False
>>> x!=59
True
>>> ================================ RESTART ================================
>>> 32
32
>>> ================================ RESTART ================================
>>> 30+50*10
530
>>> (30+50)*10
800
>>> ================================ RESTART ================================
>>> #this says hello
>>> '''hi again this along '''
'hi again this along '
>>> hey how are you doing""
SyntaxError: invalid syntax
>>> 6/10
0.6
>>>

RHEL7 - ssh-keygen setup

to login without supplying password, you generate key and copy public key to user's home direcotry under .ssh as authorized_keys

[ashok@new-host-4 .ssh]$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/ashok/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ashok/.ssh/id_dsa.
Your public key has been saved in /home/ashok/.ssh/id_dsa.pub.
The key fingerprint is:
f2:ae:fc:ac:75:e1:46:4b:8a:9e:f1:94:14:bf:59:d7 ashok@new-host-4.home
The key's randomart image is:
+--[ DSA 1024]----+
|                 |
|                 |
|        .        |
|         o     . |
|      . S = . . E|
|       = * * .   |
|      o * B      |
|     o O o       |
|      *+=        |
+-----------------+
[ashok@new-host-4 .ssh]$ pwd
/home/ashok/.ssh
[ashok@new-host-4 .ssh]$ ls
id_dsa  id_dsa.pub  known_hosts
[ashok@new-host-4 .ssh]$ ls -l
total 12
-rw-------. 1 ashok ashok 668 Dec 30 19:38 id_dsa
-rw-r--r--. 1 ashok ashok 611 Dec 30 19:38 id_dsa.pub
-rw-r--r--. 1 ashok ashok 348 Dec 30 19:34 known_hosts
[ashok@new-host-4 .ssh]$ cp id_dsa.pub authorized_keys
[ashok@new-host-4 .ssh]$ ls -ltr
total 16
-rw-r--r--. 1 ashok ashok 348 Dec 30 19:34 known_hosts
-rw-------. 1 ashok ashok 668 Dec 30 19:38 id_dsa
-rw-r--r--. 1 ashok ashok 611 Dec 30 19:38 id_dsa.pub
-rw-r--r--. 1 ashok ashok 611 Dec 30 19:39 authorized_keys
[ashok@new-host-4 .ssh]$ ssh localhost
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is e8:7e:e8:d0:24:57:4f:18:64:bd:2c:84:a6:ac:6d:e4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
\S
Kernel \r on an \m
This is a system proected by US law

Last login: Wed Dec 30 19:31:32 2015 from 192.168.10.9
[ashok@new-host-4 ~]$ ^C
[ashok@new-host-4 ~]$ logout
Connection to localhost closed.
[ashok@new-host-4 .ssh]$ ssh 192.168.10.9
\S
Kernel \r on an \m
This is a system proected by US law

Last login: Wed Dec 30 19:39:42 2015 from localhost
[ashok@new-host-4 ~]$ logout
Connection to 192.168.10.9 closed.
[ashok@new-host-4 .ssh]$

Sunday, December 13, 2015

#Exercise 1

def GetAnswerWithUnderscores(answer,letters_guessed):
    new_string=''
    for ans in answer:
        if ans in letters_guessed:
            new_string+=ans
        elif ans not in letters_guessed:
            new_string+='_'
    return new_string

answer = GetAnswerWithUnderscores('welcome', 'mel')
print('GetAnswerWithUnderscores #1: expected _el__me, got', answer)
answer = GetAnswerWithUnderscores('quick', 'rstlne')
print('GetAnswerWithUnderscores #2: expected _____, got', answer)

###Exercise 2

def GetWordSeparatedBySpaces(word):
    new_string=''
    for char in word:
        new_string+=char+' '
    return new_string[0:len(new_string)-1]  
answer = GetWordSeparatedBySpaces('plane')
print('GetWordSeparatedBySpaces #1: expected p l a n e, got', answer)
answer = GetWordSeparatedBySpaces('to')
#Don't worry about the hasattr function, the if statement, or what they
#do: it's not required for this Hangman assignment.
if hasattr(answer, 'strip') and answer.strip() == answer:
    text = 'PASS'
else:
    text = 'FAIL'
print('GetWordSeparatedBySpaces #2: expected no spaces at the beginning or end,', text)


###Exercise 3


def IsWinner(answer,letters_guessed):
    new_word=''
    for each_letter in answer:
       if each_letter in letters_guessed:
           new_word+=each_letter
       else:
            return False
    if new_word==answer:
        return True
    else:
        return False
    
answer = IsWinner('plane', 'ranedlp')
print('IsWinner #1: expected True, got', answer)
answer = IsWinner('plane', 'plan')
print('IsWinner #2: expected False, got', answer)


###Exercise 4:


def IsLegalGuess(current_guess,letters_guessed):
        if len(current_guess)!=1:
            print('The letters guessed must not be more than one character long')
            return False
        elif current_guess.isalpha()==False:
            print('Your guess must include only alphabet not numbers and symbols')
            return False
        elif current_guess in letters_guessed:
            print('You cannot repeat the character that you have previously guessed')
            return False
        else:
            return True



answer = IsLegalGuess('g', '')
print('IsLegalGuess #1: expected True, got', answer)
answer = IsLegalGuess('g', 'rstle')
print('IsLegalGuess #1: expected True, got', answer)
answer = IsLegalGuess('bb', 'cat')
print('IsLegalGuess #1: expected False, got', answer)
answer = IsLegalGuess('p', 'yzpr')
print('IsLegalGuess #1: expected False, got', answer)




#Exercise 5


def GetLegalGuess(letters_guessed):#abc
    new_guess=input('guess a letter')
    while IsLegalGuess(new_guess,letters_guessed)==False:
        new_guess=input('guess a letter')
    return new_guess
        

answer=GetLegalGuess('abc')

print('Your guess is',answer,'and it is legal')


###Exercise 6

def IsGuessRevealing(answer, legal_letter_guess):
    if legal_letter_guess in answer:
        return True
    else:
        return False

answer = IsGuessRevealing('welcome', 'c')
print('IsGuessRevealing #1: expected True, got', answer)
answer = IsGuessRevealing('quick', 'z')
print('IsGuessRevealing #1: expected False, got', answer)


# Part 2
#Exercise 2


def GetAllEnglishWords():
    file=open('hangman_english_words.txt')
    new_list=[]
    for each_word in file:
        no_space=each_word.strip()
        new_list.append(no_space)
        
    return new_list

    

answer = GetAllEnglishWords()
new_list = answer
if answer != None and 'voting' in answer:
    text = 'PASS'
else:
    text = 'FAIL'

print('GetAllEnglishWords #1: expected the word voting', text)

if answer != None and 'triage' not in answer:
    text = 'PASS'

else:
    text = 'FAIL'

print('GetAllEnglishWords #2: did not expect triage', text)

#Exercise 3



def GetRandomWord(words):
    import random
    my_index=random.randint(0,len(words)-1)
    any_word=words[my_index]
    return any_word
    

words = ['train', 'plane', 'automobile']

answer = GetRandomWord(words)


print('GetRandomWord #1: expected a random word,', answer in words)


#Exercise 4
def Play(answer):
    import random
    word=random.choice(new_list)
    
    
    word=word.lower()
    strikes=0
    revealed=list(['_']*len(word))
    guessed=[]
    print('You have ',strikes,' strikes.')
    print(' '.join(revealed))
    while strikes<5:
        if '_' not in revealed:
            print('You Won')
            return True
        new_guess=input('enter a letter:')
        if new_guess in guessed:
            print('You have already guessed',new_guess,'. guess again.')
        else:
            guessed.append(new_guess)
            if new_guess in word:
                print('Good guess! Continue.')
                for x in range(0,len(word)):
                    if (word[x])==new_guess:
                        revealed[x]=new_guess
                print(' '.join(revealed))
            else:
                strikes+=1
                print(new_guess,' is not in the word. You have ',strikes,' strike')
    print('You have lost')
    return False

Play(answer)




##Exercise 5

import random
rand_word = random.choice(new_list)

def StartupAndPlay (rand_word):
    
    ask_user = input('Would you like to play again ?')
    if ask_user != 'y':
        return 'Byebye'
    while ask_user == 'y':
        Play(rand_word)
        ask_user = input('Would you like to play again ?')
        if ask_user == 'n':
            break
        else:
            Play(any_word)

StartupAndPlay (rand_word)

#Execise 6
if __name__ == "__main__":
    StartupAndPlay(rand_word)



#Extra credit 1

def GetPlayRecord():
    play_record=[]
    file=open('hangman_play_record.txt','r')
    for each in file:
        for number in each.split(','):
            play_record.append(int(number))
    return play_record
ans = GetPlayRecord()
if ans != None and isinstance(ans[0], int) and isinstance(ans[1], int):
    text = 'PASS'
else:
    text = 'FAIL'
print('GetPlayRecord #1: expected int elements,', text)    

 #Exercise 1



def GetAnswerWithUnderscores(answer,letters_guessed):
    new_string=''
    for ans in answer:
        if ans in letters_guessed:
           new_string+=ans
        elif ans not in letters_guessed:
            new_string+='_'
    return new_string

answer = GetAnswerWithUnderscores('welcome', 'mel')
print('GetAnswerWithUnderscores #1: expected _el__me, got', answer)
answer = GetAnswerWithUnderscores('quick', 'rstlne')
print('GetAnswerWithUnderscores #2: expected _____, got', answer)

#Exercise 2

def GetWordSeparatedBySpaces(word):
    new_string=''
    for char in word:
        new_string+=char+' '
    return new_string[0:len(new_string)-1] 
answer = GetWordSeparatedBySpaces('plane')
print('GetWordSeparatedBySpaces #1: expected p l a n e, got', answer)
answer = GetWordSeparatedBySpaces('to')
#Don't worry about the hasattr function, the if statement, or what they
#do: it's not required for this Hangman assignment.
if hasattr(answer, 'strip') and answer.strip() == answer:
    text = 'PASS'
else:
    text = 'FAIL'
print('GetWordSeparatedBySpaces #2: expected no spaces at the beginning or end,', text)


#Exercise 3


def IsWinner(answer,letters_guessed):
    new_word=''
    for each_letter in answer:
       if each_letter in letters_guessed:
           new_word+=each_letter
       else:
            return False
    if new_word==answer:
        return True
    else:
        return False
   
answer = IsWinner('plane', 'ranedlp')
print('IsWinner #1: expected True, got', answer)
answer = IsWinner('plane', 'plan')
print('IsWinner #2: expected False, got', answer)


#Exercise 4:


def IsLegalGuess(current_guess,letters_guessed):
        if len(current_guess)!=1:
            print('The letters guessed must not be more than one character long')
            return False
        elif current_guess.isalpha()==False:
            print('Your guess must include only alphabet not numbers and symbols')
            return False
        elif current_guess in letters_guessed:
            print('You cannot repeat the character that you have previously guessed')
            return False
        else:
            return True



answer = IsLegalGuess('g', '')
print('IsLegalGuess #1: expected True, got', answer)
answer = IsLegalGuess('g', 'rstle')
print('IsLegalGuess #1: expected True, got', answer)
answer = IsLegalGuess('bb', 'cat')
print('IsLegalGuess #1: expected False, got', answer)
answer = IsLegalGuess('p', 'yzpr')
print('IsLegalGuess #1: expected False, got', answer)




#Exercise 5


def GetLegalGuess(letters_guessed):#abc
    new_guess=input('guess a letter')
    while IsLegalGuess(new_guess,letters_guessed)==False:
        new_guess=input('guess a letter')
    return new_guess
       

answer=GetLegalGuess('abc')

print('Your guess is',answer,'and it is legal')


#Exercise 6

def IsGuessRevealing(answer, legal_letter_guess):
    if legal_letter_guess in answer:
        return True
    else:
        return False

answer = IsGuessRevealing('welcome', 'c')
print('IsGuessRevealing #1: expected True, got', answer)
answer = IsGuessRevealing('quick', 'z')
print('IsGuessRevealing #1: expected False, got', answer)