Showing posts with label LINUX. Show all posts
Showing posts with label LINUX. Show all posts

Thursday, November 13, 2014

LINUX BASIC COMMON COMMANDS

Filter result:


grep

ex: filter all processes and ports of java

ps -e | grep java
netstat -tunap | grep java

Process and infomation :


Information about process
$ls -l /proc/$PID/exe
OR
$ps -p PID -o comm=
OR
$cat /proc/PID/cmdline

Kill process
sudo kill -9 PID

User and Group:


Login
#su - {user-name}

Login as root
#su -

check username
#whoami

logout
#logout

Extract & Compress file:


Extract
#tar -zxvf {file.tar.gz}
#tar -xf {file.tar.gz} -C /target/directory
#tar -xzf backup.tar.gz -C /target/directory
#unzip file.zip -d destination_folder

Download
#wget http://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gz  -P /target/directory

Restating the network service:


To restart the network service under RHEL / CentOS based systems, enter:
service network restart
OR
/etc/init.d/network restart

Check linux kernel version number :


Open a shell prompt (or a terminal) and type the following command to see your current Linux kernel version:

uname -r

Or type the following command
uname -mrs

To print all information, enter

uname -a

Type the following command to see Linux version info

cat /proc/version

Find Distribution Version :


Type the following command

cat /etc/*release

OR
lsb_release -a

Port & Firewall :


Port and process command
netstat -tunap

If you want to open a single port

open iptables file
vi /etc/sysconfig/iptables

Add below line to open port.

-A INPUT -m state --state NEW -m tcp -p tcp --dport 143 -j ACCEPT

For multiple, you can use the following instead (or repeat the above line multiple times):

-A INPUT -m state --state NEW -m tcp -p tcp -m multiport --dports 22,80,143 -j ACCEPT

service iptables save

Disable / Turn off Linux Firewall (Red hat/CentOS/Fedora Core)

Type the following two commands (you must login as the root user)

/etc/init.d/iptables save

/etc/init.d/iptables stop

Turn off firewall on boot

chkconfig iptables off

Enable / Turn on Linux Firewall (Red hat/CentOS/Fedora Core)

Type the following command to turn on iptables firewall

/etc/init.d/iptables start 

Turn on firewall on boot

chkconfig iptables on 

Linux / Unix - Checking Free Disk Space:


df command examples - to check free disk space

Type df -h or df -k to list free disk space

df -h

OR
df -k

Find Files and Folders in Linux :


find / -name "java" 

/: meaning is the root folder. hold systems
-name "java": all files have name is java.

Thursday, November 6, 2014

NODEJS - RUNNING A NODEJS SERVER AS A SERVICE ON LINUX


1. Download the project for testing:


The user logged in was: root

Download the demo server here

[root@homepc opt]# wget https://github.com/vanduc1102/nodejs-example/archive/daemon-bauth.tar.gz

[root@homepc opt]# tar -zxvf daemon-bauth.tar.gz

[root@homepc opt]# cd nodejs-example-daemon-bauth/

[root@homepc nodejs-example-daemon-bauth]# npm install

[root@hompec nodejs-example-daemon-bauth]# node index.js


You can open browser and test the application at localhost:3000

We had nodejs-example folder placed in /opt/nodejs-example-daemon-bauth

2. Create a service file in systemd:


Create file below in folder : /etc/systemd/system/
nodejs-example-service.service

[Unit]
Description=The nodejs-example server start

[Service]
WorkingDirectory=/opt/nodejs-example-daemon-bauth
ExecStart=/usr/bin/node index.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-example-nodejs-server
User=root
Group=root

[Install]
WantedBy=multi-user.target

Load the service at first time:
#systemctl enable nodejs-example-service

Start service:
#systemctl start nodejs-example-service

View status of the service:
#systemctl status nodejs-example-service

Stop the service:
#systemctl stop nodejs-example-service

3. Testing the service:


[root@homepc system]# systemctl start  nodejs-example-service.service
[root@homepc system]# systemctl status nodejs-example-service.service
nodejs-example-service.service - The nodejs-example server start
Loaded: loaded (/etc/systemd/system/nodejs-example-service.service; enabled)
Active: active (running) since jeu 2014-11-06 19:18:02 ICT; 8s ago
Main PID: 3967 (node)
CGroup: name=systemd:/system/nodejs-example-service.service
└─3967 /usr/bin/node index.js
[root@homepc system]# ps -e | grep "node"
3967 ? 00:00:00 node
[root@homepc system]# kill 3967
[root@homepc system]# ps -e | grep "node"
4038 ? 00:00:00 node
[root@homepc system]# systemctl status nodejs-example-service.service
nodejs-example-service.service - The nodejs-example server start
Loaded: loaded (/etc/systemd/system/nodejs-example-service.service; enabled)
Active: active (running) since jeu 2014-11-06 19:18:02 ICT; 8s ago
Main PID: 4038 (node)
CGroup: name=systemd:/system/nodejs-example-service.service
└─4038 /usr/bin/node index.js
[root@homepc system]#

As you can see, After we kill the process, It will automatically create new one.

Saturday, October 18, 2014

Restore grub2 after install windows

The problem is after you install linux and then you install a windows 

For example after install Ubuntu 9.10 and then I install Windows operating system along with Ubuntu. The windows will override Master Boot Record on Hard driver , so from now when your computer boot up, you only can see Windows.

To solve the problem:

We need to have a CD version of Ubuntu 9.10. Boot our PC with live mode of Ubuntu 9.10
.Open Terminal ( Applications  > Accessories > Terminal)

(We suppose that the Ubuntu already installed on  partition sda3 of sda)

sudo -i (change user to  root user).

mount /dev/sda3 /mnt

grub-install --root-directory=/mnt/ /dev/sda

mount --bind /proc /mnt/proc

mount --bind /dev /mnt/dev

mount --bind /sys /mnt/sys

chroot /mnt update-grub

umount /mnt/sys

umount /mnt/dev

umount /mnt/proc

exit

Reboot your computer and enjoy !

Setting Global Environment Variables in LINUX

The easiest way to set an environment variable in CentOS is to use export as in :

vanduc@VGN-FZ290E:~/Downloads$export JAVA_HOME=/usr/lib/java/jdk1.7.0_71

vanduc@VGN-FZ290E:~/Downloads$export PATH=$PATH:$JAVA_HOME

However, variables set in such a manner are transient i.e. they will disappear the moment you exit the shell. Obviously this is not helpful when setting environment variables that need to persist even when the system reboots.

In such cases, you need to set the variables within the system wide profile

In CentOS (I’m using v5.2), the folder /etc/profile.d/ is the recommended place to add customizations to the system profile.

For example, when installing the Sun JDK, you might need to set the JAVA_HOME and JRE_HOME environment variables. In this case:
Create a new file called java.sh

vanduc@VGN-FZ290E:~/Downloads$ sudo vi /etc/profile.d/java.sh

Within this file, initialize the necessary environment variables

export JAVA_HOME=/usr/lib/java/jdk1.7.0_71

export JRE_HOME=$JAVA_HOME/jre

export PATH=$PATH:$JRE_HOME/bin

export PATH=$PATH:$JAVA_HOME/bin


Now when you restart your machine, the environment variables within java.sh will be automatically initialized (checkout /etc/profile if you are curious how the files in /etc/profile.d/ are loaded) .

If you want to load the environment variables within java.sh without having to restart the machine, you can use the source command as in:

vanduc@VGN-FZ290E:~/Downloads$ source /etc/profile.d/java.sh


Test command:

vanduc@VGN-FZ290E:~/Downloads$ java -version

java version "1.7.0_71"

Java(TM) SE Runtime Environment (build 1.7.0_71-b14)

Java HotSpot(TM) Server VM (build 24.71-b01, mixed mode)

vanduc@VGN-FZ290E:~/Downloads$

P/S : some common commands use to setup java environment

vanduc@VGN-FZ290E:~/Downloads$ cd Downloads/
vanduc@VGN-FZ290E:~/Downloads$ ls
google-chrome-stable_current_i386.deb jdk-7u71-linux-i586.tar.gz sublime-text_build-3065_i386.deb
vanduc@VGN-FZ290E:~/Downloads$ tar -zxvf jdk-7u71-linux-i586.tar.gz
vanduc@VGN-FZ290E:~/Downloads$ sudo mkdir /usr/lib/java
vanduc@VGN-FZ290E:~/Downloads$ mv jdk1.7.0_71/ /usr/lib/java/


Sunday, July 7, 2013

Make your eclipse look better on ubuntu



When using eclipse on ubuntu, extra space between items and fonts size make it look awkward. I try to adjust fonts by go to [Window->Preferences->General->Appearance->Colors and Fonts]. This will not much help, reduce fonts size does not change space between item. Here is Eclipse Europa before tweak.
Anyway, at last I found the way to make eclipse look more compact from googling. Thank forWesley and Vladimir Buell for this trick. Create ".gtkrc-2.0" in your home directory enable you to customize gtk object style[more about gtkrc-2.0]. Here is my ".gtkrc-2.0".
pnix@pnix-a7n:~$ cat .gtkrc-2.0
style "gtkcompact" {
font_name="Sans 8"
GtkButton::default_border={0,0,0,0}
GtkButton::default_outside_border={0,0,0,0}
GtkButtonBox::child_min_width=0
GtkButtonBox::child_min_heigth=0
GtkButtonBox::child_internal_pad_x=0
GtkButtonBox::child_internal_pad_y=0
GtkMenu::vertical-padding=1
GtkMenuBar::internal_padding=0
GtkMenuItem::horizontal_padding=4
GtkToolbar::internal-padding=0
GtkToolbar::space-size=0
GtkOptionMenu::indicator_size=0
GtkOptionMenu::indicator_spacing=0
GtkPaned::handle_size=4
GtkRange::trough_border=0
GtkRange::stepper_spacing=0
GtkScale::value_spacing=0
GtkScrolledWindow::scrollbar_spacing=0
GtkExpander::expander_size=10
GtkExpander::expander_spacing=0
GtkTreeView::vertical-separator=0
GtkTreeView::horizontal-separator=0
GtkTreeView::expander-size=8
GtkTreeView::fixed-height-mode=TRUE
GtkWidget::focus_padding=0
}
class "GtkWidget" style "gtkcompact"

style "gtkcompactextra" {
xthickness=0
ythickness=0
}
class "GtkButton" style "gtkcompactextra"
class "GtkToolbar" style "gtkcompactextra"
class "GtkPaned" style "gtkcompactextra"
pnix@pnix-a7n:~$

and here is the result. No more extra space, I also change fonts for text editor and Dialog to "DejaVu San size 8".

Source : http://lj4newbies.blogspot.com/2008/02/make-your-eclipse-look-better-on-ubuntu.html

Tuesday, January 15, 2013

Sửa lỗi chạy MYSQL trên CentOS

Error occure when using command  :

# mysql  -u root -p

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)



Are you sure you installed mysql as well as mysql server.. ?
For example to install mySql server I'll use yum or apt to install both mysql command line tool and the server:
#yum -y install mysql mysql-server
or #apt-get install mysql mysql-server
Enable the MySQL service:
#/sbin/chkconfig mysqld on
Start the MySQL server:
#/sbin/service mysqld start
afterwards set the MySQL root password:
#mysqladmin -u root password 'new-password' (with the quotes)

Try :#mysql -u root -p
 
hope this help.
References : stackoverflow 

Xác đinh chương trình nào đang sử dụng port nào

// dùng su để lấy quyển root
#su 
// dùng lệnh netstat -tulpn để liệt kê các port đang bị chương trình nào sử dụng.
#netstat -tulpn


Tuesday, December 18, 2012

Cài đặt XAMMP trên LINUX với NOMAL USER

Bước 1 : Sau khi cài đặt chúng ta chuyển đến folder chứa LAMMP :

#cd /opt/lampp/

Liệt kê danh sách file trong thư mục gốc của LAMMP :


Nhiệm vụ được đặt ra là đổi nobody thành tên username của chúng ta :

Bước 2 : Cần phải xác định được username mà chúng ta đang sử dụng là gì :

#whoami

Xác định user-name đang sử dụng thuộc group nào :

#goups



như hình trên ta có user-name : vanduc1102 thuộc về 2 group là rootdb2iadm1

Bước 3: Cấp quyền sử dụng cho thư mục  htdocs


Bước 4 :

mở file httpd.conf trong /opt/lammp/etc/


# gedit /opt/lampp/etc/httpd.conf 



xem đoạn

User nobody

Group nogroup

</IfModule>

</IfModule>


Và đổi thành

User vanduc1102

Group nogroup

</IfModule>

</IfModule>




Khởi động lại lammp là ok .
#/opt/lammp/lammp start

Liệt kê các port đang được mở và mở port

Chúng ta sử dụng lệnh sau :

netstat -tunap | grep LISTEN

Ví dụ :


Hoặc :


Scan all port Openning in centos using :

nmap 127.0.0.1





If you want to open a single port:

open iptables file : vi /etc/sysconfig/iptables

Add below line to open port.

-A INPUT -m state --state NEW -m tcp -p tcp --dport 143 -j ACCEPT

For multiple, you can use the following instead (or repeat the above line multiple times):

-A INPUT -m state --state NEW -m tcp -p tcp -m multiport --dports 22,80,143 -j ACCEPT

The reason your line doesn't work is likely because you don't have a chain named 'RH-Firewall-1-INPUT'.

CentOS 6 simply uses 'INPUT' as the chain name.

You'll note something like the following at the top of the default config, naming the chains that exist:

:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]

Just to explain a bit further, the line -A INPUT -m state --state NEW -m tcp -p tcp --dport 143 -j ACCEPT means:

-A: append a rule
INPUT: to the INPUT chain
-m state: use the 'state' module
--state NEW: only look for NEW connections (i.e. not those that are previously established/related)
-m tcp: use the tcp module
-p tcp: look for packets using the TCP protocol
--dport 143: look for packets with a destination port of 143


To save  :
#/sbin/service iptables save
#/etc/init.d/iptables restart

Sunday, September 23, 2012

Best Torrent On Linux.


There is my opinions :this wrote by java.

This is VUZE :

here is link of VUZE : http://www.vuze.com/

just download Linux version , look like : Vuze_Installer.tar.bz2

uncompress it .and you got.



Remember . It is java program. So run it . You must installed java run time. JRE.
Hope this help.

Install Transmission on CentOS


I'm here to talk about installation Transmission on CentOS.
Install the Necessary Packages
Building Transmission requires the following packages, so you will need to download it from the yum repository.
  • gcc
  • cURL
  • libevent
  • intltool
  • gettext
 yum install gcc curl curl-devel libevent libevent-devel intltool   gettext openssl-devel
Download and Extract Transmission :

The latest version of Transmission is here :


but my tutorial only work on version 2.03. hehe.

So, you can download it using wget.

wget http://mirrors.m0k.org/transmission/files/transmission-2.03.tar.bz2
Extract the package now:
tar -xvf transmission-2.03.tar.bz2
Installing Transmission
To begin the installation, go inside the Transmission folder:

cd transmission-2.03
Here’s the fun part. You’re going to run the configure script and make! Afterwards, you’ll just run make install and you’ll be done!
./configure -q && make -s
make install
Conclusion
Congratulations! You have successfully installed Transmission! Now, run the following command line to view Transmission’s help guide:
transmissioncli -h
Here is my transmission :

It's is GUI :
Hope this help.