Thursday, April 8, 2010

A bunch o f How-to's


View Specs of you linux pc

If you want to view the hardware spec of your pc, you can use this command
--> lshw
By default, this command is installed on ubuntu linux but not installed on Centos. You can use you tools like yum to install it
--> yum install lshw

Mounting cdrom

If you are using a pc with linux that does not have automount feature, here are the steps that you can do to mount it on linux terminal.

* After inserting the cd, create a folder. Usually we create a folder inside folder /mnt. Type --> mkdir /mnt/cdrom.
* To mount it, type --> mount /dev/cdrom /mnt/cdrom. This step will mount the content of the cdrom to the folder /mnt/cdrom
* After you finish, just type --> umount /mnt/cdrom to unmount it from the folder.

You can also use this steps for other devices such as flash drive. To check where is the folder containing the device, type --> df.

how to check your kernel version


Sometimes you want to know the particulars about the kernel of your linux. This can be done using uname command.
To check kernel version:
->uname -v
To check kernel release:
->uname -r
To check kernel name:
->uname -s

Using crontab scheduler


Crontab is a tool for scheduling tasks for the computer to run. It will automate the jobs that needs to be done but will be difficult to be done by human because of time limitation. There are only 4 options for crontab which are -u, -e, -l and -r and the usage is described below:

* crontab -l - list all the crontab on standard output
* crontab -e - edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables
* crontab -r - remove the current crontab
* crontab -u - specify the user
o example: $ crontab -u username -l - this command will list the crontab belongs to the username

Crontab is set by running the crontab -e command and add the necessary time and the command to be run on that particular time. Crontab scheduling format are as below:

* * * * * command to be run

1st * is for minutes (0-59)
2nd * is for hour (0-23)
3rd * is for day of month (1-31)
4th * is for month of year (1-12)
5th * is for day of the week (0-7) where 0=Sunday and 7=Sunday

Example:
To backup your pc using rsnapshot everyday at 7.30 a.m.;

1. $ crontab -e
2. 30 7 * * * /usr/local/bin/rsnapshot daily
3. save your crontab setting

SSH without password

Using the below steps, you can ssh to the server from client without the entering any password.
The machine which run the ssh command is the client
The machine that the client access using ssh is the server

1. Run the following command on the client
* -> ssh-keygen -t dsa
2. File id_dsa and id_dsa.pub will be created inside $HOME/.ssh
3. Copy id_dsa.pub to the server's .ssh directory
* -> scp $HOME/.ssh/id_dsa.pub user@server:/home/user/.ssh
4. Change to /root/.ssh and create file authorized_keys containing id_dsa content
* -> cd /home/user/.ssh
* -> cat id_dsa >> authorized_keys
5. Change "StrictModes yes" in /etc/ssh/sshd_config to "StrictModes no"
6. Restart ssh server
7. You can try ssh to the server from the client and no password will be needed
* -> ssh user@server

Another alternative to the above steps is to use ssh-copy-id command. The steps are:

1. Run the following command on the client
* -> ssh-keygen -t dsa
2. File id_dsa and id_dsa.pub will be created inside $HOME/.ssh
3. Copy the id_dsa.pub to the server's .ssh directory
* -> ssh-copy-id -i ~/.ssh/id_dsa.pub user@server
4. Change "StrictModes yes" in /etc/ssh/sshd_config to "StrictModes no"
5. Restart ssh server
6. You can try ssh to the server from the client and no password will be needed
* -> ssh user@server

Resetting mysql root password

Sometimes you have to access a mysql database and you somehow does not know the password or the user of the database. The last resort is you have to use the root password but the password is unknown. There is always the way to solve anything and the way to reset the root password is described below. Enjoy!

1. Open a terminal and stop mysqld daemon if it has been started: $ /etc/init.d/mysqld stop
2. Type this command: $ mysqld_safe --skip-grant-tables
3. Open a new terminal, and access mysql database using root:
$ mysql -u root mysql
4. Change root password to new password: mysql> update user set password=password('newpassword') where user='root';
5. flush mysql privileges: mysql> flush privileges;
6. Exit mysql: mysql> exit
7. Close the terminal with the mysqld_safe command
8. Restart mysql: $ /etc/init.d/mysqld start

Backup using tar and ssh

Doing backup is important. You do not want to store your backup at the same machine where the data is stored. It is to avoid data loss when the machine broke down. To do this job, you can use tar paired with ssh to archive your important data and transfer it through network to another machine. Below are the steps:

1. Make sure the backup machine is installed with ssh server and the service is running
# yum install openssh-server
# service sshd start

2. Go to the folder where you want to backup. Use tar to archive and send it though network to backup machine
# tar -cvjf - /path/to/backup | ssh user@backupmachine "cat > /home/backup.tar.bz2"

3. Finish. Congatulations, your backup file is now safely kept in the backup machine


Setting up samba with password protection


To easily share your files to linux and windows clients, samba is still the preferred choice. In this guide I will show how to setup a samba server on centos 5 machine, that can be accessed only by certain people protected by password.

1. Install samba on the server
* # yum install samba
2. Create the group that all the samba users will be contained in, for example 'samba'
* # groupadd samba
3. Create samba users and add it to the above group, which is in this example is 'samba'. Below is the example to create a user named 'user1' and add it to group 'samba'. Set the password for user1
* # useradd user1 -g samba
* # passwd user1
4. Create the directory to be shared. In this example, i will use /home/shared. Change the ownership to root and group ownership to the 'samba' group. Change permission so that only user and group can read write and execute
* # mkdir /home/shared
* # chown -R root.samba /home/shared
* # chmod -R 775 /home/shared
5. Below is a simple setting of samba
* [global] workgroup = samba
server string = Samba Server
security = user [shared_folder]
comment = Sharing place
path = /home/shared
public = no
writable = yes
printable = no
write list = @samba
create mask = 0755
force create mode = 0755
directory mask = 0775
force directory mode = 0775
* What the above setting does basically is to setup /home/shared as samba shared directory but can only be accessed by user from group samba
6. Add user/users to samba
* # smbpasswd -a user1
7. Start smb service, restart if it has already been started
* # /etc/init.d/smb start
8. 'user1' can now access the samba server using address 'smb://samba_server_ip_address/shared_folder' at any nautilus address bar. For windows client, you can see at your 'My Network Places' and find a workgroup named 'samba'

Ubuntu forgotten password


What to do when you forgot your password for your ubuntu machine?? Here are some simple steps on how to change the password using single user mode.

1. Reboot the machine
2. When grub is loading, press 'Esc'
3. Choose 'Ubuntu kernel...........(recovery mode)'
* Press 'e' to edit the kernel parameter
* Append 'single init=/bin/bash' to the kernel parameter
4. Press 'b' to boot from that particular kernel
5. You will enter single user mode
6. Your hard drive will be in read-only mode. Remount it in read-write mode
* # mount -o remount,rw /dev/sda1
7. Change your passwd
* # passwd user
8. Reboot your machine
9. Access your machine using your new password

Congratulations, you just changed you user password using single user mode

CentOS/Fedora forgotten password

What to do if you forgot the password for your CentOS/Fedora/Redhat machine?? Here are some simple steps to change back the password by entering into single user mode of your machine provided you do not forgotten your grub password if you have set it ;)

1. Reboot your machine
2. Press 'Esc' key once grub starts loading
3. Select your kernel press 'e' on one of the kernel to edit the kernel parameter
4. Press 'e' on the line that starts with 'kernel /vmlinuz...'
5. Append ''single" or "1" at the end of the line
6. Press 'Enter'
7. Press 'b' to boot from the appended kernel
8. You are now in the single user mode of your linux machine once you get to the shell. You can now change the password of your account using command passwd
9. Reboot back your machine normally
10. You can now log in to your machine using your new password

Suse forgotten root password

In suse, if you forgot your root password, you can go to the single mode as usual. Steps are explained below:

1. Reboot the machine
2. When Startup Options(blue background in SLES) appear, choose Failsafe mode
3. In the Boot Options, append init=/bin/sh at the end of the line
4. Press Enter to boot
5. You will be presented with a shell. Change your password using command passwd root
6. Reboot the machine. root is now can be accessed using your new password

Restore GRUB

GRUB is a boot loader commonly used with linux operating system. It can be used to managed dual boot environment where linux and windows can coexist easily in a same machine without problem provided you install the windows OS first so that when you install linux, GRUB will overwrite Windows boot loader and automatically detect and manage both operating system the next time you boot your computer. Problems will happen if you alter your partitions outside the knowledge of GRUB, for example, you create new partition in your hard drive using windows. This will cause GRUB to automatically go into GRUB shell when boot. To restore back your GRUB is very simple, just follow easy steps below:

1. find in which partition does GRUB store its configuration file, which is your /boot partition. (hd0,2) means third partition of the first hard drive
grub> find /boot/grub/stage1
(hd0,2)

2. set the root for GRUB to be (hd0,2)
grub> root (hd0,2)

3. write GRUB to the Master boot record(MBR) of your hard drive. Change (hd0) to (hd0,2) to write GRUB to your /boot partition instead
grub> setup (hd0)

4. Reboot machine
grub> reboot

All those steps can also be used using livecd, if let say the grub shell did not come out but you cannot boot your machine or you cannot boot your linux due to messed up GRUB. just boot from livecd, open a terminal, and type "grub" as a superuser to go to GRUB shell

Logging your terminal activity

When typing on the terminal, sometimes we need to record what commands we have typed for later reference. Sure, we can simply copy the .bash_history file, but that file only shows what you have typed and not the result of the commands that you have typed. To do these kind of jobs, there are two applications that you can use:


1. script

To use script, simply run script before you start using your terminal

# script -f logfile.log

where -f is to flush output after each write, and logfile.log is the file to write whatever script has recorded.

After finish using script, simply type exit or logout to quit script


2. rootsh

Please install rootsh first if it is not installed. To use rootsh, run rootsh before start using your terminal similar to script

# rootsh -f logfile.log --no-syslog

where -f is to show which file will be used to record the session, in this case logfile.log and --no-syslog is to tell rootsh not to log to /var/log/messages. To quit from rootsh, type exit or logout. All the commands and output will be written to logfile.log.closed to show that rootsh has closed the session.

To view the output file of rootsh and script, more command can be used.

1 comments:

Anonymous said...

Nice dispatch and this post helped me alot in my college assignement. Thank you as your information.