我的个人博客:https://phoenixnest.github.io/
本篇将介绍有关
Linux终端使用
的一些笔记分享。必要时请使用
sudo
权限,但不建议
所有命令均使用sudo。
参考资料:
# get the IP address of all interfaces
networkctl status
# display all IP addresses of the host
hostname -I
# enable/disable interface
ip link set <interface> up
ip link set <interface> down
# enable firewall:
sudo ufw enable
# list rules:
sudo ufw status
# allow port:
sudo ufw allow <port>
# sudo ufw allow 22
# deny port:
sudo ufw deny <port>
# connect remotely through SSH
ssh <user>@<host IP>
# show which users are logged in
w
# Get password expiration date for <user>
chage -l <user>
# Set password expiration date for <user>
sudo chage <user>
# lock a user account
sudo passwd -l <user>
# unlock a user account
sudo passwd -u <user>
# List open ports and associated processes
sudo netstat -tulpn
# automatically detect and ban abusive IP addresses
sudo apt install fail2ban
# show banned IP addresses
sudo fail2ban-client status
sudo fail2ban-client status <jail>
# visit ubuntu.com/livepatch to get a free token for up to 3 machines.
sudo snap install canonical-livepatch
sudo canonical-livepatch enable <token>
# List files
ls
# List files with permissions and dates
ls -al
# create empty:
touch <filename>
# create with content:
echo "<content>" > <filename>
# append content:
echo "<content>" >> <filename>
# display a text file:
cat <file>
# copy:
cp <file> <target filename>
# move/rename:
mv <file> <target directory/filename>
# delete:
rm <file>
# find files modified in the last n minutes
find <directory> -mmin -<n> -type f
# eg. find . -mmin -5 -type f
# display file paginated
less <filename>
# display first n lines
head -n <n> <filename>
# display last n line
tail -n <n> <filename>
# follow file content as it inncreases
tail -f <filename>
# Pack a directory into an archive
# zip:
zip -r <target> <source dir>
# tar.gz:
tar cvzf <target>.tar.gz <source dir>
# Unpack an archive
# zip:
unzip <zip file>
# tar.gz:
tar xf <tar.gz file>
# Copy file to remote server
scp <filename> <user@server>:<destination>
# eg. scp config.yaml admin@192.0.0.0:/config
# Copy directory recursively from remote server
scp -r <user@server>:<source> <destination>
# eg. scp -r admin@192.0.0.0:/config /tmp
# create a directory
mkdir <directory>
# create directories recursively
mkdir -p <directory1><directory2>...
# delete a directory recursively
rm -r <directory1><directory2>...
# quick file search
locate <q>
# search string in file
grep <string> <filename>
# search string recursively in directory
grep -Iris <string> <directory>
# display kernel version
uname -r
# get disk usage
df -h
# get memory usage
cat /proc/meminfo
# get system time
timedatectl status
# set system timezone
timedatectl list-timezones
sudo timedatectl set-timezone <zone>
# monitor new logs for a service
journalctl -u <service> --since now -f
# get the list of recent logins
last
# display running processes
sudo apt install htop
htop
# get all running services
systemctl --state running
# start or stop a service
service <service> start/stop
# kill process by id
kill <process id>
# kill process by name
pkill <process name>
# run command in the background
<command> &
# display background commands
jobs
# bring command <n> to the foreground
fg <n>