Cafemmo Club
← Quay lại danh sách

Tuyển tập lệnh Linux by sincos

sincos17/04/20191425 lượt xem12 bình luận

Nghĩ đi nghĩ lại chả có gì chia sẻ vì trong này toàn cao thủ. Thôi thì có mỗi cái này do mình sưu tập và note lại thành file dùng dần phòng khi quên đỡ mất công search lại. Nay chia sẻ với anh em.

# Monitor traffic and http request -  cái này rất hay dùng theo dõi dạng realtime trên terminal các request tới server nhé</p>
<h2 class="mt-4 text-xl font-bold">Cài đặt</h2>
<p>yum install tcpdump -y</p>
<h2 class="mt-4 text-xl font-bold">tcpdump filter for HTTP GET</h2>
<p>tcpdump -s 0 -A &#39;tcp[((tcp[12:1] &amp; 0xf0) &gt;&gt; 2):4] = 0x47455420&#39;</p>
<h2 class="mt-4 text-xl font-bold">Theo dõi card mạng</h2>
<p>yum install nethogs -y
nethogs</p>
<p>yum -y install nload
nload venet0 -u M
nload venet0 -u M devices eth2</p>
<h2 class="mt-4 text-xl font-bold">Xem tổng số connection tới server - realtime</h2>
<p>watch -n 1 &#39;netstat -an | grep :80 | wc -l&#39;</p>
<h2 class="mt-4 text-xl font-bold">Xem số connection ứng với từng IP</h2>
<p>netstat -ntu | awk &#39;{print $5}&#39; | cut -d: -f1 | sort | uniq -c | sort -nr | head -n 30</p>
<h2 class="mt-4 text-xl font-bold">Xem số connection ứng với từng port</h2>
<p>netstat -tuna | awk -F&#39;:+| +&#39; &#39;NR&gt;2{print $5}&#39; | cut -d: -f1 | sort | uniq -c | sort -nr</p>
<h2 class="mt-4 text-xl font-bold">Change hostname &lt;= <a href="https://support.rackspace.com/how-to/centos-hostname-change/" target="_blank" rel="noopener noreferrer nofollow ugc">https://support.rackspace.com/how-to/centos-hostname-change/</a></h2>
<h2 class="mt-4 text-xl font-bold">Đổi trong file sau và reboot lại</h2>
<p>vi /etc/sysconfig/network</p>
<h2 class="mt-4 text-xl font-bold">Nếu không muốn reboot thì set hostname lại bằng lệnh sau và mở terminal khác lên để thấy sự thay đổi</h2>
<p>hostname new_name</p>
<h2 class="mt-4 text-xl font-bold">Lệnh đồng bộ lại hostname của DigitalOcean khi restore từ mộ snapshot</h2>
<h2 class="mt-4 text-xl font-bold">Vì khi restore thì vps mới sẽ vẫn bị set hostname theo vps đã tạo ra snapshot đó</h2>
<p>HNAME=&quot;$(cat /etc/hosts | grep &quot;127.0.0.1 www&quot; | awk &#39;{print $2}&#39;)&quot; &amp;&amp; sed -i -E &quot;s/HOSTNAME=[^\
]+/HOSTNAME=$HNAME/g&quot; /etc/sysconfig/network &amp;&amp; hostname $HNAME &amp;&amp; hostname</p>
<h2 class="mt-4 text-xl font-bold">Test syntax of bash file before run</h2>
<p>sh -n script-name</p>
<h2 class="mt-4 text-xl font-bold">Fix error: /bin/bash^M: bad interpreter</h2>
<h2 class="mt-4 text-xl font-bold">Mở file bằng vi hoặc vim để ở chế độ lệnh và gõ một trong 2 lênh sau rồi lưu lại</h2>
<h2 class="mt-4 text-xl font-bold"><a href="https://stackoverflow.com/a/5514351" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/a/5514351</a></h2>
<p>:set fileformat=unix
:set ff=unix</p>
<h2 class="mt-4 text-xl font-bold">wget save với chỉ định tên file</h2>
<p>wget -O example.html <a href="https://www.electrictoolbox.com/wget-save-different-filename/" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.electrictoolbox.com/wget-save-different-filename/</a></p>
<h2 class="mt-4 text-xl font-bold">wget with ipv4 - ipv6</h2>
<p>wget -4 -O example.html <a href="https://www.electrictoolbox.com/wget-save-different-filename/" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.electrictoolbox.com/wget-save-different-filename/</a>
wget -6 -O example.html <a href="https://www.electrictoolbox.com/wget-save-different-filename/" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.electrictoolbox.com/wget-save-different-filename/</a></p>
<h2 class="mt-4 text-xl font-bold">Lấy danh sách filename</h2>
<p>ls -lA | awk -F&#39;:[0-9]* &#39; &#39;/:/{print $2}&#39;</p>
<h2 class="mt-4 text-xl font-bold">Xóa hết danh sách file</h2>
<p>cat 1.txt | xargs echo rm | sh</p>
<h2 class="mt-4 text-xl font-bold">Lọc hết những file rỗng (empty)</h2>
<p>find  /tmp -type f -empty</p>
<h2 class="mt-4 text-xl font-bold">Lọc hết file có chứa keyword, từ khóa, nội dung</h2>
<p>find / -type f -exec grep -H &#39;keyword&#39; {} \\;</p>
<h2 class="mt-4 text-xl font-bold">Xem 1 file liên tục từ dòng 30 đến dòng 60</h2>
<p>watch -n 1 &quot;sed -n &#39;30,60p&#39; nginx.conf&quot;</p>
<h2 class="mt-4 text-xl font-bold">Xem 1 file liên tục 30 dòng đầu tiên</h2>
<p>watch -n 1 &quot;head -30 nginx.conf&quot;</p>
<h2 class="mt-4 text-xl font-bold">Xem 1 file liên tục 30 dòng cuối cùng</h2>
<p>watch -n 1 &quot;tail -n 30 /home/nginx/logs/nginx-error.log&quot;
watch -n 1 &quot;tail -n 35 1.txt&quot;
watch -n 1 &quot;tail -n 35 2.txt&quot;
watch -n 1 &quot;tail -n 35 3.txt&quot;
watch -n 1 &quot;tail -n 35 4.txt&quot;</p>
<h2 class="mt-4 text-xl font-bold">Limit open file</h2>
<h2 class="mt-4 text-xl font-bold">Các ứng dụng hay bị lỗi: Too many open files</h2>
<h2 class="mt-4 text-xl font-bold">nginx với traffic cao hoặc các ứng dụng ghi nhiều file</h2>
<p>vi /etc/security/limits.conf</p>
<h2 class="mt-4 text-xl font-bold">Thêm cái này vào cuối file - logout và login lại</h2>
<ul class="my-2 list-disc pl-5"><li>hard    nofile      500000</li><li>soft    nofile      500000</li></ul>
<p>root      hard    nofile      500000
root      soft    nofile      500000</p>
<h2 class="mt-4 text-xl font-bold">Kiểm tra việc limit</h2>
<p>ulimit -Hn
ulimit -Sn</p>
<h2 class="mt-4 text-xl font-bold">Kiểm tra port được sử dụng bởi thằng nào</h2>
<p>netstat -nlp | grep :80</p>
<h2 class="mt-4 text-xl font-bold">Kiểm tra toàn bộ</h2>
<p>netstat -tulpn</p>
<h2 class="mt-4 text-xl font-bold">Đếm tổng số file trong thư mục hiện tại</h2>
<p>find . -type f | wc -l</p>
<p>cp -a /source/. /dest/</p>
<h2 class="mt-4 text-xl font-bold">List all user</h2>
<p>cat /etc/passwd</p>
<h2 class="mt-4 text-xl font-bold">mysql import</h2>
<p>mysql -u username -p database_name &lt; file.sql
mysql -u root -p &lt; database.sql</p>
<h2 class="mt-4 text-xl font-bold">Mysql export</h2>
<p>mysqldump -u root -p database-name &gt; backup.sql
mysqldump -u root -p --all-databases &gt; database.sql</p>
<h2 class="mt-4 text-xl font-bold">Mysql repair all table</h2>
<p>mysqlcheck -A --auto-repair -u root -p</p>
<h2 class="mt-4 text-xl font-bold">Hardlink và Softlink trong hệ thống file của linux</h2>
<h2 class="mt-4 text-xl font-bold">2 cái đều trỏ về một vùng nhớ chứa dữ liệu</h2>
<h2 class="mt-4 text-xl font-bold">Softlink là 1 dạng shortcut nên khi xoá source thì sẽ mất dữ liệu</h2>
<h2 class="mt-4 text-xl font-bold">Hardlink khi xoá 1 trong 2 (Hardlink hoặc source) thì dữ liệu vẫn còn nguyên và thằng còn lại vẫn còn dữ liệu</h2>
<h2 class="mt-4 text-xl font-bold">Ứng dụng khi cần di chuyển dữ liệu nhưng vẫn muốn giữ lại cấu trúc đường dẫn thư mục (ví dụ thư mục: uploads, images.....)</h2>
<h2 class="mt-4 text-xl font-bold">Source                             SoftLink</h2>
<p>ln -s /home/jake/doc/test/2000/something /home/jake/xxx</p>
<h2 class="mt-4 text-xl font-bold">Source                             HardLink</h2>
<p>ln /home/jake/doc/test/2000/something /home/jake/xxx</p>
<h2 class="mt-4 text-xl font-bold">Test tốc độ ổ cứng</h2>
<p>dd if=/dev/zero of=/1GB bs=1024 count=1024k
dd if=/dev/zero of=/512MB bs=512 count=1024k</p>
<h2 class="mt-4 text-xl font-bold">Check KMV or Openvz =&gt; nếu file sau có thì là openvz</h2>
<p>cat /proc/user_beancounters</p>
<h2 class="mt-4 text-xl font-bold">Kiểm tra phiên bản của Centos version</h2>
<p>rpm --query centos-release</p>
<h2 class="mt-4 text-xl font-bold">Kill process</h2>
<p>kill -SIGTERM PID</p>
<p>for pid in $(ps -ef | grep &quot;ffmpeg&quot; | awk &#39;{print $2}&#39;); do echo $pid &amp; kill -9 $pid; done
for pid in $(ps -ef | grep &quot;abc.com&quot; | awk &#39;{print $2}&#39;); do echo $pid &amp;&amp; kill -9 $pid; done</p>
<h2 class="mt-4 text-xl font-bold">Zip toàn bộ thư mục hiện tại</h2>
<p>zip -r sincos.zip ./*</p>
<h2 class="mt-4 text-xl font-bold">Zip toàn bộ thư mục hiện tại bỏ qua 1 số thư mục con</h2>
<p>zip -r sincos.zip ./* -x ./bo-qua-1/<strong>\\* ./bo-qua-2/</strong>\\*</p>
<h2 class="mt-4 text-xl font-bold">Khắc phục lỗi unknown host trong terminal khi ssh tới 1 server lần đầu tiên</h2>
<h2 class="mt-4 text-xl font-bold">Các 1:</h2>
<p>ssh -oStrictHostKeyChecking=no root@23.227.178.99</p>
<h2 class="mt-4 text-xl font-bold">Cách 2: Vào ~/.ssh/config thêm dòng nội dung sau:</h2>
<p>StrictHostKeyChecking no</p>
<h2 class="mt-4 text-xl font-bold">Hiện ngày giờ theo timezone</h2>
<p>echo <code>TZ=Asia/Saigon date</code></p>
<h2 class="mt-4 text-xl font-bold">Update date and time =&gt; Nhiều khi server sai giờ</h2>
<p>yum install ntp
ntpdate pool.ntp.org</p>
<h2 class="mt-4 text-xl font-bold">Nếu muốn tự động sync thì thêm phần sau</h2>
<p>chkconfig ntpd on
ntpdate pool.ntp.org
service ntpd start</p>
<h2 class="mt-4 text-xl font-bold">Fix Centos: BAD PASSWORD: it is based on a dictionary word</h2>
<h2 class="mt-4 text-xl font-bold"><a href="https://www.linuxquestions.org/questions/linux-software-2/bad-password-it-is-based-on-a-dictionary-word-449149/" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.linuxquestions.org/questions/linux-software-2/bad-password-it-is-based-on-a-dictionary-word-449149/</a></h2>
<p>vi /etc/pam.d/system-auth</p>
<p>The original file looks like this</p>
<hr class="my-4 border-neutral-200 dark:border-neutral-700" />
<p>password requisite pam_cracklib.so try_first_pass retry=3
password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok
password required pam_deny.so</p>
<hr class="my-4 border-neutral-200 dark:border-neutral-700" />
<p>Comment all the three lines</p>
<hr class="my-4 border-neutral-200 dark:border-neutral-700" />
<h2 class="mt-4 text-xl font-bold">password requisite pam_cracklib.so try_first_pass retry=3</h2>
<h2 class="mt-4 text-xl font-bold">password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok</h2>
<h2 class="mt-4 text-xl font-bold">password required pam_deny.so</h2>
<hr class="my-4 border-neutral-200 dark:border-neutral-700" />
<p>Add this line</p>
<hr class="my-4 border-neutral-200 dark:border-neutral-700" />
<p>password sufficient /lib/security/$ISA/pam_unix.so nullok md5 shadow</p>
<hr class="my-4 border-neutral-200 dark:border-neutral-700" />
<p>It will look like this now</p>
<hr class="my-4 border-neutral-200 dark:border-neutral-700" />
<h2 class="mt-4 text-xl font-bold">password requisite pam_cracklib.so try_first_pass retry=3</h2>
<h2 class="mt-4 text-xl font-bold">password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok</h2>
<h2 class="mt-4 text-xl font-bold">password required pam_deny.so</h2>
<p>password sufficient /lib/security/$ISA/pam_unix.so nullok md5 shadow</p>
<hr class="my-4 border-neutral-200 dark:border-neutral-700" />
<p>Note:</p>
<hr class="my-4 border-neutral-200 dark:border-neutral-700" />
<p>If you run authconfig, the file /etc/pam.d/system-auth will be overwritten
------------------------------------------------------------------------------------

Bình luận

Đang tải...

Đang kiểm tra đăng nhập...