Linux Commands

This is list of common Ubuntu Commands

How to become Superuser
add on Ubuntu Linux

sudo -i elevate permission and login as root account
Root password is different than other password.

User Management ———————————————-
List Users
cut -d: -f1 /etc/passwd
Add a new user you can use:
sudo adduser new_username
Remove/delete
sudo userdel username
Delete the home directory for the deleted user account
sudo rm -r /home/username
Modify the username of a user:
usermod -l new_username old_username
Change the password for a user:
sudo passwd username
Change the shell for a user:
sudo chsh username
Change the details for a user (for example real name):
sudo chfn username
Add a user to the sudo group:
sudo adduser username sudo

Enable Root User ———————————————————————
sudo nano /etc/passwd
sudo nano /etc/shadow
The character “x” represents a blank or empty password.
The character “!” represents that password is locked.

sudo passwd root
sudo passwd -u root
To unlock the account. This should return
passwd: password expiry information changed

Change this as well
sudo nano /etc/ssh/sshd_config
PermitRootLogin prohibit-password change to
PermitRootLogin yes
sudo service ssh restart

Change root password in Ubuntu
Type the following command to become root user and issue passwd:
sudo passwd root
Test it your root password by typing the following command:
sudo -i
su –

Groups ————————————————————————-
List Groups
sudo groups
Get groups where user wpkeepr is member
sudo groups wpkeeper

To get a membership of the sudo group, run the command below
sudo getent group sudo
sudo adduser mustafanadarevic
sudo usermod -aG sudo mustafanadarevic
sudo adduser username
sudo deluser username
sudo addgroup groupname
sudo delgroup groupname
sudo adduser username groupname
sudo adduser hank sudo
passwd user_name
whoami
who
last
history | more
passwd Change Password
sudo addgroup web-admin
sudo delgroup groupname
sudo adduser mustafanadarevic web-admin


Repository ———————————————————
locations
/etc/apt
/etc/apt/sources.list.d/ folder
add-apt-repository ppa:ondrej/php Add PHP repository
sudo add-apt-repository ppa:ondrej/apache2 Apache Repository

List installed apps
sudo apt list php* –installed
sudo apt list mysql* –installed
sudo apt list phpmyadmin* –installed
sudo apt list apache –installed
php -v

Is it upgradable ?
sudo apt list apache** –upgradeable
sudo apt list php** –upgradeable
sudo apt list phpmyadmin* –upgradeable
sudo apt list mysql* –upgradeable

Update Linux ——————————————————————————–
sudo apt-get update Update the package
sudo apt-get upgrade To upgrade the installed packages to their latest versions
sudo apt full-upgrade Remove the installed packages if that is needed to upgrade the whole system.
sudo apt list apache2 -a List all available versions from the Ubuntu repositories
sudo apt list libapache2-mod-auth-openid
sudo apt list | grep package_name Find out whether a specific package is installed
sudo apt list php7* –installed Installed Versions
sudo apt list –upgradeable Check what is upgradeable
apt-get -u upgrade –assume-no List available updates but do not install them

sudo apt-cache search “opera-stable” Search for Available Packages with apt
sudo apt-cache show “opera-stable” Detailed information about the package
apt-cache policy libapache2-mod-auth-openidc
apt-cache policy libcjose0_0.6.1.5
sudo apt-get install opera-stable Install a Package
sudo apt-get install opera-stable –reinstall Reinstall Application
sudo apt upgrade
sudo apt remove opera

Remove Applications Permanently
sudo apt-get remove phpmyadmin
sudo apt-get purge phpmyadmin
sudo apt-get autoremove
apt-get remove phpmyadmin” Command will uninstall the phpmyadmin package
Purge command will remove configuration files related to Ubuntu phpMyAdmin
autoremove command will uninstall all the dependency packages that are no longer required.

App Sources
sudo nano /etc/apt/sources.list Open the sources file
sudo add-apt-repository “https://github.com/zmartzone”
Regular apps Check
sudo apt update
sudo apt list –upgradeable
sudo apt upgrade
Fix broken packages
apt –fix-broken install
sudo apt update
sudo apt install -f
Block package from update
sudo apt-mark hold php*
sudo apt-mark unhold php*
sudo apt-mark showhold
Aptitude
sudo apt install aptitude
sudo aptitude

Remotely Manage Ubuntu Server with SSH
sudo apt-get install openssh-server
sudo nano /etc/ssh/ssh_config #Port 22 MaxAuthTries 4
sudo service ssh status
sudo service ssh start
sudo service ssh stop

Services ——————————————-
Service Locations
ls /etc/systemd/system/
systemctl list-unit-files –type service -all
systemctl list-units –type=service –state=running
systemctl list-dependencies apache2 tree dependence
systemctl –failed
Start Stop Services
systemctl start {service} to start the service
systemctl stop {service} to stop the service
Enable Auto Start
systemctl enable {service}
systemctl disable {service}
Service Status
systemctl status apache2
Update systemd
rerun all generators
reload all unit files, and recreate the entire dependency tree.
sudo systemctl daemon-reload
Check logs for services
sudo journalctl -u goaccess
Run as Deamon
daemon –name=”yourservicename” –output=log.txt sh yourscript.sh
servicename &
start-stop-daemon -SbCv -x your_command

Directories ———————————————————————-
pwd Current Directory
cd .. Up One Folder
cd /etc
cd ../ Root home
ls
ls -l
ls -lh
ls -lah permission list
mkdir software
mv “folder 1″ folder_1” move folder
rm -r foldername remove all
rm folder_* remove folder
rmdir folder remove Folder
mcopy source destination
mkdir directory
df -H –output=source,size,used,avail Check sizes
Create symbolic links
ln -s /usr/share/phpmyadmin /var/www/html/www.com/
It will create link under /var/www/html/www.com/

WinSCP —————————————–
WinSCP to login with “sudo su -“
In the WinSCP login options window:
1. Click Advanced options
2. Go to Environment –> SCP/Shell (on left)
3. Select Shell: “sudo su -“

Files —————————————-
Find Files
sudo find / -name wp-config.php search whole drive
find /mnt/backup/ -name “*.gz” -type f -mtime +4 -delete find and delete file older than 4 days


Logs ——————————————
/var/log/auth.log
/var/log/debug
/var/log/kern.log
/var/log/syslog
sudo nano /var/log/apache2/access.log
sudo nano /var/log/apache2/error.log
/var/log/mysql/error.log
tail -f error_log
timeout 15s tail -f /var/log/syslog
tail -f /var/log/syslog
tail -f /var/log/apache2/access.log | grep 192.168.206.1
tail -f /var/log/apache2/error.log

Journalctl Utility —————————
journalctl -r Newest first
journalctl -f Monitor New Log Messages
journalctl –since “2018-08-30 14:10:10” –until “2018-09-02 12:05:50”
journalctl –since “1 hour ago”
journalctl -b last boot of your server
journalctl -u nginx.service
journalctl -u nginx.service -f Tail
journalctl -b -1 -p “emerg”..”crit” by Prioroty
journalctl -o json-pretty
Short The default option, displays logs in the traditional syslog format.
Verbose Displays all information in the log record structure.
json Displays logs in JSON format, with one log per line.
json-pretty Displays logs in JSON format across multiple lines for better readability.
cat Displays only the message from each log without any other metadata.

down arrow key, enter, e, or j Move down one line.
up arrow key, y, or k Move up one line.
space bar Move down one page.
b Move up one page.
g Go to the first line.
G Go to the last line.
10g Go to the 10th line. Enter a different number to go to other lines.
50p or 50% Go to the line half-way through the output. Enter a different number to go to other percentage positions.
n When searching, go to the next occurrence.
N When searching, go to the previous occurrence.
m Set a mark, which saves your current position. Enter a single character in place of to label the mark with that character.
Return to a mark, where is the single character label for the mark. Note that ‘ is the single-quote.
q Quit less

Up-time and processors ———————————————–
htop
ps
top
cat /proc/cpuinfo
cat /proc/version
shutdown -h now
halt
reboot
pidof apache Check PID of apache process
sudo lsof -i:22 Check is 22 is listening
sudo kill -9 process_id Kill Process using ID

Memory Usage———————————————————–
top
htop

Disk ————————————————————–
Steps to add Disk
lsblk List Disks
findmnt -D List partitions
df -h
sudo fdisk -l list
sudo fdisk /dev/sdd
m help
p print
d delete
n new partition
w write

cd /
sudo mkdir /dynatracetrans Create Mount in Point
lsblk
sudo mkfs.ext4 /dev/sdd
lsblk -f
sudo nano -Bw /etc/fstab Add following lines
UUID=”236a7892-b41e-4dcd-aa54-ea635710fb3f” //dynatrace/data /replayData ext4 defaults,nofail 1 2
sudo mount -a mount all
findmnt list mounting points
lsblk -f list mounting points
sudo restartsudo -i blkid
sudo mount /dev/sdd /mnt/media

Add Swap File to Ubuntu VM in Azure
sudo nano /etc/waagent.conf Config file by setting the following three parameters
ResourceDisk.Format=y
ResourceDisk.EnableSwap=y
ResourceDisk.SwapSizeMB= 4096
service walinuxagent restart
free
dmesg | grep swap
swapon -s
cat /proc/swaps
file /mnt/resource/swapfile
free| grep -i swap

Permissions ————————————————
ls -l list permissions
sudo chown -R dynatrace:dynatrace /dynatrace/data/replayData assign owner right
Enter location of wordpress using varibale
wp=”/var/www/html/sns-shell.com”
Check Permissions and owners
ls -l $wp


Assign Owner Apache
sudo chown -R www-data:www-data /var/www/html/www.com assign owner right to directories and files
Assign user account be owner
chown : -R www-data:www-data /var/www/html/www.com
Assign Permissions
For Directories:
sudo find /var/www/html/sns-shell.com -type d -exec chmod 755 {} \;
For Files:
sudo find /var/www/html/sns-shell.com -type f -exec chmod 644 {} \;
for wp-config
sudo find /var/www/html/sns-shell.com/wp-config.php -type f -exec chmod 660 {} \;

SH file ———————————————-
Run
chmod +x install.sh
./install.sh
OR
sh install.sh

Network Check —————————————————-
ip a
sudo lshw -class network

Permanent IP
sudo nano /etc/netplan/00-installer-config.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses: [192.168.67.2/24]
gateway4: 192.168.67.16
nameservers:
addresses: [8.8.8.8,8.8.4.4]
Dynamic IP
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: true
dhcp6: true
sudo netplan try
sudo netplan apply
hostnamectl status
sudo hostnamectl set-hostname esscdynatraceb1.canadacentral.cloudapp.azure.com
wordpress.djurasovic.com
sudo netstat -lnpt port listening
netstat -ano port listening

Time Services
date

# Change Time Zone
sudo nano /etc/timezone
sudo timedatectl set-timezone America/Toronto

# Setup NTP

sudo apt install systemd-timesyncd
sudo timedatectl set-ntp true
sudo nano /etc/systemd/timesyncd.conf add time.nrc.ca
systemctl restart systemd-timesyncd.service
timedatectl show-timesync
timedatectl status
timedatectl timesync-status