Useful Linux Command Line Stuff: Difference between revisions

From Ideasplace
Jump to navigation Jump to search
mNo edit summary
Line 52: Line 52:
*<code>ls -al</code>
*<code>ls -al</code>


====Common file operations
====Common file operations====
*create empty: touch <filename></code>
*create empty: <code>touch <filename></code>
create with content: echo "<content>" > <filename></code>
*create with content: <code>echo "<content>" > <filename></code>
append content: echo "<content>" >> <filename></code>
*append content: <code>echo "<content>" >> <filename></code>
display a text file: cat <file></code>
*display a text file: <code>cat <file></code>
copy: cp <file> <target filename></code>
*copy: <code>cp <file> <target filename></code>
move/rename: mv <file> <target directory/filename></code>
*move/rename: <code>mv <file> <target directory/filename></code>
delete: rm <file></code>
*delete: <code>rm <file></code>
Create a directory: mkdir <directory></code>
*Create a directory: <code>mkdir <directory></code>
Create directories recursively: mkdir -p <directory1>/<directory2></code>
*Create directories recursively: <code>mkdir -p <directory1>/<directory2></code>
Delete a directory recursively: rm -r <directory></code>
*Delete a directory recursively: <code>rm -r <directory></code>
Quick file search: locate <q></code>
*Quick file search: <code>locate <q></code>
Search string in file: grep <string> <filename></code>
*Search string in file: <code>grep <string> <filename></code>
Search string recursively in directory: grep -Iris <string> <directory></code>
*Search string recursively in directory: <code>grep -Iris <string> <directory></code>
Find files modified in the last n minutes: find <directory> -mmin -<n> -type f</code>
*Find files modified in the last n minutes: <code>find <directory> -mmin -<n> -type f</code>
eg. find . -mmin -5 -type f
eg. find . -mmin -5 -type f
Show only the nth column: col<n> “<separator>” <filename></code>
Show only the nth column: <code>col<n> “<separator>” <filename></code>
eg. col2 “,” foo.csv
eg. col2 “,” foo.csv
Display file paginated: less <filename></code>
Display file paginated: <code>less <filename></code>
Display first n lines: head -n <n> <filename></code>
Display first n lines: <code>head -n <n> <filename></code>
Display last n lines</code>
Display last n lines: <code>tail -n <n> <filename></code>
tail -n <n> <filename></code>
Follow file content as it increases: <code>tail -f <filename></code>
Follow file content as it increases
Pack a directory into an archive:
tail -f <filename></code>
*<code>zip: zip -r <target> <source dir></code>
Pack a directory into an archive
*<code>tar.gz: tar cvzf <target>.tar.gz <source dir></code>
zip: zip -r <target> <source dir></code>
*Unpack an archive:
tar.gz: tar cvzf <target>.tar.gz <source dir></code>
**<code>zip: unzip <zip file></code>
Unpack an archive
**<code>tar.gz: tar xf <tar.gz file></code>
zip: unzip <zip file></code>
*Copy file to remote server: <code>scp <filename> <user@server>:<destination></code>
tar.gz: tar xf <tar.gz file></code>
**eg. scp config.yaml admin@192.0.0.0:/config
Copy file to remote server
*Copy directory recursively from remote server: <code>scp -r <user@server>:<source> <destination></code>
scp <filename> <user@server>:<destination></code>
**eg. scp -r admin@192.0.0.0:/config /tmp
eg. scp config.yaml admin@192.0.0.0:/config
Copy directory recursively from remote server
scp -r <user@server>:<source> <destination></code>
eg. scp -r admin@192.0.0.0:/config /tmp


Security
==Security==
Show which users are logged in
Show which users are logged in: <code>w</code>
w
Get password expiration date for <user>: <code>chage -l <user></code>
Get password expiration date for
Set password expiration date for <user>: <code>sudo chage <user></code>
<user>
Lock a user account: <code>sudo passwd -l <user></code>
chage -l <user>
Unlock a user account: <code>sudo passwd -u <user></code>
Set password expiration
List open ports and associated processes: <code>sudo netstat -tulpn</code>
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
*Automatically detect and ban abusive IP addresses: <code>sudo apt install fail2ban</code>
abusive IP addresses
*Show banned IP addresses:
sudo apt install fail2ban
**<code>sudo fail2ban-client status</code>
Show banned IP addresses
**<code>sudo fail2ban-client status <jail></code>
sudo fail2ban-client status
 
sudo fail2ban-client status <jail>
Get the support status for installed packages: <code>ubuntu-support-status</code>
Get the support status for installed
 
packages
Enable kernel live patching:
ubuntu-support-status
*<code>sudo snap install canonical-livepatch</code>
Enable kernel live patching
*<code>sudo canonical-livepatch enable <token></code>
sudo snap install canonical-livepatch
 
sudo canonical-livepatch enable <token>
Visit ubuntu.com/livepatch to get a free token for up to 3 machines.
Visit ubuntu.com/livepatch to get a
free token for up to 3 machines.





Revision as of 10:44, 10 July 2024

From the Ubuntu Server Pro Tips guide:

Networking

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

Manage firewall rules

  • enable firewall: sudo ufw enable
  • list rules: sudo ufw status
  • allow port: sudo ufw allow <port>
  • deny port: sudo ufw deny <port>

Connect remotely through SSH

  • ssh <user>@<host IP>


Packages

Search for packages

  • apt search <string>
  • snap find <string>

List available updates

  • apt list --upgradable

Apply all available updates

  • sudo apt update && sudo apt upgrade

Install from the Ubuntu archive:

  • sudo apt install <package>

Install from the snap store:

  • sudo snap install <package>

Which package provides this file?

  • sudo apt install apt-file
  • sudo apt-file update
  • apt-file <filename or command>

==Files

List files

  • ls

List files with permissions and dates

  • ls -al

Common file operations

  • 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>
  • Create a directory: mkdir <directory>
  • Create directories recursively: mkdir -p <directory1>/<directory2>
  • Delete a directory recursively: rm -r <directory>
  • Quick file search: locate
  • Search string in file: grep <string> <filename>
  • Search string recursively in directory: grep -Iris <string> <directory>
  • Find files modified in the last n minutes: find <directory> -mmin -<n> -type f

eg. find . -mmin -5 -type f Show only the nth column: col<n> “<separator>” <filename> eg. col2 “,” foo.csv Display file paginated: less <filename> Display first n lines: head -n <n> <filename> Display last n lines: tail -n <n> <filename> Follow file content as it increases: 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

Security

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>

Get the support status for installed packages: ubuntu-support-status

Enable kernel live patching:

  • sudo snap install canonical-livepatch
  • sudo canonical-livepatch enable <token>

Visit ubuntu.com/livepatch to get a free token for up to 3 machines.


System 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> Get all running services systemctl --state running Start or stop a service service <service> start/stop Monitor new logs for a service journalctl -u <service> --since now -f

System 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> Get all running services systemctl --state running Start or stop a service service <service> start/stop Monitor new logs for a service journalctl -u <service> --since now -f


Kubernetes and containers Install MicroK8s and list available add-ons sudo snap install microk8s --classic microk8s.status --wait-ready Enable a MicroK8s add-on microk8s.enable <service> View MicroK8s nodes and running services microk8s.kubectl get nodes microk8s.kubectl get services More MicroK8s help at microk8s.io/docs Launch a LXD container lxd init lxc launch ubuntu:18.04 <container name> Or another distro lxc launch images:centos/8/amd64 <container name> Get a shell inside a LXD container lxc exec <name> -- /bin/bash Push a file to a LXD container lxc file push <filename> <container name>/<path> Pull a file from a LXD container lxc file pull <destination> <container name>/<file path> More LXD help at linuxcontainers.org/lxd

Virtualisation Install Multipass and launch an Ubuntu VM sudo snap install multipass --classic multipass launch <image> --name <VM name> Omitting <image> will launch a VM with the latest Ubuntu LTS Find available images multipass find

List existing VMs multipass list Get a shell inside a VM multipass shell <VM name> More Multipass help at discourse.ubuntu.com

OpenStack Install OpenStack and launch an instance sudo snap install microstack --classic sudo microstack.init microstack.launch The Horizon dashboard is available at 10.20.20.1 Default credentials: admin / keystone More MicroStack help at microstack.run/docs