zellda's

notes on Linux

Linux Commands Update III

Debian - should work on most distros though:

Files/Directories:

Files:

List files:
ls
dir
Create file:
touch
Delete file:
rm
Copy file:
cp
Move file:
mv
Display content of file:
cat
less
tail

Directories:

Create directory:
mkdir
Delete directory:
rm -r
rm -rf
Delete empty directory:
rmdir
Copy directory:
cp -r dir1 dir2

Create symbolic/soft links:
ln -s destination/file destination/link

Archives:

Extract filename.tar:
tar -xvf filename.tar
create filename.tar:
tar -cvf filename.tar filename

Extract filename.tar.gz:
tar -xvzf filename.tar.gz
create filename.tar.gz:
tar -czpf /path/filename.tar.gz /path/file(s)-to-archive

Extract filename.tar.bz2:
tar jxvf filename.tar.bz2
create filename.tar.bz2:
tar jxvf filename.tar.bz2 dir1 dir2 file1 file2 …

Extract filename.rar:
unrar-free filename.rar
unrar x filename.rar

Extract filename.zip:
unzip filename.zip
Create filename.zip:
zip filename.zip dir1 dir2 …

Package management:

Command line installation:
dpkg -i filename.deb

Apt:

Fix broken packages:
apt-get install -f
Search for packages:
apt-cache search package-name
Install packages without dependencies:
apt-get –no-install-recommends install package-name
Update:
apt-get update
Upgrade:
apt-get upgrade
apt-get dist-upgrade
Clean cache:
apt-get clean
apt-get autoclean

System info:

General:
uname -a
Find out version:
lsb_release -a
Installation date:
sudo dumpe2fs /dev/sda1 | grep ‘Filesystem created:’
or:
ls /var/lib/dpkg/info/*.list -lht | tail -1 | awk ‘{print $6}’
disk space:
df -h
List drives:
fdisk -l
Network adapter:
ifconfig
iwconfig
Info hardware:
inxi
Info graphics:
inxi -G
Bus-system:
lspci
Bus-system verbose:
lspci -v
Network:
lcpci | grep -i net
Wireless adapter:
iwconfig

Network:

Network adapter:
lcpci | grep -i net
Wireless adapter:
iwconfig
Stop network:
ifdown eth0
Start network:
ifup wlan0

SSH - Secure Shell

Connect to remote machine:
ssh user@machine-ip
Copy file from local to remote machine with Secure Copy - scp:
scp /path/to/file user@machine-ip:/path/to/destination
Copy file from remote to local machine:
scp user@machine-ip:/path/to/file /path/to/destination
Copy directory from local to remote machine:
scp -r /path/to/directory user@machine-ip:/path/to/destination

Users / Groups:

Users:

List users:
lastlog
cat /etc/passwd
Add user:
adduser username
Delete user:
deluser username
delete files:
deluser -r username
Change password:
passwd username
List group membership:
groups username
detailed:
id username

Groups:

List all groups:
id
cat /etc/group
(cat /etc/group |cut -d: -f1; cat /etc/passwd | cut -d: -f1)
Add group:
addgroup groupname
Delete group:
delgroup groupname
Add user to group:
adduser username groupname
gpasswd -a user group
e.g.:
gpasswd -a daniel kvm
Delete user from group:
gpasswd -d user group
Edit groups:
vim /etc/group

Manage rights:

Classic way (chmod, chown, chgrp) - a file/program can have only one owner/group:

Chown:
chown new_owner file
chown new_owner:new ownergroup file
chown :new_ownergroup file

Chgrp:
chgrp new_ownergroup file

  special rights     owner     group     others
rights     sticky, suid, sgid     x, r, w     x, r, w     x, r, w  
octal     1, 4, 2     1, 4, 2     1, 4, 2  
chmod     u=     g=     o=  

examples:
chmod 754 file
or:
chmod u=rwx,g=rx,o=r file
make executable:
chmod +x filename

ACL - Access Control List (getfacl, setfacl) - refined user-rights management:
First make sure acl is installed and activated (kernel, fstab)!

Usage:
setfacl -m u:user_x:xrw,u:user_z:r,g:group_a:rw,g:group_b:r,o::––– file/program

parameters:
-m (modifiy) assign/change rights
-d (default) assign default rights
-R (recursive) assign rights recursively
-b (remove all ACL-rights)
-k (remove default rights)

Daemons/Services:

Start daemon:
modprobe daemon_name
e.g.:
modprobe kvm
Services:
/etc/init.d/service start
/etc/init.d/service stop
e.g.:
samba, mysql, apache

Modules:

Blacklist modules:
vim /etc/modprobe.d/module-name.conf
e.g.:
vim /etc/modprobe.d/wlan.conf
add:
blacklist module-name
e.g.:
blacklist bcma
blacklist acer_wmi

Backups

Backup home with rsync (example):
rsync -arvu /home/user /run/media/user/external-drive/backups
Backup / with tar (example):
sudo tar cvpzf /media/external-drive/backups/backup-name`date +%d%h%y-%H:%M`.tgz ––exclude=”/proc/*” ––exclude=”/lost+found/*” ––exclude=”/dev/*” ––exclude=”/mnt/*” ––exclude=”/media/*” ––exclude=”/run/media/user/*” ––exclude=”/sys/*” ––exclude=”/tmp/*” ––exclude=”/var/cache/pacman/*” ––exclude=”/home/user/*” ––exclude=”/root/.thumbnails/*” /

Storage / Partitions:

Available disk space:
df -h
List drives:
fdisk -l
Mount drive:
mkdir /media/drive
mount /dev/sdh /media/drive
Unmount drive:
umount /media/drive
Copy Image to USB:
sudo dd if=/path/to/image/imagename.iso of=/dev/sdx bs=4M;sync
(works similarly with partitions)

Samba

Mount a samba share:
mount -t smbfs //IP/shared_folder /folder-to-mount-shared-folder -o username=user,password=password-for-user
or:
mount -t cifs //xyz.xyz.xyz.xyz/shared-folder /folder-to-mount-shared-folder -o username=user,password=password-for-user

Mysql

Login as root:
mysql -u root -p

Reset root password:

1) Stop mysql:
/etc/init.d/mysql stop
service mysql stop
rc.d stop mysql
2) Then (as root):
mysqld –skip-grant-tables &
mysql -u root mysql
UPDATE user SET Password=PASSWORD(‘YOURNEWPASSWORD’) WHERE User=’root’; FLUSH PRIVILEGES; exit;

This worked on Suse:
mysqladmin -u root password new password
or:
mysqladmin -u root -h hostname password new-password

Grub:

Update grub:
update-grub

Arch Linux:

Package management:

AUR - Arch User Repository:

Extract:
tar -xvzf package
Make:
makepkg -s
Install:
pacman -U package.pkg.tar.xz

Pacman:

Search for packages in database:
pacman -Ss package-name
Search for installed packages on host:
pacman -Qs package-name
Install packages:
pacman -S package-name
System update & upgrade:
pacman -Syu
Clean pacman cache - not installed packages and old versions of installed packages:
pacman -Sc
Remove packages:
pacman -R package-name
Remove packages with dependencies:
pacman -Rs package-name

Have fun!

Comments