Thursday, July 10, 2014

Road to RHCE - Day 2 & Day 3

Day 2

I had to skip the second day due to unavoidable reason. Got to know from batch mates that below topics were covered on day 2 -

init levels
editors (gedit, vi, vim)
vim commands
basic file management (ls, cp, cd)
absolute path and relative path


--End of Day 2--


Day 3

Users & Group Management

Every user has an id called user id (same goes for 'group id' for each group)

uid of root user -> 0
uid of system (default) users -> 1 to 499
uid of normal users -> 500 and above

Files and directories that gets created/changed upon every user creation:

/etc/passwd                           (contains profile info of the users)
/etc/shadow                           (contains password info of the users)
/home/${USER}                     (home directory of the user)
/var/spool/mail/${USER}         (email inbox of the user)

Few config files that controls the user creation:

/etc/default/useradd             (base config file)
/etc/skel                             (base structure for every user's home directory)
/etc/login.defs                    (advance config file that contains password expiry period, home directory creation etc)

Understanding /etc/passwd file content

Normally the contents of /etc/passwd file will look like below -

root:x:0:0:root:/root:/bin/bash
   1  |2|3|4|  5  |    6  |      7

1 -> username
2 -> password protection symbol
3 -> userid
4 -> groupid
5 -> description about the user
6 -> home directory of the user
7 -> shell used by the user


To create a user

useradd [username]


To add a description (comment) for already created user

usermod -c "[description]" [username]


To set password for a user

passwd [username]


To create a user with a different shell (other than the default /bin/bash)

useradd -s /sbin/nologin [username]
 
[! the user can not login if his shell is /sbin/nologin]


To assign a new home directory for an existing user and to move his current home content to new location

usermod -md [newhome] [username]


To lock the password of a user

passwd -l [username]


To delete a user along with all his data (home & mail directories)

userdel -r [username]


Groups

There are two type of groups - Primary and Secondary (or Supplementary)

Similar to the user id (uid), there is also an id for each group (gid) and the id range is same as that of uid's.

Files that get changed or created when a group is created

/etc/group                    (contain group info)
/etc/gshadow               (contain group password)


Understanding the contents of /etc/group file

Normally the contents of /etc/group file will look like below -

root:x:0:root,user1
  1   |2|3|     4

1 -> group name
2 -> password protection symbol
3 -> group id
4 -> group members (users) list


To add a group

groupadd [groupname]


To assign a password for group

gpasswd [groupname]


To list the groups in which the currently logged in user is part of

groups


To assign a new group for currently logged in user temporarily

newgrp [groupname]


To assign user to a secondary group permanently

usermod -G [groupname] [username]

[! this will remove the user from his previous group(s) and assign him to the new group]


To assign a user to another secondary group permanently without removing his previous subscriptions

usermod -aG [groupname] [username]


To change the primary group of a user permanently

usermod -g [groupname] [username]


To change the group id of an existing group

groupmod -g [new id] [groupname]


To delete a group

groupdel [groupname]


Permissions
A normal ls -l command will output like below -

drwxr-xr-x. 2 root root  4096 Jul  1 21:59 Documents

In the above output, the first 11 characters define the permissions for the corresponding file.

d  r  w  x  r   -   x  r  -    x    .
1  2  3  4  5  6  7  8  9  10  11

1 -> File type
        -   -> ASCII file
        d  -> directory
        c  -> character device
        b  -> block device
        l   -> symbolic link
        s  -> socket

2,3,4  -> Owner's permission
5,6,7  -> Group owner's permission
8,9,10 -> Other's permission
11  -> selinux context


To change the owner of a directory (or file)

chown [username] [directory/file]


To change the group owner of a directory (or file)

chgrp [groupname] [directory/file]


There are two ways in which the permissions can be set for directories/files - one is Symbolic method and the other one is Numeric method.


Symbolic method glossary

Who
u  -> owner
g  -> group owner
o  -> others
a  -> all



What
r  -> read
w -> write
x  -> execute/accessible
-  -> null

Action
+ -> append/add
-  -> revoke/remove
= -> assign


Example:

chmod o+w [directory/file]

the above command will append write permission to others for the [directory/file]


Numeric method glossary

4  -> read
2  -> write
1  -> execute
0  -> null

Example:

chmod 755 [directory/file]

the above command will set the permissions for [directory/file] as rwxrw-rw-


UMASK

UMASK defines the default permissions that has to be set for each file/directory as soon as it is created by a user.

The default permission for directories        -> 777
The default permission for files                   -> 666
Predefined UMASK value for root user       -> 022
Predefined UMASK value for normal user  -> 002

Which means, if a root user creates a directory, that directory's permission is set to 777-022 = 755. And if the root user creates a file, that file's permission is set to 666-022 = 644.

Similarly, if a normal user creates a directory, that directory's permission is set to 777-002 = 775. And if the normal user created a file, that file's permission is set to 666-002 = 664.

UMASK value for all users is defined in /etc/profile


To change the UMASK value for currently logged in user temporarily

umask [new_value]



Tidbits of the day
  • id command is used to get the id of
  • id command is used to get the id of currently logged in user
  • RedHat uses sha512 algorithm to encrypt the user password. The algorithm is configured in /etc/login.defs

--End of Day 3--

Wednesday, July 9, 2014

Road to RHCE - Day 1

The thought of getting myself RHCE certified started some five to six years back. That remained a thought for a long time before I finally made my mind to make it into a reality.

Picked up a training center at T.Nagar and got myself enrolled on May 1st 2014. It took about one and a half month for the center to form the batch for the timing that I opted for and the classes started on June 18th 2014.

My intention of this blog is just to keep a digital copy of whatever notes that I take in the class. I am planning to blog the topics that are covered everyday.

A big thanks to my friend Shrinivasan, who motivated me to start this series & eventually to return back to blogging.

This might also help someone who is looking for quick help in linux or it might also give a good idea for students about the topics covered in RHCE. Anyway with some hope that my blog would help myself/others as a quick reference, let me get this rolling...


Day 1

First day was slow, as normally it would be for any training course for that matter. Our faculty gave us ample time (really 'a lot of time') to get settled in the classroom and after the formal introduction, he threw an unexpected bomb to us.

"Since this is the first day, you may start now if you wish. Or if you want me to take class, I can do that as well."

We were like "what, are you kidding???"

We finally convinced our faculty to start with the class and so after the regular features of linux stuff, our day 1 started with the default filesystem hierarchy of linux. Here on, I am switching to a text book style to minimize my blabbering and just to focus on the subject rather.

Disclaimer: The information shared in this blog from this point on are direct from the classroom and I am blindly replicating those here. Constructive feedback is always welcome if any inappropriate or misleading information is found.


Linux Filesystem Hierarchy

Below are the default directories that get created when you install RedHat linux operating system -

/              - top level directory
/root       - root user's home directory
/home     - placeholder for normal user(s)'s home directories
/bin         - contains the commands that can be executed by normal users
/sbin       - contains the commands that can be executed by root user
/boot      - boot loader info
/etc         - system config files
/dev        - device(s) info
/usr        - documentation files
/var        - log files
/opt        - 3rd party app data
/proc      - process & system info
/sys        - process & system info
/mnt       - placeholder to mount external media
/media   - placeholder to mount external media
/lib         - library files and modules

Terminal

The default shell that will be used during the training is bash (bourne again shell)

Terminal is the command prompt window which we can invoke in 3 ways from the desktop -

1. Press Alt+F2 -> type gnome-terminal
2. Click Applications -> System Tools -> Terminal
3. Right-click on the desktop -> Open in Terminal

To zoom in when inside the terminal - press Ctrl+Shift+"+"
To zoom out when inside the terminal - press Ctrl+"-"
To open a new tab in the terminal - press Ctrl+Shit+T
To switch between open tabs in the terminal - press Alt+

Tidbits of the day

  • whoami command is used to know the user as whom we logged in currently
  • date command is used to view/set the system date/time
  • hwclock command is used to view the CMOS date/time
  
--End of Day 1--

Wednesday, October 12, 2011

MySQL - Quick Reference

Came across this very useful link on MySQL commands & thought of sharing here. Thanks to the original poster @ http://www.pantz.org/software/mysql/mysqlcommands.html

To login (from unix shell) use -h only if needed.

# [mysql dir]/bin/mysql -h hostname -u root -p

Create a database on the sql server.

mysql> create database [databasename];

List all databases on the sql server.

mysql> show databases;

Switch to a database.

mysql> use [db name];

To see all the tables in the db.

mysql> show tables;

To see database's field formats.

mysql> describe [table name];

To delete a db.

mysql> drop database [database name];

To delete a table.

mysql> drop table [table name];

Show all data in a table.

mysql> SELECT * FROM [table name];

Returns the columns and column information pertaining to the designated table.

mysql> show columns from [table name];

Show certain selected rows with the value "whatever".

mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";

Show all records containing the name "Bob" AND the phone number '3444444'.

mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';

Show all records not containing the name "Bob" AND the phone number '3444444' order by the phone_number field.

mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;

Show all records starting with the letters 'bob' AND the phone number '3444444'.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';

Show all records starting with the letters 'bob' AND the phone number '3444444' limit to records 1 through 5.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;

Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.

mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";

Show unique records.

mysql> SELECT DISTINCT [column name] FROM [table name];

Show selected records sorted in an ascending (asc) or descending (desc).

mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

Return number of rows.

mysql> SELECT COUNT(*) FROM [table name];

Sum column.

mysql> SELECT SUM(*) FROM [table name];

Join tables on common columns.

mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;

Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;

Change a users password from unix shell.

# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'

Change a users password from MySQL prompt. Login as root. Set the password. Update privs.

# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
mysql> flush privileges;

Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.

# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start

Set a root password if there is on root password.

# mysqladmin -u root password newpassword

Update a root password.

# mysqladmin -u root -p oldpassword newpassword

Allow the user "bob" to connect to the server from localhost using the password "passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by 'passwd';
mysql> flush privileges;

Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;

or

mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;

To update info already in a table.

mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';

Delete a row(s) from a table.

mysql> DELETE from [table name] where [field name] = 'whatever';

Update database permissions/privilages.

mysql> flush privileges;

Delete a column.

mysql> alter table [table name] drop column [column name];

Add a new column to db.

mysql> alter table [table name] add column [new column name] varchar (20);

Change column name.

mysql> alter table [table name] change [old column name] [new column name] varchar (50);

Make a unique column so you get no dupes.

mysql> alter table [table name] add unique ([column name]);

Make a column bigger.

mysql> alter table [table name] modify [column name] VARCHAR(3);

Delete unique from table.

mysql> alter table [table name] drop index [colmn name];

Load a CSV file into a table.

mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);

Dump all databases for backup. Backup file is sql commands to recreate all db's.

# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql

Dump one database for backup.

# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql

Dump a table from a database.

# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

Restore database (or database table) from backup.

# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql

Create Table Example 1.

mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));

Create Table Example 2.

mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');

Saturday, July 16, 2011

How to download multiple songs (files) from a site via wget

This was my long time requirement, ie, whenever I wanted to download songs from any site. I finally found the solution for the same - Thanks to Google.

There is no direct way to do this however, a workaround does exist.

Step1: Using lynx extract all the outgoing links from the html page where the songs are listed.

#lynx -dump "http://www.example.com" | grep -o "http:.*" > file.txt

or if the songs are listed in the same directory and you can identify the path of the directory by hovering over the song links, below would help a lot -
#lynx -dump "http://www.example.com" | grep -o "http://parent/directory/of/song/files/.*" > file.txt

Step2: Cleanup the URLs in file.txt such that it contains only the song URLs

Step3: Use wget, TADA... there you go :-)
#wget -i file.txt


-Jeeva

Tuesday, June 1, 2010

Series60-Remote - Manage your Symbian mobile in Ubuntu

Goddddddddddd! Had I known this by afternoon, I'd have saved half a day.

Finally, I got a tool with which I can manage the messages, contacts & files in my N95 with Ubuntu.

This tool is simply awesome and available for Debian, Fedora, Windows & MacOS. It's based on Python and needs a client to be installed in your mobile. You need to install PyS60 as well on your mobile. I used PythonForS60_1_4_5_3rdEd.sis for my N95 8GB since that was the latest available release. It uses sqlite or MySql in the backend for storing messages/contacts etc.

Apart from these, I had to install obexftp & python-obexftp [both available in Synaptic] as well.

Once all the required software are installed in your PC & in your mobile, you should initiate a bluetooth pairing between PC and Mobile and then open S60-remote in your mobile. I guess only bluetooth connectivity works as of now and you can not use USB mode yet. The client needs to be running all the time so you can not close it in your mobile but minimize it by pressing Menu key.

Then, open up Applications -> Internet -> Series60-Remote in your Ubuntu.

The application will list all available bluetooth devices.

Select your mobile from the list and click Next.

Application will ask if you want to push the client and PyS60 to your mobile if not done already. Ignore that and click next since we have them installed already.

After couple of screenshots explaining how to use the mobile client, application will connect to the phone and Ta-Da!

You'll see the phone is connected to the PC and it shows the battery and signal level of your phone.

From the left panel you can create a new message, manage contacts & files, see device information etc.

To see existing messages, you need to select 'View history' from Messages menu.

Once a new message come in to your mobile, the Series60-Remote in your Ubuntu will show a popup, just like a gtalk or skype chat popup, and ask if you want to continue as chat.

The app seems pretty neat, simple, fast and does a wonderful job. Unfortunately, this is available only for Symbian60 mobiles though! But still, great work by the developer.

So, those who have Symbian & using Ubuntu/Fedora/Mac or those who are not happy with the super slow message management in Ovi Suite, this app is exactly for you guys - GO FOR IT.


-Jeeva