[Linux] Basic Linux command - Continuous update

hostname: hiển thị tên máy
netconf | hostname: thay đổi tên máy
whoami: hiển thị tên người dùng hiện tại
id: hiển thị id của các nhóm và người dùng
date: hiển thị ngày giờ hệ thống
date -s "2013-04-25 09:30:10AM": thay đổi ngày giờ hệ thống
top: hiển thị danh sách những tiến trình đang chiếm dụng CPU nhiều nhất
free: hiển thị thông tin bộ nhớ (ở dạng kilobytes, dùng thêm -m để hiển thị ở dạng megabytes)
df -h: hiển thị thông tin đĩa ở chế độ dễ đọc cho người dùng
cat /proc/cpuinfo: hiển thị thông tin CPU
cat /proc/version: hiển thị thông tin phiên bản Linux
lsb_release -a | cat /etc/issue | cat /etc/lsb-release : for ubuntu version
cat /etc/redhat-release: for centos version
cat /proc/filesystems: hiển thị các loại tập tin hệ thống đang dùng
who: hiển thị các người dùng đã đăng nhập vào máy (được thống kê từ khi máy tính được bất lần cuối)
last: liệt kê tất cả người dùng đã đăng nhập vào máy từ trước đến giờ
history: liệt kê các lệnh đã được thực thi trên terminal của người dùng hiện tại từ trước đến giờ
uptime: hiển thị thời gian máy tính đã hoạt động kể từ lần bật máy cuối cùng
ps: liệt kê những tiến trình đang được thực thi bởi người dùng hiện tại (ps = print status)
ps axu: liệt kê tất cả những tiến trình đang được chạy của tất cả người dùng
set: hiển thị môi trường người dùng hiện tại như một số biến môi trường lưu thông tin tên máy, tên người dùng…
echo $PATH: hiển thị nội dung của biến môi trường tên “PATH”, bạn có thể xem tên biến môi trường qua lệnh set
lsmod: hiển thị các modules của kernel đang được tải (lệnh dành cho root)
/sbin/lsmod: hiển thị các modules của kernel đang được tải (lệnh dành cho người dùng bình thường)
pwd: hiển thị đường dẫn thư mục đang làm việc
Đối với các lệnh có số lượng thông tin hiển thị quá nhiều và tốn lần cuộn màn hình,  thêm "| more" để có thể xem từng phần.
tail -f   file.type: Hiển thị follow dạng lifetime
tail 100   file.type: Hiển thị 100 dòng cuối
mkdir -p /home/a/b/c: Tạo cây thư mục
Nén và giải nén file trong linux
tar -xzfv example.tar.gz -C /home/a/b/c: Giải nén example.tar.gz tới thư mục /home/a/b/c
Toán tử: &&: nếu lệnh 1 thành công thì làm lệnh 2, lệnh 1 fail thì không làm lệnh 2.
||: nếu lệnh 1 thành công thì không làm lệnh 2, nếu lệnh 1 fail thì làm lệnh 2.

chmod system-administrators-users   filename.type: phân quyền thư mục và tập tin:
Các giá trị: read = 4, write = 2, execute = 1, rwe = 4 (read) + 2 (write) + 1 (execute) = 7, rw = 4 (read) + 2 (write) = 6...
Dưới đây là một số lệnh chmod thường dùng:
chmod 777 filename: Cấp quyền truy cập đầy đủ cho mọi đối tượng người dùng.
chmod 775 filename: Cấp quyền truy cập đầy đủ cho chủ hệ thống và nhóm quản trị, đối tượng người dùng chỉ có quyền đọc (read) và chạy (execute) file.
chmod 755 dirname: Cấp quyền truy cập đầy đủ cho hệ thống, chỉ cho phép nhóm quản trị và đối tượng người dùng đọc và chạy các file trong thư mục.
chmod 700 filename: Chỉ cấp quyền truy cập đầy đủ cho chủ hệ thống và chặn truy cập với mọi đối tượng khác.
chmod 500 dirname: Không cho phép nhóm quản trị và người dùng truy cập vào file trong thư mục, đồng thời giới hạn quyền chủ hệ thống chỉ đọc và chạy để tránh xóa và thay đổi các file trong thư mục này.
chmod 660 filename: Cho phép chủ hệ thống và nhóm quản trị đọc, sửa, xóa và ghi dữ liệu vào file, nhưng không phân quyền truy cập cho những người dùng khác.

rm -rf: xóa file và folder không quan tâm tồn tại hay không.
find /folder_find   -name   filename.type: tìm kiếm filename.type trong folder cha folder_find
uname -a: for kernel version.
nano /etc/resolv.conf: Khai báo DNS server
ps aux | grep sshd (netstat -tap | grep sshd): Hiển thị các tiến trình (session) sshd
kill UID: đóng tiến trình UID đang chạy.
ln: Tạo link giữa các file với nhau.
touch: Tạo tập tin
Manual Sync:
rsync -avz root@192.168.1.10:/home/ /home : Đồng bộ data của thư mục /home trên server 192.168.1.10 về thư muc /home trên local.
Đăng nhập ssh dùng key, không cần login
Trên ssh server:
Edit file /etc/ssh/sshd_config
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no
- Tạo cặp khóa rsa:
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/nagios/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/nagios/.ssh/id_rsa.
Your public key has been saved in /home/nagios/.ssh/id_rsa.pub.
- Upload public key lên server:
ssh-copy-id -i /home/super/.ssh/id_rsa.pub super@192.168.2.238
apt-get install smbfs: Cài đặt smbfs cho ubuntu
sudo mount -t smbfs -o username=chung.pham //192.168.2.12/qgs/Ins" "QGS  /tmp/test: Mount thư mục Ins QGS trên Windows server cho /tmp/test trên ubuntu
umount /tmp/test: Thôi mount (tuyệt đối không được rm -rf /tmp/test)
ssh with pem key (private key): ssh -i key.pem 2.2.2.2 (2.2.2.2 is IP of SSH server)
• Dấu | (ví dụ )
• Dấu & (ví dụ)
• Edit motd after ssh login: (/etc/update-motd.d/99-footer*) add this line at bottom:
printf "motd after login content"
• Edit banner while loging by ssh: Add this line to file (/etc/ssh/sshd_confi)
Banner /etc/banner
Then, add content to /etc/banner file and restart ssh
sudo /etc/init.d/ssh restart

nano /etc/network/interfaces: Khai báo static IP Ubuntu
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.2.239
netmask 255.255.255.0
network 192.168.2.0
broadcast 192.168.2.255
gateway 192.168.2.254
dns-nameservers 8.8.8.8
nano /etc/sysconfig/network-scripts/ifcfg-eth0: Khai báo static IP CentOS
# Intel Corporation 82545EM Gigabit Ethernet Controller (Copper)
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
TYPE=Ethernet
NETWORK=192.168.2.0
NETMASK=255.255.255.0
IPADDR=192.168.2.240
DNS1=8.8.8.8
DNS2=8.8.4.4
USERCTL=no
GATEWAY=192.168.2.254
Tạo đường dẫn khởi động dịch vụ khi startup:
Ubuntu: nano /etc/rc.local  (add line: /etc/init.d/servername restart)
CentOS: chkconfig –levels 235 service_name on
Sử dụng server làm router,
Edit file /etc/sysctl.conf
net.ipv4.ip_forward=1
Setup LAMP
sudo apt-get install Apache2
sudo apt-get install Mysql-server
sudo apt-get install php5
sudo apt-get install php5-gd
sudo apt-get install php5-imap
sudo apt-get install php5-openssl
Install mysql for apache http server
sudo apt-get install libapache2-mod-auth-mysql
sudo apt-get install php5-mysql
Disable IPv6 (/etc/sysctl.conf)
# IPv6 configuration
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
Apply:
sudo sysctl -p
Add user for root privillege
adduser super
sudo /usr/sbin/visudo
super    ALL=(ALL:ALL) ALL
Reset passs user root mySQL
sudo /etc/init.d/mysql stop
sudo mysqld_safe --skip-grant-tables &
mysql -u root
use mysql;
update user set password=PASSWORD("pass_root") where User='root';
flush privileges;
quit
Remove mySQL
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean
Allow remote access mySQL
Edit file /etc/mysql/my.conf
bind_address = IP_Computer
Grant permission for remote access
GRANT ALL PRIVILEGES ON *.* to jsmith@'69.234.27.102' IDENTIFIED BY 'jimspassword';
 • Grant permission user for database:
CREATE USER 'user'@'localhost' IDENTIFIED BY 'pass';
GRANT ALL ON db_name.* TO 'user'@'localhost';
• Stop node js process (Tắt tiến trình nodejs):
ps aux | grep node
sudo kill -9 PID
• 301 Redirect www to blog
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.chungkol.com [NC]
RewriteRule ^(.*)$ http://blog.chungkol.com/$1 [L,R=301] 
• Setup default home for user
sudo usermod -d /srv/ftp user_name