A list of commands for tasks and troubleshooting, commands will work with Debian and Ubuntu distributions.
Be sure to read the flags underneath each command. It may not always be necessary to use all of the flags. Only use the options which apply to the use case.
/etc/apt/sources.list
- This is where apt searches for repos for packages. To add a new package to the repo list create a new file called packagename.list.d. This will have apt search any file with .list extension
sudo apt update && sudo apt install
- Use && to run 2 commands, if the first one fails the second one will not run
dpkg --get-selections > packages.list
sudo apt install dselect
- This software is used to install all packages which have been exported with dpkg
sudo dselect update
sudo dpkg --set-selections < packages.list
sudo apt-get dselect-upgrade
sudo apt update
sudo apt upgrade
sudo apt install -y packagename
- y - Automatically say yes to requests to install packages
sudo apt remove --purge packagename
- purge - Remove package and configuration
sudo apt search packagename
sudo apt-cache show packagename
sudo snap install packagename
- Snap is a newer package type, it is independent of system libraries which allows upgrades of software packages to be independent of OS upgrades
sudo snap find packagename
which packagename
sudo snap refresh packagename
sudo snap remove
locate packagename
tar -xzvf data.tgz -C /tmp
- x - Extract file
- z - Deal with compressed file i.e. filter the archive through gzip
- v - Verbose output i.e. show progress
- f - File, work on data.tgz file.
- C - /path/to/file
- t - List all files stored in the archive
sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"
pidof vim
- Shows process ID of the vim program
ps -aux
- add | grep programme to reduce results to the package you are looking for
ps u -C string
CTRL + Z
fg
- Add a number after fg if there are multiple terminals which have been minimized
Check htop program in useful program list
This software gives a full list of available and installed packages. It offers a GUI in the terminal to manage packages
sudo pkill -9 programname
- 9 - Send immediate terminate command
- Without 9 the standard SIGTERM is sent to the program
sudo kill 90000
systemctl
- Use sudo if you want access to info only root user can see
systemctl status -l ssh
- This shows the status of the ssh daemon
- l - Shows full list of status
sudo systemctl start / stop / restart / reload ssh
- This starts, stops, restart or reload the ssh daemon. Restart or reload are only available with some units
sudo systemctl enable / disable ssh
- disable or enable ssh daemon
free -m
Crontab is a task scheduling tool. Layout of the task in the cron file is as follows
m h dom mon dow path/to/command
Symbol | Meaning |
---|---|
m | minute |
h | hour (0-23) |
dom | day of month |
mon | month |
dow | day of week (0-6) |
eg.
4 0 * * 4 /home/user/cleanup.sh
- This task will run Friday at 12:03am
- Always use for path to file
crontab -l
crontab -e
/ect/hostname
/etc/hosts
/etc/netplan/01-netcfg.yaml
/etc/nsswitch.conf
- Within the hostname folder is where the hostname of the current machine is
- The hosts folder contains a list of the hostnames with IP addresses to resolve those names to
- Within the netplan directory are config files for network devices and their addresses. The files containing the configurations can be called 01-netcfg.yaml or 50-cloud-init.yaml
- nsswith.conf file determines order in which the machine checks DNS
sudo hostnamectl set-hostname my.new.hostname
ip addr show
ifconfig -a
curl ifconfig.me
curl ident.me
journalctl | grep -Ei 'dhcp' | tail -n30
netplan ip leases ens5
sudo ip link set enp0s3 up / down
sudo systemctl restart network.service
systemd-resolve --status | grep DNS\ Servers
sudo netstat -tulpn
- l - Display only listening sockets
- t - Display TCP connection
- n - Display addresses in a numerical form
- p - Display process ID/ Program name
- u - Display UDP connections
- c - Continuous
sudo lsof -i -P -n | grep LISTEN
sudo netstat -tulpn | grep LISTEN
sudo lsof -i:22 ## see a specific port such as 22 ##
sudo nmap -sTU -O IP-address-Here
Create file ~/.ssh/config inside the file add the following
host myserver
Hostname 192.0.0.1
Port 22
User username
IdentityFile ~/.ssh/targaryen.key
ServerAliveInterval seconds to ping remote server
- Adding a configuration file allows automatic use of settings when connecting to specific host
ssh -p 30 user@10.0.0.1
- p - Specify which port to use, by default ssh traffic is over port 22
ssh-keygen -p
- p - create passphrase with key
eval "$(ssh-agent)"
ssh-add /path/to/key
ssh-copy-id -i ~/.ssh/id_rsa.pub fortress
- This copies the key to server named fortress
scp /path/to/file user@target:/path/to/target
ssh root@MachineB 'bash -s' < local_script.sh
Shell scripting is the act of writing commands into a text file which can be run. These commands are then executed by the terminal line by line. When creating a script it is important to change the mode of the file to be executable
chmod +x /path/to/file
sample script sample-script.sh
/home/user/.bashrc
- This is where configuration settings for the bash terminal are. If any variables or aliases are added to this file they will be initialized when a user opens a terminal
cd -
env
history
- Run the command by typing !numberofcommand
history -d numberofcommand
sudo !!
echo $variablename
echo !?
alias install='sudo apt install'
- This allows you to set up custom commands
export VARIABLE=something
sudo tail n10 -f /path/to/file
- n - Number of lines
- f - Keep file open, view logs files live
find -name path/to/search filename
- -name - Name of file
- -type f - Type is file
- -type d - Type of directory
- 03 - Efficient use of resources and likelihood
ls -s originalfile linkedfile
- This command create a soft symbolic link to the first file, the second file can be moved around anywhere and manipulated. When it is open it points to the original file. It is similar to a shortcut on Windows OS
mkdir /path/to/dir
cat /path/to/file - Display the contents of file
more /path/to/file - Browse through text file
less /path/to/file - More features than more
head -n10 /path/to/file - Output the beginning or top portion of file
tail -n10 /path/to/file - Output the ending or bottom portion of the file
- Number of lines to be displayed when using the head or tail command
cat /etc/shadow | grep myuser
- This will only show information regarding the user which you grepped for
rm -rf /path/to/dir
- r - Recursively remove all files
- f - Forcefully (be careful when using this variant)
touch /path/to/file
Permissions dictate who has access to what files, the permissions are broken into 3 groups. The user, group and other. Permissions can exist of both directories and on files. Another name for permissions is mode
- Permissions on a directory can effect files in the directory
- When having an issue with a file check directory permissions
- Work your way up to root
Symbol | Octal | Permission |
---|---|---|
-rwx------ | 700 | Only owner can read write and Execute |
-rwxr-xr-x | 755 | Everyone on system can execute but only user can edit file |
-rw-rw-r-- | 664 | User read and write, Group read and write, other only read |
-rw-rw---- | 660 | Only user and group can read and write file |
-rw-r--r-- | 644 | User read and write, group and other only read |
r | w | x |
---|---|---|
4 | 2 | 1 |
Compared | |||
---|---|---|---|
Symbolic | rwx | r-x | r-- |
Binary | 111 | 101 | 100 |
Decimal | 7 | 5 | 4 |
chmod 770 -R mydir
- R - Change all sub files and folders
sudo chown -R username file.txt
- R - Recursively change the permissions
sudo chown user:group file.txt
/etc/password
/etc/shadow
- The password folder contains all user info
- The shadow folder contains password information of users
su username
- Exclude the username if you wish to switch to root user
sudo adduser username
- This command is a script for the useradd command
sudo -r userdel username
- r - Remove all user data
- If not using r flag the user directory is not deleted. Be sure to move old user home directory to the archive
sudo usermod -d /new/dir username -m
sudo usermod -l oldname newname
sudo passwd -l username
- l - Lock account
- u - Unlock account
sudo chage -l username
- l - List details
- d 0 - Set number of days to expire to 0 this will disable account
- M 90 - Set number of days after which a user needs to replace their password
- m - Set minimum number of days for password to be active, good if someone keeps changing their password back
Plugable Authentication Module
Install this application to set minimum requirements for passwords. This increases password strength within the machine
/etc/group
- This file shows all groups on the machine
/etc/sudoers
sudo EDITOR=vim visudo
- This file contains all the users who have sudo privileges, edit this folder with the following command
- This allows for the detection of any mistakes made in the sudoers file
sudo groupadd newgroup
sudo groupdel
sudo usermod -aG group user
- a - Append a secondary group, if you don't add this flag it will replace all current groups
- G - This states a secondary group to add the user to
- aG sudo - This adds a user to the sudoers group. this allows the user to use the sudo command
sudo usermod -g groupname username
sudo gpasswd -d username grouptoremove
sudo apt install ncdu
- This program allows a person to navigate though the directory tree while viewing disk usage
df -h
- h - Shows usage in human readable form
df -i
- i - Shows free inodes
du -hsc *
- h - Shows human readable disk usage
- s - Summary
- c - Current working directory
/etc/fstab
- This folder lists all active file storage devices on the machine. It also shows where devices should be mounted on startup
sudo fdisk -l
lsblk
blkid
sudo mount -a
- a - Automatically mount all available disks
This software is used to manage packages, offers a terminal GUI to navigate packages
sudo apt install aptitude
sudo aptitude unmarkauto packagename
sudo aptitude
aws conformation validate-template --template-body file://sampletemplate.json
aws cloudfront create-invalidation --distribution-id 000000 --paths "/*"
aws s3 cp directory_to_copy/ s3://bucket_name/ --recursive
- --acl public-read : Creates objects with public access
aws s3 rm s3://bucket_name/ --recursive
This program is used to keep a shell terminal running on a remote machine once the connection is lost. It is useful when setting up network connections and having to restart the connection kicks you out of the system. The command will still keep running on the remote machine
sudo apt install tmux
tmux
[ctrl +b] + c
[ctrl + b] $tab_number
[ctrl + b] + [
This program is best used to display current running processes. It offers a terminal GUI to navigate processes.
sudo apt install htop
Nginx is software to serve websites from a machine. It can also act as a reverse proxy for other services.
/etc/nginx/nginx.conf
/etc/nginx/conf.d/
/etc/nginx/sites-enabled/
- nginx.conf file is where main system settings for nginx are configured, this file includes paths to directories which contain configurations for other servers
- conf.d/ this directory is where a user should put all other servers which will be hosted on the machine. The extension should be conf
- sites-enable - this is the old directory where configuration settings are installed. A user should remove the default server file
sample configuration file mywebserver.conf
sample Nginx and JS file configuration
sudo apt install nginx
This software is able to run programs on remote machines, restart them if they go down or send alerts if there is an issue with an application
http://supervisord.org/configuration.html#supervisorctl-section-settings
/etc/supervisor/conf.d/
- Any files stored on this directory with the extension .conf will be run when the supervisor command is run
sample configuration file gunicorn-supervisor.conf
sudo apt install supervisor
This software is to run a WSGI interface for python websites, in particular it is used for Django websites
https://docs.gunicorn.org/en/stable/settings.html
sudo apt install gunicorn
sample configuration file gunicorn.config.py
Python package manager for PyPi
https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/
python -m build
twine upload build/*
- --skip-existing : Allows a new version to be uploaded