نمایش ای دی و گروه های کاربر جاری
1 |
$ id |
نمایش کسایی که با ترمینال به سیستم وصل هستند
1 2 3 |
$ who # or $ w |
اضافه کردن یک کاربر به گروه خاص
1 2 |
# add myuser to 'sudo' group $ usermod -aG sudo myuser |
نمایش تمام گروه ها
1 |
$ cat /etc/group |
نمایش تمام کاربرها
1 |
$ cat /etc/passwd |
نمایش گروه های یک کاربر خاص
1 2 3 4 |
# get groups of current user $ groups # get groups of XXX user $ groups XXX |
نمایش نوع یک فایل
1 2 3 4 |
$ file /bin/ls # => /bin/ls: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 3.2.0, BuildID[sha1]=9567f9a28e66f4d7ec4baf31cfbf68d0410f0ae6, stripped $ file test.txt # => test.txt: UTF-8 Unicode (with BOM) text, with CRLF line terminator |
نمایش اطلاعات یک فایل
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Modify is the last time that content of file modified # Change is the last time that file mta changed like owner, permissions and ... # when 'Modify time' change, the 'Change time' will be change also $ stat test.txt >>> File: test.txt Size: 22 Blocks: 8 IO Block: 4096 regular file Device: 813h/2067d Inode: 2268887 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ pepsi) Gid: ( 1000/ pepsi) Access: 2019-10-19 19:33:03.438111962 +0330 Modify: 2019-10-19 19:33:03.278114716 +0330 Change: 2019-10-19 19:33:03.278114716 +0330 Birth: - |
alias
ایجاد نام میانبر برای دستورات
1 2 3 4 |
$ alias hello='echo "hello"' $ hello # to delete an alias use 'unalias' $ unalias hello |
پاک کردن دستوراتی که قبلا در ترمینال وارد شده
1 2 |
# if you use bash it willtrunat ~/.bash_history $ history -c |
قرار دادن `xxx` به عنوان محتویات فایل test.txt، اگر فایل وجود نداشته باشه ساخته میشه
1 |
$ echo 'xxx' > test.txt |
اضافه کردن `xxx` به انتهای فایل test.txt، اگر فایل وجود نداشته باشه ساخته میشه
1 |
$ echo 'content' >> test.txt |
خالی کردن محتویات فایل
1 2 3 4 5 6 7 |
$ echo '' | test.txt # or $ :>test.txt # or $ truncate -s 0 test.txt # or $ cat /dev/null > test.txt |
فرستادن محتویات فایل `test.txt` به stdin یک برنامه
1 |
$ cat < test.txt |
اجرای برنامه app
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# 1 is standard output # 2 is standard error # & is both standard ouput and standard error # just run `test` and skip storing standard output and standard error $ app 1>/dev/null 2>/dev/null # or $ app >/dev/null 2>/dev/null # or $ app >/dev/null 2>&1 # capture both standard output and standard error to `result.log` $ app &> result.log # run it in background $ app & |
اجرای برنامه app با nohup
مزیت nohup نسبت به اجرای برنامه در پیش زمینه به وسیله & این هست که اگه ترمینال بسته بشه یا کاربر log out کنه اون برنامه بسته نمیشه
1 2 |
# capture both standard output and standard error to `nohup.log` $ nohup app &> nohup.out & |
ln
با کمک این ابزار میتونید بین فایلها و دایرکتوری ها لینک بسازید. این لینکها در دو نوع hard و soft (symbolic) هستند.
soft لینک ها مانند یک لینک میانبر برای اجرای فایل و یا دایرکتوری عمل میکنن.
hard لینک ها مختص فایلها هستن، وقتی یک hard لینک از یک فایل ایجاد میکنید تغییر در نام و یا حذف کردن فایل اصلی تاثیری در لینک ایجاد شده نمیزاره، اما وقتی در محتوای فایل اصلی تغییری ایجاد میشه، همون تغییرات در محتویات لینک هم اعمال میشه.
ln به صورت پیش فرض hard لینک درست میکنه.
1 2 3 4 5 6 7 8 |
# TARGET is the file/directory that you want to make a link from # LINK_NAME is the name of the created file $ ln TARGET LINK_NAME # always pass absolute path in TARGET # soft link $ ln -s /usr/bin xxx # hard link $ ln /bin/ls myls |
استخراج یک رشته تصادفی 100 کاراکتری که Base64 انکد شده باشه با کمک openssl
1 |
$ openssl rand -base64 100 |
استخراج یک رشته تصادفی 100 کاراکتری با dev/urandom/
1 |
$ strings /dev/urandom | head -n 30 | tr -d [:space:] |
استخراج یک رشته تصادفی 100 کاراکتری با dev/urandom/ که فقط عدد باشه
1 |
$ strings /dev/urandom | head -n 100 | grep -o '[[:digit:]]' | tr -d [:space:] |
dpkg
این ابزار برای مدیریت بسته ها در توزیع های debian استفاده میشه.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# -i, --install install a deb package $ dpkg -i xxx.deb # -I, --info show info of a deb package $ dpkg -I xxx.deb # -c, --contents list contents of a deb package $ dpkg -c xxx.deb # -x, --extract extract a deb package $ dpkg -x xxx.deb ./files # -r, --remove remove a package $ dpkg -r xxx # -P, --purge remove package and its config files $ dpkg -P xxx # -V, --verfiy verify an installed package file integrity $ dpkg -V xxx # -L, --listfiles list package installed files $ dpkg -L Xapp # -s, --status see an installed package information $ dpkg -s xxx # -l, --list search for an installed package, or list all if nothing pass $ dpkg -l "Xa*" # all installed packages $ dpkg -l |
apt
این ابزار برای مدیریت بسته ها در توزیع های debian استفاده میشه.
1 2 3 4 5 6 7 8 9 10 |
# download package deb file $ apt download php # install a package from repository $ apt install xxx # reinstall package $ apt install --reinstall xxx # remove package $ apt remove xxx # remove package and delete config files $ apt purge xxx |
systemctl
ابزار جدیدی که برای مدیریت سرویس ها در نسخه های جدید لینوکس استفاده میشه systemd هست این ابزار جایگزین system v init شده، systemctl رابط کاربری systemd هست.
در نسخه های قدیمی لینوکس برای مدیریت سرویس ها مفهومی داشتیم به نام runlevel. هر runlevel یک وضعیت رو تعریف میکنه. در هر runlevel یک سری سرویس ها باید اجرا باشن و سرویس هایی که در این runlevel تعریف نشدن باید متوقف باشن. runlevel معمولا از ۰ تا ۶ هست، به اینصورت که
- شماره ۰ سیستم خاموش هست.
- شماره ۱ وضعیتی هست که کارهای ابتدایی میتونید با سیستم انجام بدید.
- شماره ۲ تا ۴ وضعیتی هست که بیشتر سرویس ها رو دارید.
- شماره ۵ رابط کاربری هم خواهید داشت.
- شماره ۶ سیستم reboot می شود.
runlevel ها در نسخه های جدید لینوکس و جایگزین شدن system v init با systemd جای خودش رو به target ها داده با همون مفهوم به اینصورت که هر runlevel تبدیل به یک target شده
- شماره ۰ تارگت poweroff
- شماره ۱ تارگت rescue
- شماره ۲ تا ۴ تارگت multi-user
- شماره ۵ تارگت graphical
- شماره ۶ تارگت reboot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# start a service $ systemctl start application.service # you can omit the 'service' suffix $ systemctl start application $ systemctl stop application $ systemctl restart application $ systemctl status application # you can pass multiple services $ systemctl start application application1 # if service implement reload function, this command will reload service, if not it will be restarted $ systemctl reload-or-restart application # autostart service # this command creates a symbolic link from service file that locate in # '/etc/systemd/system' or '/lib/systemd/system' to # '/etc/systemd/system/some_target.target.wants' $ systemctl enable application # to disable autostart $ systemctl disable application # list all active units $ systemctl list-units # list active and inactive units $ systemctl list-units --all # list active services $ systemctl list-units -t service # filtering $ systemctl list-units --type=service $ systemctl list-units --all --state=inactive # check unit file, unit can be a service or a target $ systemctl cat application # check low-level properties of unit $ systemctl show application # edit a unit # this will create a directory that contains overriden info # to '/etc/systemd/system/application.service.d' $ systemctl edit application # to edit original file $ systemctl edit application --full # masking a service means disable starting service $ systemctl mask application # to unmask $ systemctl unmask application # get default target $ systemctl get-default # list all targets $ systemctl list-units type=target # list autostart services $ systemctl list-unit-files | grep enabled # list running services $ systemctl | grep running # change default target $ systemctl set-default graphical.target # enable specific target $ systemctl isolate multi-user.target # activate rescure target $ systemctl rescure # or emergency $ systemctl emergency # you can do shutting down and rebooting with it $ systemctl reboot $ systectl halt $ systemctl poweroff # debug specific service possible errors in loading stage by systemd $ systemd-analyze verify service_name.service |
یک نمونه فایل سرویس
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[Unit] Description= After=network.target [Service] User=ubuntu WorkingDirectory=/home/my_app/ # no " in values Environment=APP_MODE=production Environment=DATABASE_URL=sqlite:////path/to/the/database.sqlite ExecStart=/bin/bash -c 'rm -rf ./temp/*' ExecStart=/home/my_app/run Restart=always [Install] WantedBy=multi-user.target |
دو مسیر مهمی که systemctl فایلهای سرویس رو از اونجا بارگزاری میکنه (مسیر های دیگه ای هم هستنند)
- etc/systemd/system/
- lib/systemd/system/
journalctl
این ابزار جزو مه systemd هست و برای مدیریت لوگها یا جورنال های systemd استفاه میشه.
هدف اصلی این ابزار مدیریت بهتر و مشاهده سریعتر و کارامدتر لوگهای کلی سیستم هست. journalctl لوگها رو به صورت باینری ذخیره میکنه.
به صورت پیش فرض لوگها (جورنال ها) پایدار نیستن و با هر بار ریبوت شدن سیستم پاک میشن، برای اینکه اون ها رو دائمی داشه باشید باید در
etc/systemd/journald.conf/ مقدار Storage=persistent قرار بدید و بعد systemctl restart systemd-journald
لوگ ها در مسیر var/log/journal/ ذخیره میشن
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# show journals in utc time format $ journalctl --utc # journalctl shows logs from oldest to newest to reverse this use '-r' $ journalctl -r # query by time $ journalctl --since "2 days ago" $ journalctl --since "2000-01-22 18:19:00" --until "2010-01-22 18:19:00" $ journalctl --since 09:00 --until "1 hour ago" $ journalctl --since yesterday # query by priority # 0: emerg, 1: alert, 2: crit, 3: err, 4: warning, 5: notice, 6: info, 7: debug $ journalctl -p 0 # see kernel messages $ journalctl -k # specific services $ journalctl -u application $ journalctl -u application -u applicaion1 # check failed login attempts # in Debian $ journalctl -u ssh.service | grep "Fail" # in CentOS or RHEL $ journalctl -u sshd.service | grep "Fail" # follow mode, in this mode you can see new entries in real time $ journalctl -f # show entries at once with no pager $ journalctl --no-pager # show last 10 messages $ journalctl -n # show last 50 messages $ journalctl -n 50 # show stored journals disk usage $ journalctl --disk-usage # free up journals to specific size $ journalctl -vacuum-size=10G $ journalctl --vacuum-time=10days |
تنظیم منطقه زمانی
1 2 3 4 5 6 7 8 |
# first see all timezones $ timedatectl list-timezones # then set it $ timedatectl set-timezone 'desired zone' # check new timezone $ timedatectl status # in debian you can use $ dpkg-reconfigure tzdata |
تنظیم ساختار کیبورد در دبیان
1 |
$ dpkg-reconfigure keyboard-configuration |
تنظیم کردن لوکال (زبان، منطقه جغرافیایی و ...)
1 2 3 4 5 6 7 |
# see current locale settings $ locale # update locale in Debian # pass a key value pair to update-locale $ update-locale LANG=en_US.UTF-8 # in Cent there is localctl $ localctl set-locale LANG=en_US.UTF-8 |
تنظیم ساعت
1 2 3 4 5 6 7 8 9 10 11 12 |
# set system time $ date --set="23 June 1988 10:00:00" $ date --set="10:00:00" # set hardware clock $ hwclock --set --date="2013-7-31 09:30" # set the hardware Clock to the current System Time. $ hwclock --systohc # set the system time from the hardware Clock. $ hwclock --hctosys |
cat
نمایش محتویات فایل
1 2 3 4 5 6 7 |
$ cat test.txt # to show line numbers use -n $ cat -n test.txt # to see line ends and tabs use -A, it'll show line ends as '$', '^I' as tab and '^M' as carriage return # in linux line end is '\n' or LF # in windows line end is '\r\n' or CRLF $ cat -A test.txt |
ایجاد آرشیو
1 2 3 4 |
$ tar cvf test.tar test $ tar zcvf test.tar.gz test $ tar jcvf test.tar.bz2 test $ tar Jzcvf test.tar.xz test |
استخراج آرشیو
1 2 3 4 5 |
$ tar zxvf data.tar.gz $ tar jxvf data.tar.bz2 $ tar Jxvf data.tar.xz # in tar new versions, you can ommit archive type option $ tar xvf data.tar.gz |
مانیتور کردن یک برنامه خاص برای دیدن مصرف حافظه و cpu
در برنامه top فیلد RES برابر مقدار حافظه فیزیکی مصرفی برنامه مورد نظر هست
1 |
$ top -p PID |
type
نمایش اطلاعات یک دستور
1 2 3 4 5 6 |
$ type pwd # => pwd is a shell builtin $ type ls # => ls is aliased to `ls --color=auto' $ type ll # => ll is aliased to `ls -alF |
kill
ابزاری برای ارسال سیگنال به پروسه ها
1 2 3 4 5 6 7 |
# to see available signals $ kill -l # kill sends TERMINATE signal by default # 1234 is process pid $ kill 1234 # send KILL signal $ kill -KILL 1234 |
killall
این برنامه برخلاف kill که pid قبول میکنه نام کامل پروسه رو میگیره
1 2 3 |
$ killall firefox # send KILL signal $ killall -KILL firefox |
pkill
شما باید نام برنامه رو بفرستید ولی لازم نیست کامل باشه در واقع با grep اون رو پیدا میکنه
1 2 3 4 5 |
$ pkill fire # send KILL signal $ pkill -KILL fire # to logout user 'pepsi' $ pkill -u pepsi |
بستن ترمینال یک کاربر
1 2 3 4 |
# get list of active ttys by 'w' # omit /dev/ from names # to really kill terminal you must pass -9 $ pkill -9 -t pts/6 |
پیدا کردن برنامه در برنامه های در حال اجرا - نسخه قشتگتر ps aux | grep XX
1 2 |
# to find all python processes $ pgrep -a python |
پیدا کردن مسیری که یک پروسه در اون در حال اجراست
1 2 3 4 5 6 |
# get process PID by $ ps aux | grep PROCESS_NAME # now $ pwdx PID # or # ls -l /proc/PID/cwd |
fuser
با این ابزار میتونید ببینید چه پروسس هایی از یک فایل مشخص در حال استفاده هستند.
1 2 3 4 5 6 |
$ fuser myfile # to see user use -u # to kill those processes use -k $ fuser -k myfile # to send other signal than KILL, just pass it $ fuser myfile -k -TERM |
اجرای یک برنامه به همراه تنظیم کردن یک متغیر محیطی برای اون برنامه
1 2 |
# just set desired variables before typing command $ DEBUG=1 ENV2=100 python main.py |
مشاهده اینکه یک برنامه از چه کتابخونه هایی استفاده میکنه
1 |
$ ldd /usr/ls |
اتصال به سرور با شل
1 2 3 4 5 |
$ ssh username@remote # -p to set port $ ssh root@1.2.3.4 -p 1516 # to just run command on remote, pass desired command in quotation marks as last argument $ ssh root@1.2.3.4 "ls /home" |
تنظیمات OpenSSH
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# user settings can be store in ~/.ssh/config # system wide settings can be store in /etc/ssh/ssh_config # you can add shortcuts for hosts to make it easier to ssh to a server # sample settings Host * User root Port 22 # ServerAliveInterval make openssh send null packets when in server mode every 60 seconds to make other side alive # when in client mode use ClientAliveInterval # ServerAliveCountMax set how many times try if no response recieved ServerAliveInterval 60 ServerAliveCountMax 30 ServerAliveInterval 60 ServerAliveCountMax 30 # with BatchMode enabled, the SSH connection will fail immediately if the key is rejected, instead of failing back to # a password prompt. This will prevent your scripts from "hanging" while it waits for you to type a password. # you can do this in terminal like `ssh -o BatchMode=true root@1.1.1.1 ....` BatchMode true # override as per host Host server1 HostName server1.cyberciti.biz Port 4242 # use specific public key IdentityFile /path/to/id_rsa |
اتصال با شل به سرور به وسیله کلید رمزنگاری شده
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# on your machine generate keys $ ssh-keygen # then we should add our key to remote trusted keys, to do it there are two ways # use "ssh-copy-id" $ ssh-copy-id user@remoteserver # or # copy content of public key $ cat ~/.ssh/id_rsa.pubis to add your machine key to server trusted keys # then on remote machine # create `authorized_keys` file if not exists with desired user $ mkdir ~/.ssh $ chmod 700 ~/.ssh $ touch ~/.ssh/authorized_keys $ chmod 600 ~/.ssh/authorized_keys # then paste copied content to ~/.ssh/authorized_keys # now whenever you try to ssh to remote server with the privileges of the user who you put # her keys on server, OS ssh agent will automatically authenticate you $ ssh user@server # to remove a key $ ssh-keygen -R [IP]:PORT |
تغییر پورت ssh
1 2 3 4 |
$ nano /etc/ssh/sshd_config # change below line to desired port Port 22 $ systemctl restard sshd |
rsync
با این ابزار میتونید بین دو سیستم فایل جا به جا کنید و یا محتویات دو مسیر رو با هم sync یا هماهنگ کنید.
قدرت این برنامه به دلیل استفاده از الگوریتم delta-transfer هست این الگوریتم با سرعت بالایی تغییرات فایلهای مبدا و مقصد رو بررسی و تنها تفاوت های فایلها رو ارسال میکنه و نه کل فایل رو.
این برنامه به طور پیش فرض فایلهای هم نامی که (mtime (modification time و یا اندازشون در مقصد متفاوت هست رو کپی میکنه.
وقتی دو مسیر رو بخواید sync کنید این برنامه به طور پیش فرض فایلهای اضافی که تو مسیر مقصد هست رو حذف نمیکنه برای اینکار باید سوییچ delete-- و force-- استفاده کنید.
اگه مسیر تو سرور مقصد وجود نداشته باشه این برنامه تا یک سطح رو براتون میسازه.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# sync /home/data on local with remote one, mind the forwarding slash in source directory # -a archive mode or rlptgod: r recursive, l copy symlinks as symlinks, p preserve permissions, t preserve modification time # g preserve group, o preserve owner (super-user only) and D for --devices --special # -v verbose # -z compress # -h human readable size # --progress show progress $ rsync -avzh /home/data/ root@12.13.14.15:/home/data --progress # copy (not sync) /home/data directory to remote (just omit forwarding slash in source directory) $ rsync -avzh /home/data root@12.13.14.15:/home # copy /home/data directory to remote and make a new directory # now data directory is inside /home/new_dir $ rsync -avzh /home/data root@12.13.14.15:/home/new_dir # copy data.tar.gz file from local to remote machine # if 'data' directory won't be presentd on remote, the file stored on that as 'data' $ rsync -avzh /home/data.tar.gz root@12.13.14.15:/home/data # to copy file with original name, add forwarding slash to destination directory $ rsync -avzh /home/data.tar.gz root@12.13.14.15:/home/ # transfer multiple files $ rsync -avzh /home/data.tar.gz data2 data3 root@12.13.14.15:/home/data # exclude a directory, the path is relative to source $ rsync -avzh --exclude 'logs' /home/data root@12.13.14.15:/home # exclude a specific file type $ rsync -avzh --exclude '*.db' /home/data root@12.13.14.15:/home # we can pass ssh arguments by '-e' option, for example using rsync with different ssh port $ rsync -avzh -e 'ssh -p 1056' /home/data/ root@12.13.14.15:/home/data # or $ rsync -avzhe 'ssh -p 1056' /home/data/ root@12.13.14.15:/home/data # sync and delete excessive files on remote, to force remove excessive directories, use --force $ rsync -avzhe 'ssh -p 1056' /home/data/ root@12.13.14.15:/home/data --delete --force |
iptraf
با این ابزار میتونید فعالیتهای شبکه رو بررسی کنید.
1 |
$ iptraf-ng |
مشاهده پورت های باز سیستم
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# -t tcp # -u udp # -p return program name # -n don't resolve ip to host names # -a list both listning and non-listing connections, without this you'll get only connected ones # this can also be achieved with `lsof -i` $ netstat -tunap # or $ netstat -tunp # to get just tcp connections $ netstat -tnap # to see which processes using port 80 $ netstat -tnap | grep :80 # -l to see listening connections # get tcp listening programmes with program name $ netstat -ltp # all descibed commands is achievable by 'ss' $ ss -tunap |
strace
با این ابزار میتونید فعالیت های یک برنامه رو بررسی کنید.
1 2 3 4 5 6 |
# -p attach to process # -f follow forks # -s length of string in each print $ strace -f -p 1234 -s 9999 # run 'tcpdump' and save output to 'output.log' $ strace -f -o output.log tcpdump |
ifstat
مشاهده ترافیک و سرعت شبکه
1 2 3 |
# -S show result at same line, not printing new lines # -T show total traffic $ ifstat -ST |
ioping
مشاهده response time دیسک
1 |
$ ioping /dev/sda1 |
lsof
این برنامه فایلهای باز سیستم را نشان می دهد و به دلیل اینکه هر چیزی در لینوکس از اتصال شبکه تا پروسس ها و... به صورت فایل مدیریت می شوند با این ابزار میشه کارهای فوق العاده ای کرد.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# list all open files $ lsof # list network connections $ lsof -i # list TCP connections $ lsof -iTCP # list connections on port 22 $ lsof -i :22 # list connections to a specific host $ lsof -i @10.20.30.40 # get open files of a specific user $ lsof -u pepsi # get processes than interact with specific file, this can be achieved with 'fuser' too $ lsof myfile # check open files of a spepcific process with its pid $ lsof -p 1234 # to return only process ids pass -t, so you can send signal to them # kill `lsof -u pepsi` |
مشخصات سیستم عامل
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$ uname -a Linux pepsi 5.3.0-53-generic #47~18.04.1-Ubuntu SMP Thu May 7 13:10:50 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux $ cat /etc/*-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=18.04 DISTRIB_CODENAME=bionic DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS" NAME="Ubuntu" VERSION="18.04.4 LTS (Bionic Beaver)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 18.04.4 LTS" VERSION_ID="18.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=bionic UBUNTU_CODENAME=bionic $ cat /proc/version Linux version 5.3.0-53-generic (buildd@lgw01-amd64-016) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #47~18.04.1-Ubuntu SMP Thu May 7 13:10:50 UTC 2020 |
مشخصات cpu
1 2 3 |
$ lscpu # or $ cat /proc/cpuinfo |
تعداد هسته های cpu
1 2 3 |
$ nproc --all # or $ lscpu | grep "CPU(s):" |
مدل و مشخصات سخت افزاهای متصل به سیستم
برنامه اول lshw
1 2 3 4 5 6 |
$ lshw # or $ lshw -short # to get specific hardware info, get hardware class name by 'lshw -short' and pass it through -c switch # only `disk` info $ lshw -c disk |
برنامه دوم dmidecode
اگه نوع سخت افزار مورد نظر رو با lshw پیدا نکردید از این برنامه استفاده کنید برای مثال lshw نوع کارت حافظه رو نمیده ولی این برنامه میده
1 2 3 4 |
$ dmidecode # to get specific hardware info, you can pass hardware type through -t switch # available types are: bios, system, baseboard, chassis, processor, memory, cache, connector, slot $ dmidecode -t bios |
مشخصات دستگاه های PCI متصل به سیستم
1 2 3 4 5 |
$ lspci # or more verbose $ lspci -vv # check wireless card $ lspci | grep -i wireless |
نمایش دیسک ها با ظرفیت
1 2 3 |
$ lsblk # to show with filesystem $ lsblk -f |
نمایش دیسکها به همراه لیبل
1 |
$ blkid |
نمایش مشخصات دیسکها
1 2 3 |
# first get disk path $ blkid $ smartctl -a /dev/sda |
مشخصات حافظه
1 2 3 |
$ free -h # or $ cat /proc/meminfo |
nmcli
با ابن برنامه میتونید مشخصات اتصال های شبکتون رو ببینید
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# show all network interfaces $ nmcli dev # get interfaces information like dns, ip and more $ nmcli dev show # show more information $ nmcli -f ALL dev show # get system ip $ nmcli dev show | grep IP4.ADDRESS # get available wifi networks # you must scan before running that by 'nmcli dev wifi rescan' $ nmcli dev wifi # show more information $ nmcli -f ALL dev wifi |
mount کردن
1 2 |
# mount /dev/sda to directory /media/sda $ mount /dev/sda1 /media/sda |
unmount کردن
1 2 3 4 |
# to unmount type mount path or devce path $ umount /dev/sda1 # or $ umount /media/sda |
eject
با کمک این ابزار میتونید دستگاه های usb متصل به سیستم رو safe remove کنید.
من قبل از کشیدن فلش متصل به دستگاه، به جای unmount کردن از این ابزار استفاده میکنم.
1 |
$ eject /dev/sdc |
دیدن لیست درایوهای mount شده به سیستم
1 |
$ mount |
نمایش اطلاعات کلی فضای هارد دیسک
1 2 3 4 5 |
$ df # to see human readable size $ df -h # to see with filesystem $ df -T |
برای دیدن فضای هارد دیسک دو برنامه df و du وجود داره
اگه یک فایل حجیمی رو پاک کردید و دیدید که تاثیری تو فضای گزارش شده توسط df ایجاد نشد دلیلش این هست که یک پروسه داره از اون فایل استفاده میکنه و descriptor فایل باز هست و وقتی شما فایل رو پاک کنید اون فایل همچنان برای پروسه قابل دسترس خواهد بود تا وقتی که اون پروسه بسته بشه، اما برنامه du سیستم گزارش دهیش اینجوری نیست و فقط فایلهای موجود رو حساب میکنه
برای اینکه ببینید چه پروسه ای داره از این فایل استفاده میکنه میتونید از دستور fuser (که تو همین پست توضیح دادم) یا دستور زیر استفاده کنید
1 |
$ lsof | grep deleted |
نمایش اطلاعات فضای دایرکتوری ها
1 2 3 4 5 |
# -h to see human readable size # -s to show only summary $ du -sh /home # get disk usage of all super directories of `/` $ du -sh /* |
dd
این ابزار برای کپی بایت به بایت اطلاعات استفاده میشه
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# 'if' is source # 'of' is destination $ dd if=/ev/sda1 of=/dev/sdb1 # to flush disk buffer cache, you should sync dd command like $ dd if=/ev/sda1 of=/dev/sdb1 conv=fdatasync # or $ dd if=/ev/sda1 of=/dev/sdb1 && sync # 'bs' is block size that dd reads everytime # 'count' specifies how many times read 'bs' size # make a dummy file with 1G size $ dd if=/ev/zero of=file bs=1G count=1 # to do a disk write test $ dd if=/dev/zero of=test bs=64K count=16K conv=fdatasync # read test # first purge cache $ echo 3 | tee /proc/sys/vm/drop_caches $ dd if=test of=/dev/null bs=64K count=16K # get flash drive partition label from `lsblk`, here is '/dev/sda' (not /dev/sdaX) $ umount /dev/sda* # if don't unmount you'll get corrupted data $ dd if=image.iso of=/dev/sda && sync |
مشاهده فعالیت پروسس های سیستم بر اساس میزان استفاده از دیسک
1 |
$ iotop |
نمایش برعکس فایل
1 |
$ tac file.txt |
mkfs
ایجاد فایل سیستم (فرمت) برای یک پارتیشن.
1 2 3 4 5 6 7 8 9 10 |
# get drive partition label from `lsblk` # unmount it first $ umount /dev/sdb1 # format it with desired formats with mkfs.vfat, mkfs.ex3, mkfs.ex4 # to set label, pass it with -n # you can format multiple partitions at the same time $ mkfs.vfat /dev/sdb1 /dev/sdb2 # if the partition is less than 4GB, mkfs.vfat creates an fat16 filesystem. Is the partition more than 4 GB, it creates fat 32. # to check disk information type $ df -TH |
fsck
این ابزار برای چک کردن سلامت هارد و اصلاح خطاهای اون در صورت وجود استفاده میشه.
به وصورت پیش فرص وقتی یک از دروایوهای تعرفی شده در /etc/fstab به صورت unclean میشه یا تعداد دفعات مونت شدنش از یه تعداد بیشتر میشه این برنامه اتوماتیک اجرا میشه
1 2 3 4 5 6 7 |
# -p autorepair (same as -y and -a) # -M Skip mounted file systems, cheking mounted partitions is risky # -A Check all disks listed in /etc/fstab # -R If using the -A option, do not check the root filesystem # first unmount partition, you can get drive label with lsblk $ umount /dev/sdb1 $ fsck -Ma /dev/sdb1 |
smartctl
با این ابزار میتونید مشخصات S.M.A.R.T هارد رو ببنید
1 2 3 4 5 6 |
# smartctl is in smartmontools package # -i devie short information # -a all SMART information # -x all SMART and non-SMART information # -H device health status $ smartctl -x /dev/sda |
ارسال درخواست GET با telnet
1 2 3 4 5 6 7 8 |
$ telnet example.com 80 Trying 192.168.252.10... Connected to example.com. Escape character is '^]'. # type below lines GET /questions HTTP/1.0 Host: example.com # press enter |
ایجاد یک سرور با netcat
1 2 3 4 |
$ nc -l 8000 # in other shell window to send data, type $ nc -v localhost 8000 dummy |
دریافت یک صفحه وب با netcat
1 2 3 4 5 6 |
$ nc -v bestical.ir 80 # press enter GET / HTTP/1.1 # press enter Host: bestical.ir # press enter twice |
دانلود لیستی از لینکها
1 2 3 |
$ xargs -n 1 curl -O < file.txt # or $ wget -i file.txt |
تبدیل انکدینک فایلهای متنی
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# first detect file encoding $ file -i input.txt # input.txt: text/x-c; charset=us-ascii # if iconv reported character set as 'non-iso extended-ascii text' it means it didn't detect correct encoding # try with chardet $ chardet input.txt # input.txt: us-ascii with confidence 0.99 # now convert it # -f is from encoding # -t is to encoding # you can see available encodings by 'iconv -l' $ iconv -f us-ascii -t utf-8 input.txt -o output.txt # you can use two other keywords, when there are problems in converting characters # TRANSLIT: to convrt to similar characters when converting the original character is not possible # IGNORE: to ignore character when converting is not possible $ iconv -f utf-8 -t windows-1252//TRANSLIT -o output.txt |
نمایش انتهای فایل و دیدن اطلاعات جدید اضافه شده
1 |
$ less +F file |
ls
لیست کردن محتویات یک دایکتوری
1 2 3 4 5 6 7 |
# -l use long listing format # -a show all entries including those starting with '.' # -F show '/' in front of directories and '@' in front of symbolik links # -n show uid and gid in numbers # -U don't sort alphabetically, show in dirctory order # -t sort by modification date $ ls -Fh |
chown
تعیین مالکیت فایلها و دایرکتوری ها
1 2 3 4 5 6 7 8 9 10 |
# change user $ chown user dir # you can pass multiple directories or files $ chown user dir1 file file2 # change user and group $ chown user:group dir # -R to do recursively $ chown -R user:group dir # change only group $ chown :group dir |
تعیین سطح دسترسی
1 2 3 4 5 6 7 8 9 |
# 1=> execute, 2=> write, 4=> read # set 777 permission to file $ chomod 777 file # or $ chmod u=rwx,g=rwx,o=rwx file # you can pass multiple directories or files $ chmod 777 file dir1 # -R to do recursively $ chmod 777 dir -R |
ماشین حساب
1 |
$ echo 5 | bc |
چاپ یک رشته به صورت تکراری تا زمانی که شما متوقفش کنید
1 |
yes 'hello' |
فرستادن یک پیام به ترمینال کاربران لوگین شده
1 |
$ wall hello |
پاک کردن کش
1 2 3 4 5 6 7 8 |
# 1=> pagecache (memory cache) # 2=> dentries and inodes cache (diretories and files structure cache) # 3=> both $ echo 3 > /proc/sys/vm/drop_cache # to destroy swap, you should disable it then enable it # -a means all $ swapoff -a $ swapon -a |
ساخت swap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# here we make a 1G swap space on file, you can use a partition instead # first create a 1G file, skip it if using a dedicated partitio for swap $ dd if=/dev/zero of=file.swp bs=1G count=1 && sync # make filesystem $ mkswp file.swp # enable it $ swapon file.swp # to disable it $ swapoff file.swp # to disable all swaps $ swapoff -a |
which
پیدا کردن فایل اجرایی یک دستور
1 2 3 4 5 6 7 |
$ which less # => /usr/bin/less # to get all matched files use -a $ which -a less # the /usr/bin/less is symlink of /bin/less # => /usr/bin/less # /bin/less |
whereis
پیدا کردن فایل اجرایی، دستورالعمل ها و سورس ها
این دستور شبیه به which هست. شما یک ورودی به این برنامه میدید و اون تو فایهای اجرایی، دستورالعمل ها و سورس ها اون رو جستجو میکنه
1 2 3 4 5 6 7 8 |
# -b search only for binaries # -m search only for manuals # -s search only for sources # -l output effective lookup paths $ whereis less # => less: /usr/bin/less /bin/less /usr/share/man/man1/less.1.gz $ whereis -b less # => less: /usr/bin/less /bin/less |
shutdown
خاموش کردن سیستم
1 2 3 4 5 6 |
# power off system now $ shutdown now # power off system in 15 minutes later $ shutdown +15 # cancel previous command $ shutdown -c |
mv
انتقال و یا تغییر نام فایل و دایرکتوری
1 2 3 4 5 6 7 8 9 |
# -f or --force don't propmt before overwrite # -b or --no-clobber do not overwrite existing files # move dir1 directory to /home/new # if /home/new is not exists, below command will rename dir1 to new and move it in /home $ mv dir1 /home/new # rename dir1 to dir2 $ mv dir1 dir2 # overwriting $ mv -f dir1 /home/new |
cp
کپی فایل و دایرکتوری
1 2 3 4 5 6 7 8 9 10 11 |
# -f, --force if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used) # -b, --no-clobber do not overwrite existing files # -r copy directories # -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing # -i, --interactive prompt before overwrite (overrides a previous -n option) # in major linux distributions 'cp' command is aliased to 'cp -i', to ignore this alias call cp with \ like '\cp' # cp default behaviour is to replace existing files # copy dir1 directory to /home/new # if /home/new is not exists, below command will copy dir1 to /home and rename it to new $ cp -r dir1 /home/new |
date
نمایش تاریخ
1 2 3 4 5 6 |
# --iso-8601 or -I show in iso8601 format # --rfc-email or -R show in rfcemail format $ date -I # 2019-10-25 $ date -R # Fri, 25 Oct 2019 08:20:38 +0330 |
sed
این برنامه برای تغییر یک رشته خاص در فایلها مورد استفاده قرار میگیره
1 2 3 4 5 6 7 |
# to replace a string # -i to do replace in place $ sed -i 's/one/two/g' file.txt # to delete a string $ sed -i 's/one//g' file.txt # batch replace with sed and find $ find . -type f -exec sed -i 's/one/two/g' {} \; |
split
تبدیل یک فایل به قسمت های کوچکتر
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# -b to convert a file to specific size, valid scales are K for kilobytes, M for megabytes and G for gigabytes $ to split to 2 gigabyte parts use -b 2000M or 2000000K or 2000000000 $ split file -b 200M # the output is a series of files with alphabetical order # xaa xab xac xad xae # -n to split to specific number of chunks $ split file -n 5 # xaa xab xac xad xae # -d to have files with numeric prefix $ split file -b 200M -d # x00 x01 x02 x03 x04 # if you want to make output file have specific prefix, add it to end of the command $ split file -b 200M part_ # part_aa part_ab part_ac part_ad part_ae # to recreate original file, concat parts by cat # because parts are in alphabetical order, cat weill get them in that order too $ cat * > file |
تنظیم ویرایشگر فایل پیشفرض
1 2 3 4 5 6 7 |
# this will change default editor when changing systemctl unit files or crontab or ... # CentOS $ export EDITOR=/use/bin/vim # Debian $ update-alternatives --config editor |
ایجاد یک فضای ذخیره سازی tmpfs
tmpfs یک فضای ذخیره سازی موقت در رم هست که همونطور که مشخصه سرعت بالایی داره
1 2 3 |
$ mkdir /mnt/test $ mount -t tmpfs -o size=400m tmpfs /mnt/node_a # all the data in /mnt/test will be transferred to ram |
iperf3
این برنامه برای سنجش میزان پهنای باند (سرعت) شبکه استفاده میشه
1 2 3 4 5 6 7 8 |
# on server that we want to test $ iperf3 --server --port 3333 # on client $ iperf3 --client SERVER_IP --port 3333 # to simulate mutiple clients (connections) use '--parallel NUMBER_OF_CLIENTS' $ iperf3 --client SERVER_IP --port 3333 --parallel 10 |