Useful Linux Command Line Stuff: Difference between revisions
Jump to navigation
Jump to search
(26 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
Most of the Information below is from the 2020 Canonical Ubuntu Server Pro Tips guide (no longer available) | |||
{| class="wikitable" | |||
= | |'''In development:''' This page is very much in development and needa much work - send any suggestions to us via the normal channels! | ||
Note that some shells and distributions may vary in their syntax and so some commands may not work or be available (by default). Always read the relevant documentation for your distro before working on a live machine. | |||
: | |} | ||
==Commands to Manipulate Files== | |||
==Files== | |||
====List files==== | ====List files==== | ||
Line 53: | Line 13: | ||
:<code>ls -al</code> | :<code>ls -al</code> | ||
====create empty==== | ====create empty==== | ||
:<code>touch <filename></code> | :<code>touch <filename></code> | ||
Line 82: | Line 41: | ||
====Find files modified in the last n minutes==== | ====Find files modified in the last n minutes==== | ||
:<code>find <directory> -mmin -<n> -type f</code> | :<code>find <directory> -mmin -<n> -type f</code> | ||
:eg. <code>find . -mmin -5 -type f</code> | ::eg. <code>find . -mmin -5 -type f</code> | ||
====Show only the nth column==== | ====Show only the nth column==== | ||
:<code>col<n> “<separator>” <filename></code> | :<code>col<n> “<separator>” <filename></code> | ||
Line 96: | Line 56: | ||
====Pack a directory into an archive==== | ====Pack a directory into an archive==== | ||
:zip - <code>zip -r <target> <source dir></code> | :zip - <code>zip -r <target> <source dir></code> | ||
:tar.gz - <code>tar cvzf <target>.tar.gz <source dir></code> | |||
====Unpack an archive==== | ====Unpack an archive==== | ||
:<code> | :zip - <code>unzip <zip file></code> | ||
:tar.gz - <code>tar xf <tar.gz file></code> | |||
====Copy file to remote server==== | ====Copy file to remote server==== | ||
:<code>scp <filename> <user@server>====<destination></code> | :<code>scp <filename> <user@server>====<destination></code> | ||
:eg. <code>scp config.yaml admin@192.0.0.0====/config</code> | ::eg. <code>scp config.yaml admin@192.0.0.0====/config</code> | ||
====Copy directory recursively from remote server==== | ====Copy directory recursively from remote server==== | ||
:<code>scp -r <user@server>====<source> <destination></code> | :<code>scp -r <user@server>====<source> <destination></code> | ||
:eg. <code>scp -r admin@192.0.0.0====/config /tmp</code> | ::eg. <code>scp -r admin@192.0.0.0====/config /tmp</code> | ||
==Networking== | |||
====Get the IP address of all interfaces==== | |||
:<code>networkctl status</code> | |||
====Display all IP addresses of the host==== | |||
:<code>hostname -I</code> | |||
====Enable/disable interface==== | |||
:<code>ip link set <interface> up</code> | |||
:<code>ip link set <interface> down</code> | |||
====Manage firewall rules==== | |||
:enable firewall: <code>sudo ufw enable</code> | |||
:list rules: <code>sudo ufw status</code> | |||
:allow port: <code>sudo ufw allow <port></code> | |||
:deny port: <code>sudo ufw deny <port></code> | |||
====Connect remotely through SSH==== | |||
:<code>ssh <user>@<host IP></code> | |||
==Packages== | |||
====Search for packages==== | |||
:<code>apt search <string></code> | |||
:<code>snap find <string></code> | |||
====List available updates==== | |||
:<code>apt list --upgradable</code> | |||
====Apply all available updates==== | |||
:<code>sudo apt update && sudo apt upgrade</code> | |||
====Install from the Ubuntu archive:==== | |||
:<code>sudo apt install <package></code> | |||
====Install from the snap store:==== | |||
:<code>sudo snap install <package></code> | |||
====Which package provides this file?==== | |||
:<code>sudo apt install apt-file</code> | |||
:<code>sudo apt-file update</code> | |||
:<code>apt-file <filename or command></code> | |||
==Security== | ==Security== | ||
Show which users are logged in: <code>w</code> | ====Show which users are logged in==== | ||
Get password expiration date for <user>: <code>chage -l <user></code> | :<code>w</code> | ||
Set password expiration date for <user>: <code>sudo chage <user></code> | ====Get password expiration date for <user>==== | ||
Lock a user account: <code>sudo passwd -l <user></code> | : <code>chage -l <user></code> | ||
Unlock a user account: <code>sudo passwd -u <user></code> | ====Set password expiration date for <user>==== | ||
List open ports and associated processes: <code>sudo netstat -tulpn</code> | : <code>sudo chage <user></code> | ||
====Lock a user account==== | |||
: <code>sudo passwd -l <user></code> | |||
====Unlock a user account==== | |||
: <code>sudo passwd -u <user></code> | |||
====List open ports and associated processes==== | |||
: <code>sudo netstat -tulpn</code> | |||
Automatically detect and ban abusive IP addresses: <code>sudo apt install fail2ban</code> | ====Automatically detect and ban abusive IP addresses==== | ||
Show banned IP addresses | :<code>sudo apt install fail2ban</code> | ||
====Show banned IP addresses==== | |||
:<code>sudo fail2ban-client status</code> | |||
:<code>sudo fail2ban-client status <jail></code> | |||
Get the support status for installed packages: <code>ubuntu-support-status</code> | ====Get the support status for installed packages==== | ||
: <code>ubuntu-support-status</code> | |||
Enable kernel live patching: | ====Enable kernel live patching: ==== | ||
:<code>sudo snap install canonical-livepatch</code> | :<code>sudo snap install canonical-livepatch</code> | ||
:<code>sudo canonical-livepatch enable <token></code> | :<code>sudo canonical-livepatch enable <token></code> | ||
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. | ||
==System== | ==System== | ||
Line 171: | Line 159: | ||
==Kubernetes and containers== | ==Kubernetes and containers== | ||
Install MicroK8s and list available add-ons | ====Install MicroK8s and list available add-ons==== | ||
sudo snap install microk8s --classic | :<code>sudo snap install microk8s --classic</code> | ||
microk8s.status --wait-ready | :<code>microk8s.status --wait-ready</code> | ||
Enable a MicroK8s add-on | ====Enable a MicroK8s add-on==== | ||
microk8s.enable <service> | :<code>microk8s.enable <service></code> | ||
View MicroK8s nodes and running services | ====View MicroK8s nodes and running services==== | ||
microk8s.kubectl get nodes | :<code>microk8s.kubectl get nodes</code> | ||
microk8s.kubectl get services | :<code>microk8s.kubectl get services</code> | ||
====Launch a LXD container==== | |||
Launch a LXD container | :<code>lxd init</code> | ||
lxd init | :<code>lxc launch ubuntu:18.04 <container name></code> | ||
lxc launch ubuntu:18.04 <container name> | ====Or another distro==== | ||
Or another distro | :<code>lxc launch images:centos/8/amd64 <container name></code> | ||
lxc launch images:centos/8/amd64 | ====Get a shell inside a LXD container==== | ||
<container name> | :<code>lxc exec <name> -- /bin/bash</code> | ||
Get a shell inside a LXD container | ====Push a file to a LXD container==== | ||
lxc exec <name> -- /bin/bash | :<code>lxc file push <filename> <container name>/<path></code> | ||
Push a file to a LXD container | ====Pull a file from a LXD container==== | ||
lxc file push <filename> | :<code>lxc file pull <destination> <container name>/<file path></code> | ||
<container name>/<path> | |||
Pull a file from a LXD container | |||
lxc file pull <destination> | |||
<container name>/<file path> | |||
==Virtualisation== | ==Virtualisation== | ||
Install Multipass and launch | ====Install Multipass and launch an Ubuntu VM==== | ||
an Ubuntu VM | :<code>sudo snap install multipass --classic</code> | ||
sudo snap install multipass --classic | :<code>multipass launch <image> --name <VM name></code> | ||
multipass launch <image> --name <VM name> | ::Omitting <image> will launch a VM withthe latest Ubuntu LTS | ||
Omitting <image> will launch a VM | ====Find available images==== | ||
:<code>multipass find</code> | |||
Find available images | |||
multipass find | |||
List existing VMs | ====List existing VMs==== | ||
multipass list | :<code>multipass list</code> | ||
Get a shell inside a VM | ====Get a shell inside a VM==== | ||
multipass shell <VM name> | :<code>multipass shell <VM name></code> | ||
==OpenStack== | ==OpenStack== | ||
Install OpenStack and launch | ====Install OpenStack and launch an instance==== | ||
an instance | :<code>sudo snap install microstack --classic</code> | ||
sudo snap install microstack --classic | :<code>sudo microstack.init</code> | ||
sudo microstack.init | :<code>microstack.launch</code> | ||
microstack.launch | ::The Horizon dashboard is available at <code>10.20.20.1</code> | ||
The Horizon dashboard is available at 10.20.20.1 | ::Default credentials: <code>admin / keystone</code> | ||
Default credentials: admin / keystone | |||
Latest revision as of 12:57, 10 July 2024
Most of the Information below is from the 2020 Canonical Ubuntu Server Pro Tips guide (no longer available)
In development: This page is very much in development and needa much work - send any suggestions to us via the normal channels!
Note that some shells and distributions may vary in their syntax and so some commands may not work or be available (by default). Always read the relevant documentation for your distro before working on a live machine. |
Commands to Manipulate Files
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>
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
- eg.
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
- eg.
Copy directory recursively from remote server
scp -r <user@server>====<source> <destination>
- eg.
scp -r admin@192.0.0.0====/config /tmp
- eg.
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>
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
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
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>
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 withthe latest Ubuntu LTS
Find available images
multipass find
List existing VMs
multipass list
Get a shell inside a VM
multipass shell <VM name>
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
- The Horizon dashboard is available at