linux command line
System Management
- Restart the machine:
1
|
sudo reboot
|
- Shutdown the machine:
1
|
sudo halt
|
User Management: How do I add a new user in Linux?
Without a home directory
1
2
|
sudo useradd myuser
|
With home directory
1
2
|
sudo useradd -m myuser
|
Then set the password
1
2
|
sudo passwd myuser
|
Then set the shell
1
|
sudo usermod -s /bin/bash myuser
|
Disk Management: How much disk space left on Linux machine?
- Checking for disk space on available volumes:
1
|
df -h command
|
Check for size of folders in current working directory:
1
|
du -h --max-depth=1
|
App Management: How do I install new software in Linux?
Install new software in Debian / Ubuntu Linux
1
|
sudo apt-get install package>
|
Install new software in Gentoo Linux
1
|
sudo emerge package>
|
Install software from source
1
2
3
4
5
|
tar xvfz package.tar.gz
cd package
./configure
make
make install
|
Install rpm packages
1
|
rpm -ivh file.rpm
|
Filesystem: Basics
Create a directory in Linux
1
|
mkdir Directory>
|
Remove a directory in Linux
1
|
rm -rf Directory>
|
List files in directory
1
|
ls -l
|
Enter a directory
1
|
cd Directory>
|
Up a directory
1
|
cd ..
|
Execute a program
1
|
./filename
|
Filesystem: How do I compress/decompress files?
Create .tar.gz / .tgz files:
1
|
tar -czvf {output.tgz} {folder}
|
Extract .tar.gz / .tgz files:
1
|
tar -xzvf {input.tgz} {path}
|
Create .zip files:
1
|
zip -r {output.zip} {folder}
|
Extract .zip files to directory:
1
|
unzip {input.zip} -d {folder}
|
Watch a log file real-time:
1
|
tail -f {file}
|
Find files containing text:
1
|
grep -Rl {search} {path}
|
Download a file from the internet:
1
|
wget {url}
|
Back up your stuff locally:
1
|
rsync {source} {destination}
|
Back up your stuff to another server:
1
|
rsync -avz -e ssh {path} {user}@{host}:{path}
|
File Permissions
Make a file executable:
1
|
chmod +x {filename}
|
Environment variables
Inspect the current PATH variables:
1
|
echo $PATH
|
Append directory to the existing
1
|
PATH: export PATH=$PATH:/path/to/binary
|
Declare a new environment variable:
1
|
export {variable}=/path/to/binary
|
Edit system-wide environment variables:
1
|
nano /etc/environment
|
Create an alias for a command:
1
|
nano ~/.bash_aliases followed by alias {alias name}='{command}'
|
Process Management
Kill a process:
1
|
kill {pid}
|
See all running processes:
1
|
ps aux
|
Get PIDs of running processes under a specific name:
1
|
ps aux | grep {name}
|
Anytime a process stops, start it
1
|
initctl upstart
|
Machine Properties
Modify the host name:
1
|
sudo nano /etc/hostname
|
Set a static IP address:
1
|
sudo nano /etc/network/interfaces
|
Modify hosts:
1
|
sudo nano /etc/hosts
|
Cron Jobs
Check existing cron jobs:
1
|
crontab -e
|
Networking
What IP adress is assiged to me?
1
|
ifconfig
|
What TCP/IP ports are open and what programs are listening on them?
1
|
lsof -i -P
|
(needs to have lsof installed and be run as root)
Where do I configure my network interfaces?
1
|
nano /etc/network/interfaces
|
then run ifdown
and then ifup
What are the details of a specific network interface?
1
|
iwlist interface> scan
|
Connect to a specific wireless network for a given interface?
1
|
iwconfig interface> essid "
|
Get an IP address assigned from a router?
1
|
dhclient interface>
|
How do I list all NAT rules?
1
|
iptables -L
|
What are all of the available network devices for this machine?
1
|
lspci
|
Filesystem Management
View available files systems on your machine:
1
2
|
# cd /sbin
# ls mkfs*
|
Write a new file system to a device (DANGEROUS!)
1
|
mkfs -t ext3 /dev/sda6
|
Check for bad blocks on a device
1
|
mke2fs -c /dev/sda6
|
Force creating a file system on a new device
1
|
mke2fs -F /dev/sda6
|
Set volume label
1
|
mke2fs -L DATA /dev/sda6
|
Partition Management
Manipulate partition table
1
|
fdisk
|
Manipulate partition table (gui)
1
|
sudo cfdisk /dev/sda
|
Show disk information, display attributes of a physical volume. This will output interesting information about your partitions.
1
|
sudo pvdisplay
|
Creates a volume group descriptor at the start of the /dev/hdb1 partition.
1
|
pvcreate /dev/hdb1
|
Resize logical volume
1
2
3
4
|
#You can extend a Logical Volume with:
sudo lvextend -L +5g foo/bar
#After extending the Logical Volume you need to expand the filesystem to use the new space.
sudo resize2fs /dev/foo/bar
|
Destroy partition table of your disk (VERY DANGEROUS)
1
2
|
# dd if=/dev/zero of=/dev/diskname bs=1k count=1
# blockdev --rereadpt /dev/diskname
|